I have two variables in my xslt 1.0 template, each one holding a node-set (selected with xpath). I want to determine if the node set in one variable is contained in the node set in the other. For example, suppose I have a chapter with nested sections. I want to see if $b is contained in $a:
<xsl:variable name="a" select="chapter//section" />
<xsl:variable name="b" select="chapter//section" />
I’ve tried to get an intersection of the nodesets to see if $a is the first common element shared by both $a and $b but this is not working:
<xsl:if test="$a/ancestor-or-self::*[count(.|$b) = count($b/ancestor::*)][1] = $a">
Adding some context. I’m creating a tabbed interface for a tree of html pages with this xslt, and the problem I’m trying to solve is to put class=”current” on the proper tab. By testing each section to see if it is a child of the tab section, it gets the class=”current” and so the tab will be styled to look as if it is on top.
To expand on Michael Kay’s good answer:
When a node
$nbelongs to a node-set$ns?If a node belongs to a node-set then the union of the node with the node-set doesn’t result in a bigger node-set — so the number of the nodes in the union must not be greater (in fact must be equal to) than the number of nodes in the original nodeset.
To express this with an XPath expression:
Now, the same logic tells us that a nodeset
$ns1is a subset of a nodeset$ns2exactly when the union of the two node-sets doesn’t contain more elements that the bigger node-set:If we want to test if
$ns1is a true subset of$ns2: