[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

Re: [xsl] Is this node, the document root... best test?


Subject: Re: [xsl] Is this node, the document root... best test?
From: Stephen Cunliffe <scunliffe@xxxxxxxxxxxxxxxxxx>
Date: Mon, 20 Oct 2003 14:10:07 -0400

Thanks Michael and Wendell, etc.,

Okay, so I think i'm getting this now....

So, for any given XML document, (e.g. the fragment below),

I want to test, when i'm in the ((match"*" template, mode="xyz"))
If, I've "drilled" my way back up to the "document element node"???

Do I have the correct test below? or do I need the:
".. and not(parent::*)"
or
"parent::node()"



<?xml version="1.0"?>
<foo>
 <bar>
   <world>technology</world>
 </bar>
 <baz>
   <hello>bonjour</hello>
 </baz>
</foo>

In my XSL, I have (I've stripped out/simplified the irrelivant stuff):

<xsl:template match="/">
 ...
 <xsl:apply-templates select="./*" mode="abc"/>
 ...
</xsl:template>

<xsl:template match="*" mode="abc">
 ...
 <xsl:apply-templates select="./*" mode="abc"/>
 ...
</xsl:template>

<xsl:template match="*" mode="xyz">
 ...
 <!--
   check if this node, is (in the above case "foo")...
   which if I'm understanding correctly, is the "document element node"???
   which test, should I use?
 -->
 <xsl:choose>
   <xsl:when test="not(parent::*)">
     <!--
       break, do whatever (I need to know when this
       condition occurs, regardless, of what the name
       of my tag "foo" is called.
     -->
     So, for my sample, printing
     [<xsl:value-of select="name(.)"/>]
     will print: [foo]
   </xsl:when>
   <xsl:otherwise>
     <!-- continue, process this node's parent -->
     <xsl:apply-templates select="parent::*" mode="xyz"/>
   </xsl:otherwise>
 </xsl:choose>
 ...
</xsl:template>

Cheers,

Steve




On 10/20/2003 12:58 PM, Michael Kay wrote::


In a template, matching on "*", I need to test, if this node, is the document root node.



Start by getting your terminology right! By reverse engineering your
code, I think you are trying to test for the "outermost element": that
is, the element whose parent is a root node, in XPath 1.0 terminology.


<xsl:when test="not(parent::*)">
This works, but is it safe/efficient?



This is fine, except that it will match both the root node and the outermost element node. If you want to match the outermost element without matching the root, use

test=".. and not(parent::*)"



</xsl:when>
<!-- Method (2) -->
<xsl:when test="string(name(parent::*)) = ''">
This works, but is it safe/efficient?



This is clumsy. If parent::* selects anything, then it will select an element, and name() applied to an element is always a non-empty string, and string() applied to a non-empty string always gives you a non-empty string, so the test is equivalent to test="not(parent::*)".

Michael Kay


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







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



Current Thread
Keywords