Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there an XSLT name-of element?

Tags:

xml

xslt

xpath

In XSLT there is the

<xsl:value-of select="expression"/> 

to get the value of an element, but is there something to select the tag-name of the element?

In a situation like this:

<person>   <!-- required stuff -->   <name>Robert</name>   <!-- optional stuff, free form for future extension.         Using XMLSchema's xsd:any -->   <profession>programmer</profession>   <hobby>photography</hobby> </person>  <xsl:for-each select="person">    <xsl:tag-of select="."/> : <xsl:value-of select="."/> </xsl:for-each> 

To get output like this:

name : Robert profession : programmer hobby : photography 

Of course the above XSLT won't compile because

 <xsl:tag-of select="expression"/> 

doesn't exist. But how could this be done?

like image 752
Robert Gould Avatar asked Feb 25 '09 09:02

Robert Gould


People also ask

What is local name in XSLT?

XSLT/XPath Reference: XSLT elements, EXSLT functions, XPath functions, XPath axes. The local-name function returns a string representing the local name of the first node in a given node-set.

Which symbol is used to get the element value in XSLT?

XSLT <xsl:value-of> Element The <xsl:value-of> element is used to extract the value of a selected node.

What is the difference between XPath and XSLT?

XPath. At bottom, XSLT is a language that lets you specify what sorts of things to do when a particular element is encountered. But to write a program for different parts of an XML data structure, you need to specify the part of the structure you are talking about at any given time. XPath is that specification language ...


2 Answers

This will give you the current element name (tag name)

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

OP-Edit: This will also do the trick:

<xsl:value-of select ="local-name()"/> 
like image 117
SO User Avatar answered Oct 21 '22 05:10

SO User


Nobody did point the subtle difference in the semantics of the functions name() and local-name().

  • name(someNode) returns the full name of the node, and that includes the prefix and colon in case the node is an element or an attribute.
  • local-name(someNode) returns only the local name of the node, and that doesn't include the prefix and colon in case the node is an element or an attribute.

Therefore, in situations where a name may belong to two different namespaces, one must use the name() function in order for these names to be still distinguished.

And, BTW, it is possible to specify both functions without any argument:

name() is an abbreviation for name(.)

local-name() is an abbreviation for local-name(.)

Finally, do remember that not only elements and attributes have names, these two functions can also be used on PIs and on these they are identical).

like image 43
Dimitre Novatchev Avatar answered Oct 21 '22 05:10

Dimitre Novatchev