Wednesday, April 20, 2011

problem with using XPath to locate some part of XML

Duplicate of

HOw to make the text repeat only once every for-each?

I have problem with using XPATH to point to some element in XML. Here is my XML code:

    <character>
      <name>some name </some name>
    </character>

  </quote>
 <quote>
  ....
  </quote>
 </quotes>

and here My XSLT code:

<xsl:for-each select="quotes/quote">
    <xsl:value-of select="quotes/quote/character/name"/>
 </xsl:for-each>

WHat I try to do is that I try to list the name of character for every quote. BUt this code does not work WOuld you please help me with this problem? Thank you

From stackoverflow
  • The error is:
    You use quotes/quote twice

    You might try this one:

    <xsl:for-each select="quotes/quote">
      <xsl:value-of select="character/name"/>
    </xsl:for-each>
    
  • Inside the for-each, the context node is quote - so it looks like you just need:

    <xsl:for-each select="quotes/quote">
        <xsl:value-of select="character/name"/>
    </xsl:for-each>
    

    Of course, the purists would argue that it would be better to use a match:

    <xsl:apply-templates select="quotes/quote" />
    ...
    <xsl:template match="quote">
        <xsl:value-of select="character/name"/>
    </xsl:template>
    

0 comments:

Post a Comment