在XSL中读取和显示XML id属性

在XSL中读取和显示XML id属性,xml,xslt,Xml,Xslt,我编写了以下XML代码: <game id="1"> <title>Call of duty</title> <release> <year>2007</year> <month>06</month> <day>27</day> </release> <publisher>Infini

我编写了以下XML代码:

<game id="1">
    <title>Call of duty</title>
    <release>
      <year>2007</year>
      <month>06</month>
      <day>27</day>
     </release>
    <publisher>Infinity</publisher>
    <engine>Source</engine>
    <platforms>
      <platform>Windows</platform>
      <platform>Xbox</platform>
      <platform>wii</platform>
    </platforms>
 </game>

使命召唤
2007
06
27
无穷
来源
窗户
Xbox
wii
我试图显示游戏的id,在本例中是1号。我怎样才能做到呢?我编写了以下XSL代码:

<xsl:apply-templates select="game"/>

<xsl:template match="game">
  Game: <xsl:value-of select="//game/@id"/>
  <br />
</xsl:template>

游戏:

谢谢你

简单地说

<xsl:template match="game">
  <xsl:text>Game: </xsl:text>
  <xsl:value-of select="@id"/>
  <br/>
</xsl:template>

游戏:


当您匹配游戏时,您已经在节点中,因此无需在路径中再次指定它。

您的XSLT代码不起作用或不完整(或两者都不完整)
xsl:apply-templates
必须位于
xsl:template
内。您可以使用
xsl:text
以更可控的方式输出文本。