Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 521
  • Last Modified:

Transform XML using XSL, write element with a specific attribute value

Real beginner question. I have this XML file.

<?xml version="1.0" encoding="utf-8"?>
<results>
<result id="1">
<attribute name="name"><![CDATA[The Name]]></attribute>
<attribute name="translation"><![CDATA[The Translation]]></attribute>
</result>
</results>

It's not my XML and I can't do anything about the structure

The following XSL outputs "The Name"

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
      <xsl:for-each select="/results/result">
              <xsl:value-of select="attribute" />
      </xsl:for-each>
</xsl:template>
</xsl:stylesheet>

Instead, I want to output the value of the 2nd item named attribute - ie my output should be "The Translation"
0
Garve
Asked:
Garve
  • 2
1 Solution
 
Geert BormansCommented:
           <xsl:value-of select="attribute[@name = 'translation']" />
if you need the selection to be based on the value of the attribute

or
           <xsl:value-of select="attribute[2]" />
if you simply need the second
0
 
GarveAuthor Commented:
Brilliant! Thanks very much.
0
 
Geert BormansCommented:
welcome
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now