xsl-list
[Top] [All Lists]

Re: XPath expression confusion

2006-01-12 15:51:19
Ah, nice one! (one for the Quiz perhaps!)

The = operator is a string compare operator. The node set with number attributes on the left hand of the test is converted to strings, but it seems to give an empty result for attributes, resulting in an always failing test. I'm not sure wether this is according to the XSLT req, I would expect behaviour as if each attribute was converted with the string function internally, more or less.

If you put the attributes in the for-each and the $all-req on the left hand of the test it does work. So in principle you were on the right track. But I'm afraid that switching them does not give the result you are looking for.

Another approach is to put the comparison with the attribute in a predicate test. That way it is no longer a node set versus node comparison, but an attribute versus node comparison. That seems to give better results:

      <xsl:for-each select="$all-reqs">
        <xsl:variable name="number" select="." />
        <xsl:choose>
          <xsl:when test="/system/components/component/requirement[(_at_)number = 
$number]" />
          <xsl:otherwise>
            <requirement><xsl:copy-of select="." /></requirement>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:for-each>

Cheers,
Geert

PS: perhaps put the result of /system/components/component/requirement in a variable outside the loop. That will perform better on large sets..

cknell(_at_)onebox(_dot_)com wrote:
<xsl:variable name="all-reqs" 
select="document('requirements.xml')/requirements//requirement/number" />

  <xsl:template match="/">
    <un-assigned-reqs>
      <xsl:for-each select="$all-reqs">
        <xsl:choose>
          <xsl:when test="/system/components/component/requirement/@number = ." 
/>
          <xsl:otherwise>
            <requirement><xsl:value-of select="." /></requirement>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:for-each>
    </un-assigned-reqs>
  </xsl:template>

It was my belief that the XPath expression in the test would search all the 
number attribute values in the document for a value that matched the value of 
the current iteration of the loop.
Instead, it seems that there are no matches, as I get one element output for 
each iteration of the loop.

--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--