Jonathan Britt
Jonathan Britt

Reputation: 53

How to display attributes in XSLT of a specific node?

I am trying to style XML data using an XSLT stylesheet. I have two teams in my XML file "V" and "H" for visiting and home. I want to display their stats in two separate tables. I'm just not sure how to tell the XSLT that I want only the attributes for the specific team.I'd like to be able to say xsl:value-of select="team" where the vh attribute = "V" pull out these values of ID, Name, Record, etc.
XML:

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="stylesheet.xsl"?> 
<bbgame source="STAT CREW Basketball" version="4.15.03" generated="12/17/2013">
<team vh="V" id="MSU" name="MISSOURI STATE" record="8-2">
    <linescore line="24,36" score="60">
      <lineprd prd="1" score="24"></lineprd>
      <lineprd prd="2" score="36"></lineprd>
    </linescore>
</team>
<team vh="H" id="LOU" name="LOUISVILLE" record="10-1">
    <linescore line="47,43" score="90">
      <lineprd prd="1" score="47"></lineprd>
      <lineprd prd="2" score="43"></lineprd>
    </linescore>
</team>
</bbgame>

XSL:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="bbgame">
<html>
<head>
    <link rel="stylesheet" type="text/css" href="stylesheet.css"/
</head>
<body>
    <h1>Official Basketball Box Score -- Game Totals</h1>
    <table>
        <tr>
            <td><xsl:value-of select="venue/@visname"/> vs. </td>
            <td><xsl:value-of select="venue/@homename"/></td>
            <td><xsl:value-of select="venue/@date"/></td>
            <td><xsl:value-of select="venue/@time"/></td>
            <td><xsl:value-of select="venue/@location"/></td>
        </tr>
    </table>
<table>
    <tr>
        <td>
            <xsl:value-of select="team/@id"/></td>
        <td>    <xsl:value-of select="team/linescore/@score"/></td>     
    </tr>   
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet> 

EDIT:

<table>
    <xsl:if test="team[@vh="V"]">
    <tr>
        <td>    <xsl:value-of select="team/@id"/></td>
        <td>    <xsl:value-of select="team/linescore/@score"/></td>     
    </tr>
    </xsl:if>   
</table>

Upvotes: 1

Views: 2828

Answers (1)

Thomas Weller
Thomas Weller

Reputation: 59289

The XPaths you're looking for are /team[@vh="V] and /team[@vh="H"]. You can use it in

<xsl:value-of select="team[@vh='H']/@id"/>

Conditions in XPath are given in square brackets. Unfortunately I didn't understand where you want to use it in the output, otherwise I would have tried to give you a working example.

In general I would advise to use an extra template for what you want to achieve:

<xsl:template match="team">
<table>
    <tr>
        <td>
            ID: <xsl:value-of select="@id"/>
       </td>
        <td>Score:    <xsl:value-of select="linescore/@score"/></td>     
        <td>Record:    <xsl:value-of select="@record"/></td>     
    </tr>   
</table>
</xsl:template>

This template is then reusable, e.g. like this:

<xsl:apply-templates select="team[@vh='H']"/>
<xsl:apply-templates select="team[@vh='V']"/>

Simply remove your team table from the <body> tag of the bbgame template and replace it by one or more apply-template calls.

Upvotes: 1

Related Questions