mateuszs
mateuszs

Reputation: 165

How to use for-each in xslt?

I have XML which has one set of education brackets. I use xml-stylesheet which contains related code:

  <xsl:for-each select="CV/education">
  <xsl:value-of select="CV/education/college"/>
  <xsl:value-of select="CV/education/fieldOfStudy"/>
  <xsl:value-of select="CV/education/studyStartDate"/>
  <xsl:value-of select="CV/education/studyEndDate"/>
  </xsl:for-each>

This however does not seem to display related values. I have put

      <xsl:value-of select="CV/education/college"/>

after for-each bracket and it seems to work. Can you please let me know what is not correct in used for-each?

Upvotes: 1

Views: 37

Answers (1)

zx485
zx485

Reputation: 29022

You are ignoring the current node in your expressions: <xsl:for-each select="CV/education"> sets the current node to CV/education, so you don't have to repeat that path in your xsl:value-of expressions. Relative paths are evaluated relative to the current node.

Simplify your code to

<xsl:for-each select="CV/education">
  <xsl:value-of select="college"/>
  <xsl:value-of select="fieldOfStudy"/>
  <xsl:value-of select="studyStartDate"/>
  <xsl:value-of select="studyEndDate"/>
</xsl:for-each>

and it should work as desired.

Upvotes: 3

Related Questions