Retrieve all the attribute values from XML using XSLT

Erik Åstrand picture Erik Åstrand · May 15, 2013 · Viewed 24.5k times · Source

I can't figure out how to access all the attributes in a tag from an XML document.

Let's say I have the following XML:

<names>
  <name firstname="Rocky" lastname="Balboa" divider=", "/>
  <name firstname="Ivan" lastname="Drago" divider=", "/>
</names>

I want the following output: Rocky Balboa, Ivan Drago,

What I currently have is:

<xsl:for-each select="names/name">
   <xsl:value-of select="@firstname"/>
   <xsl:value-of select="@lastname"/>
   <xsl:value-of select="@divider"/>
</xsl:for-each>

What I'm wondering is if it's possible to do this in just one value-of select instead of having to do three of them. So to clarify, I want to be able to output all the attributes in the tag with one single value-of select. Is this possible?

Thanks.

Answer

james31rock picture james31rock · May 15, 2013

try the following:

<xsl:template match="/">
 <xsl:for-each select="names/name/@*">
        <xsl:value-of select="concat( ., ' ')"/>
  </xsl:for-each>
</xsl:template>