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

January 3rd, 2007, 06:46 AM
|
|
Authorized User
|
|
Join Date: Sep 2006
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
How do I get and print node name?
If I have unknown node, how do I print its name?
|
|

January 3rd, 2007, 07:00 AM
|
 |
Wrox Author
|
|
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
|
|
Use the name() function.
Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
|
|

January 3rd, 2007, 07:42 AM
|
|
Authorized User
|
|
Join Date: Sep 2006
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
But it is xpath function and I need to print the value not only get it.
|
|

January 3rd, 2007, 09:27 AM
|
 |
Wrox Author
|
|
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
|
|
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
|
|

January 3rd, 2007, 09:58 AM
|
|
Authorized User
|
|
Join Date: Sep 2006
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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???
|
|

January 3rd, 2007, 10:06 AM
|
 |
Wrox Author
|
|
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
|
|
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
|
|

January 3rd, 2007, 11:17 AM
|
|
Authorized User
|
|
Join Date: Sep 2006
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
So how can I get the "root" node name?
|
|

January 3rd, 2007, 11:21 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
|
|
You can't, it doesn't have a name. The root element name can be shown with name(/*).
--
Joe ( Microsoft MVP - XML)
|
|

January 3rd, 2007, 11:25 AM
|
 |
Wrox Author
|
|
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
|
|
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
|
|

January 3rd, 2007, 12:31 PM
|
|
Authorized User
|
|
Join Date: Sep 2006
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Thanks, It's work!
Code:
$ xsltproc test.xsl test.xml
<?xml version="1.0"?>
root
|
|
 |