1

I want to know how to loop over the attribute of the looping element in xslt. I have following xml structure:

<catalog>
    <cd v="1">
        <title>Empire Burlesque</title>
        <artist>Bob Dylan</artist>
        <country>USA</country>
        <company>Columbia</company>
        <price>10.90</price>
        <year>1985</year>
    </cd>
    <cd v="2">
        <title>Hide your heart</title>
        <artist>Bonnie Tyler</artist>
        <country>UK</country>
        <company>CBS Records</company>
        <price>9.90</price>
        <year>1988</year>
    </cd>

And my xslt is as follows:

 <xsl:for-each select="catalog/cd">
  <tr>
    <td><xsl:value-of select="../cd/@v"/></td>
    <td><xsl:value-of select="title"/></td>
    <td><xsl:value-of select="artist"/></td>
  </tr>
  </xsl:for-each>

What happens with this xslt is that, it fetches the attribute for cd from the first tag only. So for all the rows the value will be 1. How can i get it to iterate over the other attribute values also if there are more elements? Am i doing something wrong?

1 Answer 1

1

Instead of:

<xsl:value-of select="../cd/@v"/>

use simply:

<xsl:value-of select="@v"/>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.