Re: choose on element content.

Subject: Re: choose on element content.
From: "G. Ken Holman" <gkholman@xxxxxxxxxxxxxxxxxxxx>
Date: Tue, 24 Aug 1999 10:43:18 -0400
At 99/08/23 16:28 +0100, DPawson@xxxxxxxxxxx wrote:
given
<INDI ID='I001'>
<NAME>John Derek <S>Montalt (Moult)</S></NAME>
<SEX>M</SEX>

I need conditional processing, dependent on the sex of
the NAMEd person.

I'm getting a syntax error with

<xsl:template match="INDI">
 <xsl:choose>
   <xsl:when test="[string(SEX)='M']">
           ^^^^^^^^^^^^^^^^^^ syntax error.^^^^^^^^^^^^^

Predicates are allowed as filters on a node test, not standalone as the above shows.


But predicates aren't what you need ... you can just write an expression that uses the value of the child and compares it to a string. The value is a string already, so no conversions are required.

An example is below ... I hope this helps.

........... Ken


T:\FTEMP>type test.xml <?xml version="1.0"?> <test> <person><sex>M</sex><name>John</name></person> <person><sex>F</sex><name>Jane</name></person> </test> T:\FTEMP>type test.xsl <?xml version="1.0"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/XSL/Transform/1.0";>

<xsl:template match="/test">
  <xsl:for-each select="person">
    <xsl:choose>
      <xsl:when test="sex='M'">&#xa;Male: </xsl:when>
      <xsl:when test="sex='F'">&#xa;Female: </xsl:when>
      <xsl:otherwise>&#xa;Who knows?: </xsl:otherwise>
    </xsl:choose>
    <xsl:value-of select="name"/>
  </xsl:for-each>
</xsl:template>

</xsl:stylesheet>

T:\FTEMP>call xsl test.xml test.xsl test.txt
T:\FTEMP>type test.txt

Male: John
Female: Jane
T:\FTEMP>

--
G. Ken Holman                    mailto:gkholman@xxxxxxxxxxxxxxxxxxxx
Crane Softwrights Ltd.             http://www.CraneSoftwrights.com/s/
Box 266, Kars, Ontario CANADA K0A-2E0   +1(613)489-0999   (Fax:-0995)
Website:  XSL/XML/DSSSL/SGML services, training, libraries, products.
Practical Transformation Using XSLT and XPath      ISBN 1-894049-01-2
Next instructor-led training:                     MT'99 1999-12-05/06


XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list



Current Thread