XSL Problem: compare with previous node name
Added by Anonymous over 19 years ago
Legacy ID: #3213876 Legacy Poster: Clemens Prerovsky (cprerovsky)
Hi guys, I'm currently stuck with a really nasty XSL problem. I want to compare the currently active node name with the previous one inside an xsl:for-each loop. My data looks like: <root> <sub_A> <node_A/> <node_B/> <node_B/> <node_C/> <node_D/> <node_D/> <node_D/> </sub_A> <sub_B> ... </sub_B> </root> I'm using this loop to walk through my nodes: <xsl:for-each select="/root/sub_A/"> Now - when the node name changes I want to display a header. This is how it should work for my taste: <xsl:if test="name() != ../[(position()-1)]/name()"> ... display header ... </xsl:if> But unfortunately it does not work. Any ideas? I'd greatly appreciate any hints on this cause I don't know how to solve this one. Best regards, Clemens
Replies (2)
RE: XSL Problem: compare with previous node n - Added by Anonymous over 19 years ago
Legacy ID: #3213929 Legacy Poster: Michael Kay (mhkay)
General XSLT coding problems (that aren't Saxon-specific) should ideally go to the xsl-list at mulberrytech.com rather than here. The reason your code doesn't work is that position() changes inside a predicate. X[position()-1] in fact means X[position() = position()-1] which obviously selects nothing. Since your for-each isn't sorting, the easiest way to reference the previous item is using preceding-sibling: if (name() != name(preceding-sibling::[1]) However, a better way to tackle this might be <xsl:for-each-group select="subA/" group-adjacent="node-name()"> <...header...> <xsl:for-each select="current-group()"> <...content...> Michael Kay http://www.saxonica.com/
RE: XSL Problem: compare with previous node n - Added by Anonymous over 19 years ago
Legacy ID: #3213943 Legacy Poster: Clemens Prerovsky (cprerovsky)
Thanks for your help! The if statement worked fine - I'll also try to implement the for-each-group. Sorry for posting to the wrong newsgroup - I'll ask at mulberrytech for further help. Best regards Clemens Prerovsky
Please register to reply