Thread: xsl question
View Single Post
  #3 (permalink)  
Old September 14th, 2005, 03:15 AM
mhkay's Avatar
mhkay mhkay is offline
Wrox Author
Points: 18,487, Level: 59
Points: 18,487, Level: 59 Points: 18,487, Level: 59 Points: 18,487, Level: 59
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

You haven't understood the concept that xsl:for-each changes the context node, and relative path expressions start from the context node.

    <xsl:for-each select="//HEADER/LINK/@name">

This means that the context node is now an attribute, the @name attribute. (Also note, HEADER is the outermost element of your document, so you don't need to search the whole document to find it. Use /HEADER rather than //HEADER).

        <xsl:if test="@name=$LINK">

@name is a relative path expression, it looks for an attributes of the context node called name. But attributes don't have attributes, so it won't find one. You want test=". = $LINK".

        <xsl:for-each select="//HEADER/LINK/TITLE">

This is a so-called "absolute path expression". It doesn't start from the context node, it starts from the root. So you're selecting all the TITLE elements, not just the one for the selected LINK. You want the TITLEs of the LINK that's the parent of the selected attribute: select="../TITLE". Or most people would do it like this:

<xsl:for-each select="/HEADER/LINK[@name=$LINK]">
        <a>
      <xsl:attribute name="HREF">test<xsl:value-of select="TITLE"/>test</xsl:attribute>
      <xsl:value-of select="@name" />

      </a>
 </xsl:for-each>

Go back to the spec or your favourite reference book, and reread the section about the context node.



Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
Reply With Quote