Html XSLT 中的嵌套 for-each 循环不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23530495/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 01:44:01  来源:igfitidea点击:

Nested for-each loops in XSLT not working

htmlxmlxsltnestedxls

提问by user3614312

I cant seem to get this nested for loop to work correctly, I want to print all of the tracks on the EP in the row with the EP name and other details. Everything from the first for-each loop displays correctly but nothing is pulled through for the for-each loop to get the tracks.

我似乎无法让这个嵌套的 for 循环正常工作,我想用 EP 名称和其他详细信息在行中打印 EP 上的所有曲目。第一个 for-each 循环中的所有内容都正确显示,但没有为 for-each 循环拉取任何内容来获取曲目。

Here is my XML

这是我的 XML

<dalehoward>

<ep>
    <name>Letters EP</name>
    <year>2012</year>
    <label>Static Audio</label>
    <image>letters.jpg</image>

        <tracks>
        <track number="1">
            <tname>Letters</tname>
            <length>6.35</length>
        </track>
        <track number="2">
            <tname>Later</tname>
            <length>7.56</length>
        </track>
            <track number="3">
            <tname>'89 Flava</tname>
            <length>7:38</length>
        </track>
        <track number="4">
            <tname>Safe Presentation</tname>
            <length>7.55</length>
        </track>
        </tracks>
</ep>



<ep>
    <name>Inner City EP</name>
    <year>2012</year>
    <label>Lost My Dog</label>
    <image>innercity.jpg</image>

    <tracks>
    <track number="1">
            <tname>C'Mon</tname>
            <length>7.15</length>
        </track>
        <track number="2">
            <tname>Koppabird</tname>
            <length>6.27</length>
        </track>
        <track number="3">
            <tname>Inner City</tname>
            <length>8:50</length>
        </track>
        <track number="4">
            <tname>You Can</tname>
            <length>8:16</length>
        </track>
        <tracks>
</ep>
<dalehoward>

and here is the XSLT

这是 XSLT

<xsl:variable name="imagefolder" select="'xml/images/'" />
<xsl:template match="/">


<html>
<body>
<h2>My CD Collection</h2>
<table border="1" width="100%">
  <tr bgcolor="#9acd32">
    <th style="text-align:left">Title</th>
    <th style="text-align:left">Year</th>
    <th style="text-align:left">Label</th>
    <th style="text-align:left">Tracks</th>
    <th style="text-align:left">Artwork</th>
  </tr>
  <xsl:for-each select="dalehoward/ep">
  <tr>
    <td><xsl:value-of select="name"/></td>
    <td><xsl:value-of select="year"/></td>
    <td><xsl:value-of select="label"/></td>
    <td>testtext<xsl:for-each select="dalehoward/ep/tracks">
        <xsl:value-of select="tname"/><br />
        <xsl:value-of select="length"/> <br /><br />
        </xsl:for-each></td>
    <td><img width="150px" height="150px"><xsl:attribute name="src">
             <xsl:copy-of select="$imagefolder"/>
             <xsl:value-of select="image"/>
             </xsl:attribute></img></td>
  </tr>
  </xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>

Thanks a lot in advance for any help

非常感谢您的帮助

回答by michael.hor257k

The outer loop puts you in the context of ep. The context of the inner loop needs to be established from there (or as an absolute path, starting from the root) - so change:

外循环将您置于ep. 需要从那里建立内部循环的上下文(或作为绝对路径,从根开始) - 所以改变:

<xsl:for-each select="dalehoward/ep/tracks">

to:

到:

<xsl:for-each select="tracks/track">