 |
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|

March 8th, 2005, 03:46 AM
|
Registered User
|
|
Join Date: Mar 2005
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
how to get xml tag names in xslt
I want to get xml tag names in xslt file. i tried to use name() in value-of but it is giving me wrong result (mag mag), which is the repeatition of the select in for-each. i need result same as "first,second,first,second". my concern is to get the tag names of child elements of tag supplied in for-each's select. it is really urgent.
xml file:-
<mags>
<mag> <first>1</first><second>2</second></mag>
<mag> <first>99</first><second>88</second></mag>
</mags>
xslt logic:-
<xsl:template match="/">
<xsl:for-each select="/mags/mag">
<xsl:value-of select="name()"/>
</xsl:for-each>
</xsl:template>
|

March 8th, 2005, 04:42 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
|
|
Something like:
Code:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:template match="/mags">
<xsl:apply-templates select="mag/*"/>
</xsl:template>
<xsl:template match="mag/*">
<xsl:value-of select="position()"/><xsl:text>: </xsl:text><xsl:value-of select="name()"/><xsl:text>#xa;</xsl:text>
</xsl:template>
</xsl:stylesheet>
--
Joe ( Microsoft MVP - XML)
|

March 8th, 2005, 09:03 AM
|
Registered User
|
|
Join Date: Mar 2005
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
First of all thanks for your support i implemented your idea and am getting the following error.
"Top level elements should have non-null namespace, html."
i want to create a table with the tag names. my code is below.
<xsl:transform version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" omit-xml-declaration="yes" />
<html>
<body>
<xsl:template match="/mags">
<xsl:apply-templates select="mag/*"/>
</xsl:template>
<table width="80%" border="1">
<tr bgcolor="lightgrey">
<xsl:template match="mag/*">
<td>
<xsl:value-of select="name()"/>
</td>
</xsl:template>
</tr>
</table>
</body>
</html>
</xsl:transform>
|

March 8th, 2005, 10:53 AM
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 1,212
Thanks: 0
Thanked 1 Time in 1 Post
|
|
it's because your opening <html> etc isn't inside any xsl template - just put <xsl:template match="/mags"> before it (and remember to close the template after the closing </html>
|

March 8th, 2005, 10:54 AM
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 1,212
Thanks: 0
Thanked 1 Time in 1 Post
|
|
sorry, bad cut and paste. I meant to say
just put <xsl:template match="/"> before it...
|

March 9th, 2005, 02:36 AM
|
Registered User
|
|
Join Date: Mar 2005
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
i just added the <xsl:template match="/"> tag before <html> and closed it after </html>. i am getting the following error.
(This is an unexpected token. the expected token is "NAME".)
|

March 9th, 2005, 03:01 AM
|
Registered User
|
|
Join Date: Mar 2005
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
sorry about my last reply i am getting the following error not the one i wrote in my last reply.
"'xsl:template' cannot be a child of 'body; element."
|

March 9th, 2005, 06:11 AM
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 1,212
Thanks: 0
Thanked 1 Time in 1 Post
|
|
Apologies, I didn't really look at your stylesheet past noticing that you didn't have a top-level template. I think you need to do a bit more reading about the basics, especially the relationship between xsl:template and xsl:aaply-templates.
Anyway, is this what you want?
<xsl:transform version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html"/>
<xsl:template match="/">
<html>
<body>
<table width="80%" border="1">
<xsl:apply-templates/>
</table>
</body>
</html>
</xsl:template>
<xsl:template match="mag">
<tr bgcolor="lightgrey">
<xsl:apply-templates/>
</tr>
</xsl:template>
<xsl:template match="mag/*">
<td>
<xsl:value-of select="name()"/>
</td>
</xsl:template>
</xsl:transform>
|

March 14th, 2005, 05:55 AM
|
Registered User
|
|
Join Date: Mar 2005
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
instead of all the tag names of mags/mag. Is it possible to get the child tag names of first "mag"?
|
|
 |