Re: [xsl] how to test previous node name

Subject: Re: [xsl] how to test previous node name
From: Kamal Bhatt <kbhatt@xxxxxxxxx>
Date: Mon, 12 Feb 2007 08:40:33 +1100
Andrew Welch wrote:
On 2/11/07, xslt. new <xslt.new@xxxxxxxxx> wrote:
Hi all:

I have an XML input:

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

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


select="preceding::*[1][self::item1 or self::text]"

cheers
andrew


OK, Andrew and I have different interpretations of what you said, further complicated by the fact that in your example, test1 is never preceding an item (it is the parent of the item). Here is what I had, based on what you requested then by what I assume you actually want.

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>

<xsl:template match="test1">
<xsl:apply-templates select="item[preceding::*[1][local-name() = 'text']" mode="text"/>
<xsl:apply-templates select="item[preceding::*[1][local-name() = 'test1']" mode="test1"/>
</xsl:template>


<xsl:template match="item" mode="text">
 ...processing for text...
</xsl:template>

<xsl:template match="item" mode="test1">
 ...processing for text...
</xsl:template>

</xsl:stylesheet>

You could use parent::* in a similar way, if that is actually what you want.

Cheers.

--
Kamal Bhatt

Current Thread