0

I'm trying to create more than one table in my document. This is the current XSLT code im using:

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

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
  <html>
  <body>
    <h2 align="center">UFC Fighter Roster</h2>
<table border="0" align="left" width="20%" >
  <th>Heavyweight</th>
      <tr bgcolor="#9acd32">
        <th>Name</th>
        <th>Rank</th>
      </tr>
    <xsl:for-each select="roster/cd">
    <xsl:if test="weight=180">
      <tr>
        <td><xsl:value-of select="title"/></td>
        <td><xsl:value-of select="rank"/></td>
      </tr>
</table>


    </xsl:if>
    </xsl:for-each>
  </body>
  </html>
</xsl:template>
</xsl:stylesheet>

I'm using this table to pull fighters at weight=180, if I wanted to produce a table on the same page that represents the fighters whose weight=170 how do I go about it because everytime i try using the same code but a different weight figure it doesnt work?

Here is a version of my XML that I'm using.

<?xml version="1.0" encoding="UTF-8"?>

<roster>
    <cd>
        <title>Cain Velasquez</title>
        <rank>1st</rank>
        <weight>180</weight>
    </cd>
    <cd>
        <title>Fabricio Werdum</title>
        <rank>2nd</rank>
        <weight>180</weight>

    </cd>
    <cd>
        <title>Junior Dos Santos</title>
        <rank>3rd</rank>
        <weight>180</weight>
    </cd>
    <cd>
        <title>Travis Browne</title>
        <rank>4th</rank>
        <weight>180</weight>

    </cd>
    <cd>
        <title>Antonio Silva</title>
        <rank>5th</rank>
        <weight>180</weight>

    </cd>
    <cd>
        <title>Jon Jones</title>
        <rank>1st</rank>
        <weight>170</weight>
    </cd>
    <cd>
        <title>Alexander Guftasson</title>
        <rank>2nd</rank>
        <weight>170</weight>

    </cd>
    <cd>
        <title>Rashad Evans</title>
        <rank>3rd</rank>
        <weight>170</weight>

    </cd>
    <cd>
        <title>Glover Teixiera</title>
        <rank>4th</rank>
        <weight>170</weight>
    </cd>
    <cd>
        <title>Shogun Rua</title>
        <rank>5th</rank>
        <weight>170</weight>
    </cd>

</catalog>
2
  • The value you use depends on the contents of a <weight> element in your source document. Please edit your question and add your XML source. Also, fix your XSL because it's not well formed (there are two closing </table> tags, an extra closing angle bracket, at least). May 14, 2014 at 20:15
  • I've added the XML source and fixed the XSL @helderdarocha May 14, 2014 at 20:30

3 Answers 3

1

I'm using this table to pull fighters at weight=180, if I wanted to produce a table on the same page that represents the fighters whose weight=170 how do I go about it

You should use a key instead of trying to do this with xsl:if.


Edit

For example:

XSLT 1.0

xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

<xsl:key name="cd-by-weight" match="cd" use="weight" />

<xsl:template match="/">
    <html>
        <table>
            <tr>
                <th>Heavyweight</th>
            </tr>
            <tr>
                <th>Name</th>
                <th>Rank</th>
            </tr>
            <xsl:for-each select="key('cd-by-weight', 180)">
                <tr>
                    <td><xsl:value-of select="title"/></td>
                    <td><xsl:value-of select="rank"/></td>
                </tr>
            </xsl:for-each>
        </table>
        <br/>
        <table>
            <tr>
                <th>Less Heavyweight</th>
            </tr>
            <tr>
                <th>Name</th>
                <th>Rank</th>
            </tr>
            <xsl:for-each select="key('cd-by-weight', 170)">
                <tr>
                    <td><xsl:value-of select="title"/></td>
                    <td><xsl:value-of select="rank"/></td>
                </tr>
            </xsl:for-each>
        </table>
    </html>
</xsl:template>

</xsl:stylesheet>

This is assuming the source document always contains fighters of both weights. Note that the duplication of code could be eliminated by calling a named template with the weight as a parameter.

0

Check XSLT 1.0 Group By

In XSL 2.0

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

<xsl:stylesheet version="2.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="/">
        <html>
            <body>
                <h2 align="center">UFC Fighter Roster</h2>

                <xsl:for-each-group select="roster/cd" group-by="weight">
                   <table border="0" align="left" width="20%" >
                        <th>Heavyweight</th>
                        <tr bgcolor="#9acd32">
                            <th>Name</th>
                            <th>Rank</th>
                        </tr>
                       <xsl:for-each select="current-group()">
                        <tr>
                                    <td><xsl:value-of select="title/node()"/></td>
                                    <td><xsl:value-of select="rank/node()"/></td>
                         </tr>
                          </xsl:for-each>
                    </table>  
                </xsl:for-each-group>


            </body>
        </html>
    </xsl:template>
</xsl:stylesheet>
0

if you want it to be modular, you can do it as follow:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsl:output method="html" encoding="UTF-8" indent="yes"/>

<xsl:template match="/">
    <html>
        <body>
            <h2 align="center">UFC Fighter Roster</h2>
            <xsl:call-template name="table">
                <xsl:with-param name="title">Heavyweight</xsl:with-param>
                <xsl:with-param name="w" select="180"/>
            </xsl:call-template>
            <xsl:call-template name="table">
                <xsl:with-param name="title">weight (170)</xsl:with-param>
                <xsl:with-param name="w" select="170"/>
            </xsl:call-template>
        </body>
    </html>
</xsl:template>

<xsl:template name="table">
    <xsl:param name="w"/>
    <xsl:param name="title"/>
    <table border="0" align="left" width="20%" >
        <th><xsl:value-of select="$title"/></th>
        <tr bgcolor="#9acd32">
            <th>Name</th>
            <th>Rank</th>
        </tr>
        <xsl:apply-templates select="/catalog/cd[weight=$w]"/>
    </table>
</xsl:template>

<xsl:template match="cd">
    <tr>
        <td><xsl:value-of select="title"/></td>
        <td><xsl:value-of select="rank"/></td>
    </tr>
</xsl:template>
</xsl:stylesheet>

For XSLT2.0 grouping is a good alternative, and if you want specific value then use current-group-key():

<xsl:for-each select="current-group()[current-grouping-key() = ('170','180')]">
  <tr>
    <td><xsl:value-of select="title/node()"/></td>
    <td><xsl:value-of select="rank/node()"/></td>
  </tr>
</xsl:for-each>

Hope it will help.
regards,
yann

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.