I have the following XML:
<system-folder>
<system-folder>
<system-page>
<display-name>One</display-name>
</system-page>
</system-folder>
<system-folder>
<system-page>
<display-name>Two</display-name>
</system-page>
</system-folder>
<system-folder current="true">
<system-page>
<display-name>Three</display-name>
</system-page>
<system-page>
<display-name>Four</display-name>
</system-page>
<system-page>
<display-name>Five</display-name>
</system-page>
</system-folder>
</system-folder>
And the following code works as expected:
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="/">
<xsl:for-each select="//system-folder[@current="true"]/..//system-page">
Current: <xsl:value-of select="display-name"/><br/>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
And returns what I expected:
Current: One
Current: Two
Current: Three
Current: Four
Current: Five
But when I try to get the previous items using:
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="/">
<xsl:for-each select="//system-folder[@current="true"]/..//system-page">
Previous: <xsl:value-of select="preceding-sibling::*[1]/display-name"/><br/>
Current: <xsl:value-of select="display-name"/><br/><br/>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
I get:
Previous:
Current: One
Previous: <-- Why is this missing?
Current: Two
Previous: <-- Why is this missing?
Current: Three
Previous: Three
Current: Four
Previous: Four
Current: Five
Why are the two items above missing? It looks to me that the preceding-sibling isn’t getting out of the folder. How can I resolve that?
A lot of thanks to whoever can help me – I’ve been stuck on it for too long 🙁
You seem to be confusing the
preceding::axis with thepreceding-sibling::axis.What you want is:
when this transformation is applied on the provided XML document:
the wanted, correct result is produced: