2014-01-20 73 views
1

我想使用XSLT樣式表來設置XML數據的樣式。在我的XML文件「V」和「H」中,我有兩個團隊訪問和回家。我想在兩個單獨的表格中顯示他們的統計數據。我只是不確定如何告訴XSLT,我只想要特定團隊的屬性。我想能夠說xsl:value-of select =「team」,其中vh attribute =「V」pull出ID,姓名,錄音等
XML的這些值:如何顯示特定節點的XSLT中的屬性?

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

編輯:

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

回答

1

你正在尋找中的XPath是/team[@vh="V]/team[@vh="H"]。 您可以在XPath中使用它

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

條件在方括號中給出。不幸的是,我不明白你想在輸出中使用它,否則我會試圖給你一個工作的例子。

一般來說,我會建議使用一個額外的模板,你想達到什麼:

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

這個模板是可重複使用的,然後,例如像這樣:

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

只需從bbgame模板的<body>標記刪除您的團隊表,並通過一個更換或多個應用模板調用。

+0

在XSLT的底部,我有xsl:value-of select =「team/@ id」。我需要在團隊之前放置一個? –

+0

/不需要。關鍵是[@vh =「...」]。 –

+0

好的。我道歉,我應該更具體。我想創建一個HTML表格,說明團隊vh = V,然後輸出Name,Record,Score的特定值。 –

相關問題