Re: [xsl] how to test previous node name

Subject: Re: [xsl] how to test previous node name
From: "Andrew Welch" <andrew.j.welch@xxxxxxxxx>
Date: Mon, 12 Feb 2007 09:40:08 +0000
On 2/12/07, xslt. new <xslt.new@xxxxxxxxx> wrote:
Thank you for your response. I did try this inside template <item> but
it fails to get me the right result.

This is what I tried:

<xsl:template match="item">
<xsl:if test="preceding::*[1][self::test1]">

<xsl:text>Found test1</xsl:text>
</xsl:if>

</xsl:template>

It is not working.

I thought I was answering what you were asking for:


How do I check if the previous node name for <item> is test1 or text?

...and not what your example suggested:


<test1>
<item></item>
<text></text>
<item></item>
</test1>

The preceding axis gets all nodes before the context node in document order, minus any ancestors:

"the preceding axis contains all nodes that are descendants of the
root of the tree in which the context node is found, are not ancestors
of the context node, and occur before the context node in document
order"

If your example was:

<root>
 <test1/>
 <test1>
   <item></item>
   <text></text>
   <item></item>
 </test1>
</root>

or

<root>
 <foo><test1/></foo>
 <test1>
   <item></item>
   <text></text>
   <item></item>
</test1>
</root>

The reply I gave would give the results you expect - it all depends on
how you define the "previous node" for that first <item> child of
<test1>.

If you define the previous node to mean the parent node, then just add
a test using the parent axis:

select="parent::test1 or preceding::*[1][self::test1]"

(It could well be that you don't want to check the whole preceding
axis, just the preceding-sibling axis - its worth figuring that out
now based on the examples I've given.)


cheers andrew

Current Thread