xsl-list
[Top] [All Lists]

RE: name() of root element

2003-04-01 08:42:38
Hi Joseph,

Why does this not select the name of the root *element* but 
instead gives me the text value of that element, i.e., the 
concatenated text values of every element in my document?

    <xsl:template match="/" >
        <xsl:value-of select="(/node()[1])[name()]" />
        <xsl:apply-templates />
    </xsl:template>

This returns the value of the first node child of the root node (see below),
if it has a name (which, as an element, it does), which is its text content.
Remember that anything within [] is a predicate and is used simply to filter
the previous XPath step.

When I try select="/name()" (and variations thereof) I get 
error messages from the xsl processor.

The XPath "/" does not return the document element (the element in which all
others are contained), but the root node, which is an abstract higher-level
container which contains everything (including any comments, etc., which do
not fall within the document element). It has no name, it just is
(nominalist metaphysics notwithstanding). If you want the name of the
document element, you want the first (and only) element child of "/"; that
is <xsl:value-of select="name(/*)"/>.

The fact that at the start of an XPath "/" means the root node, whereas
anywhere else it is the step delimiter can be confusing.

Hope that helps,

Stuart

 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list



<Prev in Thread] Current Thread [Next in Thread>