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

Subject: Re: [xsl] processing multiple values in a single attribute
From: "Matthew L. Avizinis" <mla@xxxxxxxxx>
Date: Fri, 15 May 2009 18:29:34 -0400
I would add that you should use <xsl:choose> and have two <xsl:when> tests:
<xsl:choose>
<xsl:when test="not($string = '') and 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:when>
<xsl:when test="not($string = '')">
    <statemanipulation>
      <variableref name="{$string}"/>
       <fault fault-code="{$string}" fault-state="ok"/>
    </statemanipulation>
</xsl:when>
</xsl:choose>
in order to ensure it grabs the last value of the string.
Of course, having a tokenize function would be loads easier if it's
available to you...
Peace,
Matthew Avizinis
Gleim Publications, Inc. (www.gleim.com)

G. Ken Holman wrote:
>
> <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