Using XSLT I’m trying to work out a way to only emit a HTML table row when a value is different from the last iteration of the for-each loop. Essentially I want to make the table appear to have grouped headers, by only writing out the header once it changes.
I’m having a bit of trouble conceptually working out how I could do that considering you can’t change the value of a variable once it’s been defined.
<xsl:for-each select="//databaseConstraint/constraint">
<xsl:variable name="caption" select="@caption" />
<tr>
<th colspan="2"><xsl:value-of select="$caption" /></th>
</tr>
...
</xsl:for-each >
Update I’ve tried below, but I get an error: NodeTest expected here
<xsl:for-each select="//databaseConstraint/constraint">
<xsl:variable name="caption" select="@caption" />
<xsl:if test="not(@caption = preceding-sibling::@caption)">
<tr>
<th colspan="2">
<xsl:value-of select="$caption" />
</th>
</tr>
</xsl:if>
...
</xsl:for-each >
This would test whether the caption is equal to a caption attribute that is a sibling of the context node, i.e. a sibling of the constraint element that you’re processing. But I suspect that it “fails” with a syntax error, because the caption step has two axes:
preceding-sibling::, andattribute::(which @ is short for).What you probably want is
This will do what you want, it’s simpler than Muenchian, and it’s probably speedy enough, if the browser’s XPath implementation is decent, because it only needs to test one other node, rather than all preceding constraint nodes.
If this strategy is not fast enough for your purposes, e.g. if you have a lot of data, you can use Muenchian grouping, as @Frédéric said.
Addition: the
[1]is an abbreviation for[position() = 1]. What it means here is that on the right-hand side of the=, we only have the @caption of constraint immediately preceding the current element. If we omitted the[1], we would be comparing the current element’s @caption value with the @captions of all preceding sibling constraint elements.It’s important to realize that the XPath
=operator operates on node sets (when given a chance), not just on single values or nodes. SoA = B, where A and B are nodesets, returns true if there is any member of nodeset A that is equal to any member of nodeset B. This is rather like a join in SQL. It’s a powerful operation, but you have to be aware of what it’s doing.One other detail… Why does the
[1]yield the constraint element immediately preceding the current one, instead of the first one in the document? Becauseposition()reflects the direction of the current axis, which in this case ispreceding-sibling. As the XPath spec says,HTH.