christo360
christo360

Reputation: 27

Reading and displaying XML id attribute in XSL

I have written the following XML code:

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

I'm trying to display the game's id which in this case is number 1. How would I be able to do so? I have written the following XSL code:

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

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

Thank you

Upvotes: 2

Views: 400

Answers (1)

Jean-Fran&#231;ois Savard
Jean-Fran&#231;ois Savard

Reputation: 21004

Simply with

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

When you match game you are already in the node so there is no need to specify it again in the path.

Upvotes: 2

Related Questions