<xsl:if test="not(@caption = preceding-sibling::@caption)">
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::
, and attribute::
(which @ is short for).
What you probably want is
<xsl:if test="not(@caption = preceding-sibling::constraint[1]/@caption)">
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. So A = 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? Because position()
reflects the direction of the current axis, which in this case is preceding-sibling
. As the XPath spec says,
An
axis that only ever contains the
context node or nodes that are before
the context node in document order is
a reverse axis. Thus, the ancestor,
ancestor-or-self, preceding, and
preceding-sibling axes are reverse
axes; all other axes are forward axes.
...
The proximity position of a member of
a node-set with respect to an axis is
defined to be the position of the node
in the node-set ordered in document
order if the axis is a forward axis
and ordered in reverse document order
if the axis is a reverse axis. The
first position is 1.
HTH.