p2p.wrox.com Forums

Need to download code?

View our list of code downloads.


Go Back   p2p.wrox.com Forums > XML > XSLT
I forgot my password Register Now
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 p2p Programmer to Programmer discussion community. This is a community of more than 40,000 computer programmers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining our free Wrox p2p community you can post your own programming questions and respond to other programmers’ questions. Registered users also don't have to see the ads that are displayed to guests. Registration is fast, simple and absolutely free so please, join today!
Join today and post to win prizes! Post more to increase your chances of being Wrox’s top poster of the month.

Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old March 8th, 2005, 03:46 AM
Registered User
Points: 17, Level: 1
Points: 17, Level: 1 Points: 17, Level: 1 Points: 17, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Mar 2005
Location: , , .
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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>

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
  #2 (permalink)  
Old March 8th, 2005, 04:42 AM
joefawcett's Avatar
Wrox Author
Points: 8,994, Level: 40
Points: 8,994, Level: 40 Points: 8,994, Level: 40 Points: 8,994, Level: 40
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Exeter, , United Kingdom.
Posts: 2,922
Thanks: 0
Thanked 13 Times in 12 Posts
Default

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)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
  #3 (permalink)  
Old March 8th, 2005, 09:03 AM
Registered User
Points: 17, Level: 1
Points: 17, Level: 1 Points: 17, Level: 1 Points: 17, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Mar 2005
Location: , , .
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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>


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
  #4 (permalink)  
Old March 8th, 2005, 10:53 AM
Friend of Wrox
Points: 2,450, Level: 20
Points: 2,450, Level: 20 Points: 2,450, Level: 20 Points: 2,450, Level: 20
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: , , United Kingdom.
Posts: 1,212
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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>
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
  #5 (permalink)  
Old March 8th, 2005, 10:54 AM
Friend of Wrox
Points: 2,450, Level: 20
Points: 2,450, Level: 20 Points: 2,450, Level: 20 Points: 2,450, Level: 20
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: , , United Kingdom.
Posts: 1,212
Thanks: 0
Thanked 0 Times in 0 Posts
Default

sorry, bad cut and paste. I meant to say
just put <xsl:template match="/"> before it...
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
  #6 (permalink)  
Old March 9th, 2005, 02:36 AM
Registered User
Points: 17, Level: 1
Points: 17, Level: 1 Points: 17, Level: 1 Points: 17, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Mar 2005
Location: , , .
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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".)

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
  #7 (permalink)  
Old March 9th, 2005, 03:01 AM
Registered User
Points: 17, Level: 1
Points: 17, Level: 1 Points: 17, Level: 1 Points: 17, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Mar 2005
Location: , , .
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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."


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
  #8 (permalink)  
Old March 9th, 2005, 06:11 AM
Friend of Wrox
Points: 2,450, Level: 20
Points: 2,450, Level: 20 Points: 2,450, Level: 20 Points: 2,450, Level: 20
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: , , United Kingdom.
Posts: 1,212
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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>
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
  #9 (permalink)  
Old March 14th, 2005, 05:55 AM
Registered User
Points: 17, Level: 1
Points: 17, Level: 1 Points: 17, Level: 1 Points: 17, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Mar 2005
Location: , , .
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

instead of all the tag names of mags/mag. Is it possible to get the child tag names of first "mag"?

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
class names in xslt not working chobo XSLT 2 January 13th, 2008 02:43 AM
How do I transform XSLT without the <?xml?> tag? nadavvin XSLT 4 June 10th, 2007 10:25 AM
XSLT For Each - Field Names lahatfield XSLT 6 May 15th, 2007 07:33 AM
How to force XSLT to output a close tag in XML? richcon XSLT 2 June 29th, 2006 03:39 AM
XML to (different <tag>) XML mapping Thodoris XML 4 April 27th, 2004 05:02 AM



All times are GMT -4. The time now is 03:17 AM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
© 2008 Wiley Publishing, Inc