XSLT数组就像索引一样

时间:2012-10-26 12:32:21

标签: arrays xslt indexing

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>

1 个答案:

答案 0 :(得分:1)

我认为这就是你所追求的 - 假设你希望Bob Dylan拥有第一个notold/list(即&#39; 1&#39;),Bonnie Tyler没有数字,以及Dolly Parton有第二个notold/list

通过计算匹配相同过滤器的所有cd的列表中当前匹配的cd位置,通过计算匹配列表中的preceding-sibling,然后访问相应的{ {1}}基于1基索引的项目。

notold/list