简体   繁体   中英

XSLT: for-each in for-each not working?

i've a XML file which looks like:

<VersionHistory>
  <Release Version="0.0.1" Date="27/1/2011">
    <NewFeature>foo</NewFeature>
    <BugFix>some text</BugFix>
    <BugFix>some text</BugFix>
    <BugFix Ticket="12004">some text</BugFix>
  </Release>
  <Release Version="0.0.2" Date="15/2/2011">
    <NewFeature>foobar</NewFeature>
    <BugFix>some more text</BugFix>
    <BugFix>some more text</BugFix>
    <BugFix Ticket="12001">some more text</BugFix>
  </Release>
</VersionHistory>

Now my XSLT looks like this:

  <xsl:template match="/">
    <xsl:for-each select="VersionHistory/Release">
      <xsl:sort select="@Version"/>
  <ul>
        <li>
          <h3>New Feature<br/></h3>
          <xsl:for-each select="NewFeature">
            <ul>
              <li>
                <xsl:value-of select="NewFeature"/>
              </li>
            </ul>
          </xsl:for-each>
        </li>
         <h3>
            Fixed<br/>
          </h3>
            <xsl:for-each select="BugFix">
              <ul>
                <li>
                  <p>
                    <xsl:value-of select="BugFix"/>
                  </p>
                </li>
              </ul>
            </xsl:for-each>
        </li>
...

My problem is, that the 2nd for-each which goes through the BugFixes creates the amout of list items the xml contains of this element.. but i dont get the text which is between . Why? How can i fix this?

this is not only for BugFix of course.. its for all this elements like BugFix, NewFeature (there are some more.. i haven't list here)

greets

In your for-each you select NewFeature , then in the value-of , you are calling the value of a child-node called NewFeature :

      <xsl:for-each select="NewFeature">
        <ul>
          <li>
            <xsl:value-of select="NewFeature"/>
          </li>
        </ul>
      </xsl:for-each>

Since NewFeature doesn't contain a NewFeature node, you see nothing.

This would work (selecting the current node in the for-each loop):

      <xsl:for-each select="NewFeature">
        <ul>
          <li>
            <xsl:value-of select="."/>
          </li>
        </ul>
      </xsl:for-each>

But a better option would be to use a template (which you should to with all of your for-each loops):

<xsl:template match="NewFeature">
  <ul>
    <li><xsl:value-of select="."/></li>
  </ul>
</xsl:template>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM