Extract attributes using xslt

Luis Angel Fdez. Fdez.

Usenet Archive
Joined
Jul 19, 2007
Messages
8
Reaction score
0
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.
 

Dimitre Novatchev

Usenet Archive
Joined
Nov 7, 2000
Messages
737
Reaction score
0
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
 

Markus Schneider

Usenet Archive
Joined
Sep 30, 2007
Messages
1
Reaction score
0
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>
 

Dimitre Novatchev

Usenet Archive
Joined
Nov 7, 2000
Messages
737
Reaction score
0
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
 

Joe Kesselman

Usenet Archive
Joined
Feb 3, 2006
Messages
1,229
Reaction score
0
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
548,050
Messages
3,104,249
Members
248,133
Latest member
DujonT

Latest Threads

Top