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

时间:2015-02-23 22:48:34

标签: xml xslt

我编写了以下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>

我正在尝试显示游戏的ID,在这种情况下是数字1.我怎么能这样做?我写了以下XSL代码:

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

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

谢谢

1 个答案:

答案 0 :(得分:2)

只需

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

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