Re: [xsl] node() as pattern

Subject: Re: [xsl] node() as pattern
From: Jeni Tennison <jeni@xxxxxxxxxxxxxxxx>
Date: Wed, 12 Jun 2002 15:08:50 +0100
Hi Dave,

> Anyone got a nice clear description of what's happening
> with such as 
>
> match='/node()[2]/node()[8]/node()[2]/node()[8]/node()[7]'
>
> please?

This matches the 7th child node of the 8th child node of the 2nd child
node of the 8th child node of the 2nd child node of the root node. So
it'll only match anything if the root node has at least two children,
the second of which has at least 8 children, the 8th of which has at
least 2 children, the 2nd of which has at least 8 children, the 8th of
which has at least 7 children.

Assuming that you're dealing with a well-formed XML document,
containing only elements (no processing instructions, comments or
text), it won't match anything since the root node doesn't have a
second child.

Your other example was:

<doc>
  <nd1>
    <nd2>..</nd2>
    <nd3/>
  </nd1>
</doc>

(well, I assume that's what you meant -- you had an extra closing tag
for the nd1 element that didn't fit) with:

<xsl:template match='/node()[1]/node()[2]'>
  ...
</xsl:template>

This matches the second child node of the first child node of the root
node. The first child node of the root node is the doc element, but
that only has one child node (the nd1 element), so again, the template
doesn't match any nodes.

Perhaps it would help you to imagine the nodes numbered by their
position within their parent as follows:

<doc node="1">
  <nd1 node="1">
    <nd2 node="1">..</nd2>
    <nd3 node="2" />
  </nd1>
</doc>

So templates that would match these nodes would be:

  /node()[1]                       (matches doc)
  /node()[1]/node()[1]             (matches nd1)
  /node()[1]/node()[1]/node()[1]   (matches nd2)
  /node()[1]/node()[1]/node()[2]   (matches nd3)

Given that you don't have any processing instructions, comments or
text nodes, using node() is exactly the same as using *.

Cheers,

Jeni

---
Jeni Tennison
http://www.jenitennison.com/


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


Current Thread