www
www

Reputation: 596

XSLT array like index

XML code:

<?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 code:

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

I want to give the first match number 1 and to the second match number 2. But the problem is, after every match, it gives 1 and 2 at the same time. How can I make it to use only one number for each match.

Thanks.

Output:

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

EDIT:

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

Upvotes: 0

Views: 3927

Answers (1)

StuartLC
StuartLC

Reputation: 107317

I think this is what you are after - assuming that you want Bob Dylan to have the first notold/list (i.e. '1'), Bonnie Tyler to have no number, and Dolly Parton to have the second notold/list.

It works by finding the current matching cd's position within the list of all cd's matching the same filter, by counting preceding-siblings in the match list, and then accessing the appropriate notold/list item based on the 1 based index.

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

Upvotes: 1

Related Questions