Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
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
 
Old January 3rd, 2007, 06:46 AM
Authorized User
 
Join Date: Sep 2006
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to nadavvin
Default How do I get and print node name?

If I have unknown node, how do I print its name?

 
Old January 3rd, 2007, 07:00 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Use the name() function.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old January 3rd, 2007, 07:42 AM
Authorized User
 
Join Date: Sep 2006
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to nadavvin
Default

But it is xpath function and I need to print the value not only get it.

 
Old January 3rd, 2007, 09:27 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

XSLT doesn't print anything, it produces a result tree. If you want to get the node name into the result tree, do

<xsl:value-of select="name()"/>



Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old January 3rd, 2007, 09:58 AM
Authorized User
 
Join Date: Sep 2006
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to nadavvin
Default

It try to:

Code:
$ cat test.xml
<?xml version="1.0" encoding="utf-8"?>
<root>
        <a>a</a>
</root>
[nadav@pcnadav b]$ cat test.xsl
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/">
<xsl:value-of select="name()"/>
</xsl:template>
</xsl:stylesheet>

[nadav@pcnadav b]$ xsltproc test.xsl test.xml
<?xml version="1.0"?>

[nadav@pcnadav b]$
Why didn't it work???

 
Old January 3rd, 2007, 10:06 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

It didn't work because "/" is the document node, and the document node doesn't have a name.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old January 3rd, 2007, 11:17 AM
Authorized User
 
Join Date: Sep 2006
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to nadavvin
Default

So how can I get the "root" node name?

 
Old January 3rd, 2007, 11:21 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

You can't, it doesn't have a name. The root element name can be shown with name(/*).

--

Joe (Microsoft MVP - XML)
 
Old January 3rd, 2007, 11:25 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

As I said, the root (the document node) does not have a name.

If you want the outermost element, you can address that as /* - so either call name() when this is the current node, or call name(/*) to select it explicitly.

(I think you need to do some reading to grasp the basic concepts - this is a very slow way to learn a language)

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old January 3rd, 2007, 12:31 PM
Authorized User
 
Join Date: Sep 2006
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to nadavvin
Default

Thanks, It's work!

Code:
$ xsltproc test.xsl test.xml
<?xml version="1.0"?>
root





Similar Threads
Thread Thread Starter Forum Replies Last Post
The reference node is not a child of this node.XSL XMLUser XSLT 2 February 25th, 2008 05:22 AM
how to append child node after an node in XML + C# vishnu108mishra XML 5 November 13th, 2007 05:30 AM
Missing Node (Node data) pashworth XSLT 3 January 29th, 2007 12:39 PM
Copying Source Node attributes to output node pvsat XSLT 2 November 3rd, 2005 09:46 AM
Print and print preview file on the website withou appleLover General .NET 0 February 19th, 2005 02:24 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.