Extract attributes using xslt

  • Thread starter Luis Angel Fdez. Fdez.
  • Start date
L

Luis Angel Fdez. Fdez.

Hi!

I have a simple .xml file only for testing purpouses...

<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
<?xml-stylesheet type="text/xsl" href="attr.xsl" ?>
<root>
<element>
<id>0</id>
<name>A cheap thing</name>
<prize currency='GBP'>0.5</prize>
<stock>15</stock>
</element>
<element>
<id>1</id>
<name>A one hundred euros cost thing</name>
<prize currency='EUR'>100</prize>
<stock>7</stock>
</element>
<element>
<id>2</id>
<name>The most expensive thing</name>
<prize currency='USD'>15000</prize>
<stock>2</stock>
</element>
</root>

What I want to do is to extract the value of the attribute currency. I
tried this:

<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/
Transform">
<xsl:template match="/">
<html>
<head>
</head>
<body>
<h2>Testing XML-XSL II</h2>
<table border="1">
<tr bgcolor="#9acd32">
<th align="center">Name</th>
<th align="center">Prize</th>
</tr>
<xsl:for-each select="root/element">
<tr>
<td align="center"><xsl:value-of select="name"/></td>
<td align="center"><xsl:value-of select="@currency"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>

But it doesn't work. It show nothing. I read on book 'XML in a
Nutshell' something about built-in templates and it seems <xsl:apply-
templates select="@currency"/> should work but it doesn't... even if I
include into the xsl:

<xsl:template match="text()|@*">
<xsl:value-of select="."/>
</xsl:template>

What am I doing wrong?

Bye.
 
D

Dimitre Novatchev

As shown in the provided xml document an "element" element does not have a
"currency" attribute. Its "prize" child has such an attribute.

Therefore, you need to modify the second "xsl:value-of" instruction
accordingly.

Cheers,
Dimitre Novatchev
 
M

Markus Schneider

Hello,

Currency is not a child of element. So try this:

xsl:value-of select="prize/@currency"/>
> <xsl:for-each select="root/element">
> <tr>
> <td align="center"><xsl:value-of select="name"/></td>
> <td align="center"><xsl:value-of select="@currency"/></td>
> </tr>
> </xsl:for-each>
 
D

Dimitre Novatchev

Currency is not a child of element. So try this:

Just to make it clear: "currency" is not a child of the "prize" element or
of any element.

"currency" is an attribute and attributes are not children (are not selected
with the "child::" axis), although the element they attribute is selected
by an expression such as:

parent::*


Cheers,
Dimitre Novatchev
 
L

Luis Angel Fdez. Fdez.

El Sun, 30 Sep 2007 17:59:37 +0200, Markus Schneider escribió:

Hi!
Currency is not a child of element. So try this:

Oh, that's true...
xsl:value-of select="prize/@currency"/>

That's work!. Anyway, I tried prize[@currency] as I saw somewhere but
it didn't work.

Bye and thanks both of you for your answers.
 
J

Joe Kesselman

Luis said:
That's work!. Anyway, I tried prize[@currency] as I saw somewhere but
it didn't work.

That finds prize elements which have a currency attribute.

You really do want to take the time to read a good XSLT tutorial rather
than guessing...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top