Multiple ifs (Was: RE: Simplified production for FilterExpr in XPath spec?)

Subject: Multiple ifs (Was: RE: Simplified production for FilterExpr in XPath spec?)
From: Jeni Tennison <Jeni.Tennison@xxxxxxxxxxxxxxxx>
Date: Tue, 18 Jul 2000 11:25:42 +0100
Jukka,

>But that "sem[@role='user']" doesnt work, am I away from right answer ??

There are two problems with your XSLT template.

The first lies in the XPaths that you're using.  Your xsl:ifs appear within
a template that matches a 'sem' element.  Within the template, the 'sem'
element is the current node.  The XPaths in the xsl:if tests are of the
form sem[@role='user'], which looks for a 'sem' element child of the
current node with a 'role' attribute with a value of 'user'.  You're
actually interested in the @role attribute of the current node, the 'sem'
element, rather than any 'sem' child, so you should try using the XPath
"@role='user'" and "@role='error'" instead.

The second lies in the use of multiple xsl:if elements within the
xsl:attribute.  Whenever an xsl:if tests true, then the contents of that
xsl:if is processed.  This means that, given <sem role="error" />, the XSLT
processor evaluates:

 - @role='user' = false
 - @role='error' = true
 - @role = true

As both @role='error' and @role are true, you get an output of:

  <sem type="parameterdoesnt work" />

which isn't what you wanted.

Instead, use the xsl:choose element with xsl:when:

<xsl:template match="sem"> 
  <sem>
    <xsl:attribute name="type">
      <xsl:choose>
        <xsl:when test="@role='user'">state</xsl:when>     
        <xsl:when test="@role='error'">parameter</xsl:when>
        <xsl:when test="@role">doesnt work</xsl:when>
      </xsl:choose> 	
    </xsl:attribute>
    <xsl:apply-templates/>	
  </sem>
</xsl:template>

or, possibly:

<xsl:template match="sem"> 
  <sem>
    <xsl:attribute name="type">
      <xsl:choose>
        <xsl:when test="@role='user'">state</xsl:when>     
        <xsl:when test="@role='error'">parameter</xsl:when>
        <xsl:otherwise>doesnt work</xsl:otherwise>
      </xsl:choose> 	
    </xsl:attribute>
    <xsl:apply-templates/>	
  </sem>
</xsl:template>

This is tested and works with SAXON.

I hope that helps,

Jeni

Dr Jeni Tennison
Epistemics Ltd, Strelley Hall, Nottingham, NG8 6PE
Telephone 0115 9061301 ? Fax 0115 9061304 ? Email
jeni.tennison@xxxxxxxxxxxxxxxx



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


Current Thread