0

XML 代码:

<?xml version="1.0" encoding="ISO-8859-1"?>
<catalog>
<notold>
    <list>1</list>
    <list>2</list>
</notold>
<cd>
    <title>Empire Burlesque</title>
    <artist>Bob Dylan</artist>
    <country>USA</country>
    <company>Columbia</company>
    <price>10.90</price>
    <year>1985</year>
</cd>
<cd>
    <title>Hide your heart</title>
    <artist>Bonnie Tyler</artist>
    <country>UK</country>
    <company>CBS Records</company>
    <price>9.90</price>
    <year>1988</year>
</cd>
<cd>
    <title>Greatest Hits</title>
    <artist>Dolly Parton</artist>
    <country>USA</country>
    <company>RCA</company>
    <price>9.90</price>
    <year>1985</year>
</cd>
</catalog>

XSLT 代码:

    <?xml version="1.0" encoding="ISO-8859-1"?>
<!-- Edited by XMLSpy® -->
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="/">
    <h2>My CD Collection</h2>
    <table border="1">
    <tr bgcolor="#9acd32">
    <th>Title</th>
    <th>Artist</th>
        </tr>
        <xsl:for-each select="catalog/cd">
        <tr>

        <td><xsl:value-of select="title"/></td>
        <td><xsl:value-of select="artist"/></td>
        <td><xsl:value-of select="year"/></td>
        <xsl:if test='year=1985'>
          <td><xsl:value-of select="/catalog/notold"></td>
    </xsl:if>
        </xsl>
      </tr>
      </xsl:for-each>
    </table>
  </body>
  </html>
</xsl:template>
</xsl:stylesheet>

我想给第一场比赛编号 1 和第二场比赛编号 2。但问题是,在每场比赛之后,它同时给出 1 和 2。我怎样才能让它在每场比赛中只使用一个号码。

谢谢。

输出:

<table border="1">
<tbody><tr bgcolor="#9acd32">
<th>Title</th>
<th>Artist</th>
</tr>
<tr>
<td>Empire Burlesque</td>
<td>Bob Dylan</td>
<td>1985</td>
<td>
1
2
</td>
</tr>
<tr>
<td>Hide your heart</td>
<td>Bonnie Tyler</td>
<td>1988</td>
</tr>
<tr>
<td>Greatest Hits</td>
<td>Dolly Parton</td>
<td>1985</td>
<td>
1
2
</td>
</tr>
</tbody></table>

编辑:

<?xml version="1.0" encoding="ISO-8859-1"?>
<cdash>

<builds>
 <build>
  <buildid>19389</buildid>
 </build>
 <build>
  <buildid>19390</buildid>
 </build>
</builds>

<etests>
<columnname>LoadTime</columnname>


<etest>
<name>LoadTime</name><buildid>19389</buildid><value>676</value>
</etest>
<columnname>Median</columnname>
<etest>
<name>Median</name><buildid>19389</buildid><value>868</value>
</etest>

<etest>
<name>LoadTime</name><buildid>19390</buildid><value>1777</value>
</etest>
<etest>
<name>Median</name><buildid>19390</buildid><value>1508</value>
</etest>

</etests


</cdash>

XML:

<?xml version="1.0" encoding="iso-8859-1"?>
<!-- Edited by XMLSpy® -->
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output indent="yes"/>
    <xsl:template match="/">
        <h2>Collection</h2>
        <table border="1">
            <tr bgcolor="#9acd32">
                <th>Title</th>
                <th>Artist</th>
            </tr>
            <xsl:for-each select="cdash/builds/build">
                <tr>
                    <td><xsl:value-of select="buildid"/></td>

                <td>
        <xsl:if test='buildid = /cdash/etests/etest/buildid'>
                            <xsl:variable name='index' 
                                          select='count(preceding-sibling::build[buildid = /cdash/etests/etest/buildid]) + 1' />
                            <xsl:value-of select="/cdash/etests/etest/value[position()=$index]" />
        </xsl:if>
                        </td>

                </tr>
            </xsl:for-each>
        </table>
    </xsl:template>
</xsl:stylesheet>
4

1 回答 1

1

我认为这就是您所追求的-假设您希望 Bob Dylan 拥有第一个notold/list(即“1”),Bonnie Tyler 没有数字,而 Dolly Parton 拥有第二个notold/list

它通过在匹配相同过滤器的所有 cd 列表中查找当前匹配 cd 的位置,通过计算preceding-sibling匹配列表中的 s,然后notold/list根据基于 1 的索引访问适当的项目来工作。

<?xml version="1.0" encoding="iso-8859-1"?>
<!-- Edited by XMLSpy® -->
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output indent="yes"/>
    <xsl:template match="/">
        <h2>My CD Collection</h2>
        <table border="1">
            <tr bgcolor="#9acd32">
                <th>Title</th>
                <th>Artist</th>
            </tr>
            <xsl:for-each select="catalog/cd">
                <tr>
                    <td><xsl:value-of select="title"/></td>
                    <td><xsl:value-of select="artist"/></td>
                    <td><xsl:value-of select="year"/></td>
                    <xsl:if test='year=1985'>
                        <td>
                            <xsl:variable name='index' 
                                          select='count(preceding-sibling::cd[year=1985]) + 1' />
                            <xsl:value-of select="/catalog/notold/list[position()=$index]" />
                        </td>
                    </xsl:if>
                </tr>
            </xsl:for-each>
        </table>
    </xsl:template>
</xsl:stylesheet>
于 2012-10-26T13:23:11.640 回答