Reputation: 2947
Ok, I know this has been covered a couple of times, but previous answers don't seem to give the result I'm looking for. It could be that xslt is not processing as linearly as I expect it to, but I don't know how to address it if it's not.
Let's say I have an xml file:
<?xml version="1.0" encoding="UTF-8"?>
<root>
<section>A</section>
<divider/>
<divider/>
<section>B</section>
<divider/>
<section>C</section>
<divider/>
<divider/>
<section>D</section>
<divider/>
</root>
As you can see, there are occasionally duplicate elements. With an xslt:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="2.0">
<xsl:strip-space elements="*" />
<xsl:template match="/root">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="section">
<xsl:value-of select="text()"/>
</xsl:template>
<xsl:template match="divider">
<xsl:if test="not(preceding-sibling::divider)">
<xsl:text>--</xsl:text>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
I was expecting to see
A
--
B
--
C
--
D
--
... but that's not what's happening. Can anyone tell me what I'm missing?
What I'm getting right now is:
A
--
B
C
D
Upvotes: 0
Views: 77
Reputation: 167471
preceding-sibling::divider
selects any preceding sibling divider
element. If you want to check that the immediate preceding sibling is not a divider
then use not(preceding-sibling::*[1][self::divider])
.
Upvotes: 1