user1177969
user1177969

Reputation: 105

Preceding sibling in XSLT

I am trying to put a specific condition on the basis of preceding sibling check. I tried various options but nothing worked. Here is the sample XML

<abc>
 <title>something</title>
 <element>1</element>
 <element>2</element>
 <element>3</element>
 <element>4</element>    
</abc>

I have a template match for <element> tag and I want to check if its immediate element is <title> Do some additioanl processing else do some other processing. Any pointers appriciated.

Upvotes: 0

Views: 2107

Answers (3)

Nils Werner
Nils Werner

Reputation: 36859

<xsl:template match="element">
    I am boring. <xsl:value-of select="." />
</xsl:template>

<xsl:template match="element[preceding-sibling::*[1][self::title]]">
    I am special. <xsl:value-of select="." />
</xsl:template>

Upvotes: 4

Ian Roberts
Ian Roberts

Reputation: 122424

If the current context is an element element then you can get its nearest preceding sibling element (regardless of name) using

preceding-sibling::*[1]

and so to check whether that element is a title you can use

preceding-sibling::*[1][self::title]

Upvotes: 1

shapiy
shapiy

Reputation: 1155

<xsl:template match="element[preceding-sibling::element()[1][self::title]]"/>

Upvotes: 0

Related Questions