如何在xslt中使用for-each?

时间:2018-08-10 20:45:57

标签: xml xslt

我有带一组教育括号的XML。 我使用包含相关代码的xml-stylesheet:

  <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>

但是,这似乎没有显示相关值。 我放了

      <xsl:value-of select="CV/education/college"/>
在每个括号之后的

似乎有效。 您能否让我知道用过的for-for中的每项都不正确?

1 个答案:

答案 0 :(得分:3)

您将忽略表达式中的当前节点:<xsl:for-each select="CV/education">将当前节点设置为CV/education,因此您不必在xsl:value-of表达式中重复该路径。相对路径相对于当前节点进行评估。

将代码简化为

<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>

它应该可以正常工作。