Re: [xsl] processing multiple values in a single attribute

Subject: Re: [xsl] processing multiple values in a single attribute
From: "G. Ken Holman" <gkholman@xxxxxxxxxxxxxxxxxxxx>
Date: Fri, 15 May 2009 18:13:01 -0400
At 2009-05-15 17:16 -0400, Charlie O wrote:
I need to convert each value of single attribute to an XML structure. I'm not really sure where to start.

For XSLT 1.0 you need a recursive eating one token at a time.


I'm using XSL 1.0.

This is so much easier in XSLT 2.0. I've included both solutions below.


I hope this helps.

. . . . . . Ken


T:\ftemp>type o.xml <FAULT-REF FAULTS="PF01 PF02 PF03 PF04 PF05 PF06 PF07 PF08 PF09 PF10 PF11"/>

T:\ftemp>type o.xsl
<?xml version="1.0" encoding="US-ASCII"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
                version="1.0">

<xsl:output indent="yes"/>

<xsl:template match="FAULT-REF">
  <xsl:call-template name="do-tokens">
    <xsl:with-param name="string"
                    select="concat(normalize-space(@FAULTS),' ')"/>
  </xsl:call-template>
</xsl:template>

<xsl:template name="do-tokens">
  <xsl:param name="string"/>
  <xsl:if test="contains($string,' ')">
    <statemanipulation>
      <variableref name="{substring-before($string,' ')}"/>
       <fault fault-code="{substring-before($string,' ')}" fault-state="ok"/>
    </statemanipulation>
    <xsl:call-template name="do-tokens">
      <xsl:with-param name="string" select="substring-after($string,' ')"/>
    </xsl:call-template>
  </xsl:if>
</xsl:template>

</xsl:stylesheet>
T:\ftemp>call xslt o.xml o.xsl
<?xml version="1.0" encoding="utf-8"?>
<statemanipulation>
   <variableref name="PF01"/>
   <fault fault-code="PF01" fault-state="ok"/>
</statemanipulation>
<statemanipulation>
   <variableref name="PF02"/>
   <fault fault-code="PF02" fault-state="ok"/>
</statemanipulation>
<statemanipulation>
   <variableref name="PF03"/>
   <fault fault-code="PF03" fault-state="ok"/>
</statemanipulation>
<statemanipulation>
   <variableref name="PF04"/>
   <fault fault-code="PF04" fault-state="ok"/>
</statemanipulation>
<statemanipulation>
   <variableref name="PF05"/>
   <fault fault-code="PF05" fault-state="ok"/>
</statemanipulation>
<statemanipulation>
   <variableref name="PF06"/>
   <fault fault-code="PF06" fault-state="ok"/>
</statemanipulation>
<statemanipulation>
   <variableref name="PF07"/>
   <fault fault-code="PF07" fault-state="ok"/>
</statemanipulation>
<statemanipulation>
   <variableref name="PF08"/>
   <fault fault-code="PF08" fault-state="ok"/>
</statemanipulation>
<statemanipulation>
   <variableref name="PF09"/>
   <fault fault-code="PF09" fault-state="ok"/>
</statemanipulation>
<statemanipulation>
   <variableref name="PF10"/>
   <fault fault-code="PF10" fault-state="ok"/>
</statemanipulation>
<statemanipulation>
   <variableref name="PF11"/>
   <fault fault-code="PF11" fault-state="ok"/>
</statemanipulation>
T:\ftemp>type o2.xsl
<?xml version="1.0" encoding="US-ASCII"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
                version="2.0">

<xsl:output indent="yes"/>

<xsl:template match="FAULT-REF">
  <xsl:for-each select="tokenize(normalize-space(@FAULTS),' ')">
    <statemanipulation>
      <variableref name="{.}"/>
       <fault fault-code="{.}" fault-state="ok"/>
    </statemanipulation>
  </xsl:for-each>
</xsl:template>

</xsl:stylesheet>
T:\ftemp>call xslt2 o.xml o2.xsl
<?xml version="1.0" encoding="UTF-8"?>
<statemanipulation>
   <variableref name="PF01"/>
   <fault fault-code="PF01" fault-state="ok"/>
</statemanipulation>
<statemanipulation>
   <variableref name="PF02"/>
   <fault fault-code="PF02" fault-state="ok"/>
</statemanipulation>
<statemanipulation>
   <variableref name="PF03"/>
   <fault fault-code="PF03" fault-state="ok"/>
</statemanipulation>
<statemanipulation>
   <variableref name="PF04"/>
   <fault fault-code="PF04" fault-state="ok"/>
</statemanipulation>
<statemanipulation>
   <variableref name="PF05"/>
   <fault fault-code="PF05" fault-state="ok"/>
</statemanipulation>
<statemanipulation>
   <variableref name="PF06"/>
   <fault fault-code="PF06" fault-state="ok"/>
</statemanipulation>
<statemanipulation>
   <variableref name="PF07"/>
   <fault fault-code="PF07" fault-state="ok"/>
</statemanipulation>
<statemanipulation>
   <variableref name="PF08"/>
   <fault fault-code="PF08" fault-state="ok"/>
</statemanipulation>
<statemanipulation>
   <variableref name="PF09"/>
   <fault fault-code="PF09" fault-state="ok"/>
</statemanipulation>
<statemanipulation>
   <variableref name="PF10"/>
   <fault fault-code="PF10" fault-state="ok"/>
</statemanipulation>
<statemanipulation>
   <variableref name="PF11"/>
   <fault fault-code="PF11" fault-state="ok"/>
</statemanipulation>
T:\ftemp>rem Done!



--
XSLT/XSL-FO/XQuery hands-on training - Los Angeles, USA 2009-06-08
Crane Softwrights Ltd.          http://www.CraneSoftwrights.com/s/
Training tools: Comprehensive interactive XSLT/XPath 1.0/2.0 video
Video lesson:    http://www.youtube.com/watch?v=PrNjJCh7Ppg&fmt=18
Video overview:  http://www.youtube.com/watch?v=VTiodiij6gE&fmt=18
G. Ken Holman                 mailto:gkholman@xxxxxxxxxxxxxxxxxxxx
Male Cancer Awareness Nov'07  http://www.CraneSoftwrights.com/s/bc
Legal business disclaimers:  http://www.CraneSoftwrights.com/legal

Current Thread