views:

25

answers:

1

How to retrive the id attribute of the root node of a xml using XSL?

+2  A: 

How to retrive the id attribute of the root node of a xml using XSL?

You mean ... of the top element. The root node is not an element and cannot have attributes.

This simple XPath expression selects the id attribute of the top element of any XML document:

/*/@id

In XSLT, one will use:

<xsl:variable name="vsomeName" select="/*/@id"/>

or

<xsl:copy-of select="/*/@id"/>

or

<xsl:value-of select="/*/@id"/>
Dimitre Novatchev
+1. Note that in the XML spec, the term for the top element is "root or document element", and in XDM it appears to be "document element". However XSLT 2.0 spec editor Michael Kay prefers "outermost element" as it is less easily confused with its parent, the "document node".
LarsH
@LarsH: I always use the term "top element" to make a clear distinction from the *root* node (the document node) as described in the W3C XPath 1.0 spec.: http://www.w3.org/TR/1999/REC-xpath-19991116/#root-node
Dimitre Novatchev
@Dimitre: I understand, that makes sense. I wanted to let @user know that he/she would encounter different terms in the official specs, and why some serious XMLites don't use the official terms.
LarsH