This is the mail archive of the
xsl-list@mulberrytech.com
mailing list .
Re: Matching braches without a certain node
- To: XSL-LIST <xsl-list at mulberrytech dot com>
- Subject: Re: Matching braches without a certain node
- From: Dimitre Novatchev <dnovatchev at yahoo dot com>
- Date: Wed, 29 Nov 2000 01:42:38 -0800 (PST)
- Reply-To: xsl-list at mulberrytech dot com
Hi Ted,
> <xsl:for-each select="//middle">
> <xsl:if test="not (foo[node()]">
> [Whatever I want to do]
> </xsl:if>
> </xsl:for-each>
The expression not (foo[node()] has unbalanced brackets and will
generate an error from the XSLT processor.
More importantly, after you add the missing closing ')', this will not
eliminate all "nodes that _don't_ have the 'foo' node" .
It will return a node-set of all child elements that do not have a
'foo' child with a node child.
So, in case you add another 'middle' element (id=4) at the end of your
xml document, like the following:
<top>
<middle id="1">
<data>value</data>
<foo>bar</foo>
</middle>
<middle id="2">
<data>value</data>
</middle>
<middle id="3">
<data>value</data>
<foo>bar</foo>
</middle>
<middle id="4">
<data>value</data>
<foo/>
</middle>
</top>
Then the expression //middle[not (foo[node()])]
returns a nodeset of two elements:
<middle id="2">
<data>value</data>
</middle>
and
<middle id="4">
<data>value</data>
<foo/>
</middle>
As you can see, the second node in the returned node-set still has a
'foo' child.
The expression that will return all 'middle' elements that do not have
a 'foo' child is:
//middle[not(foo)]
Dimitre Novatchev.
Theodore Weatherly wrote:
Suppose we have this XML:
<top>
<middle id="1">
<data>value</data>
<foo>bar</foo>
</middle>
<middle id="2">
<data>value</data>
</middle>
<middle id="3">
<data>value</data>
<foo>bar</foo>
</middle>
</top>
I want to match the 'middle' nodes that _don't_ have the 'foo' node
(id==2).
This seems to work:
<xsl:template match="/">
[stuff before]
<xsl:for-each select="//middle">
<xsl:if test="not (foo[node()]">
[Whatever I want to do]
</xsl:if>
</xsl:for-each>
</xsl:template>
(You get the general idea.) Isn't there any simpler way to do this??
I'd
rather do something where, in one line, I match the 'middle' nodes
without
the 'foo' tag, and then do an unconditional loop over these nodes.
Thanks!
-Ted
__________________________________________________
Do You Yahoo!?
Yahoo! Shopping - Thousands of Stores. Millions of Products.
http://shopping.yahoo.com/
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list