smr5
smr5

Reputation: 2793

XSLT get attribute value from XML

There's a section in my XML file that contains the following:

<Agent>
<GroupsList>
  <int name="55555(My City IIM)">0</int>
</GroupsList>
<Name>John Smith</Name>
</Agent>

How do I get the value "55555(My City IIM)" from the name attribute in the XSLT file?

I have tried this:

<xsl:for-each select="/Agent/GroupsList/int">
                <xsl:value-of select="name(.)"/> : <xsl:value-of select="."/>
                </xsl:for-each>

but it's returning int : 0

Any suggestions?

Upvotes: 0

Views: 135

Answers (1)

Fabian Mebus
Fabian Mebus

Reputation: 2415

To get the value of an attribute, prefix the name of the attribute with an @ and reference it in a <xsl:value-of select="@ATTRIBUTE_NAME" />.

In your code example it would be:

<xsl:for-each select="/Agent/GroupsList/int">
    <xsl:value-of select="@name"/> : <xsl:value-of select="."/>
</xsl:for-each>

So you get:

55555(My City IIM) : 0

Upvotes: 1

Related Questions