Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to match nodes which have ALL nodes in another sequence

Tags:

xslt

xpath

We're using XSLT2. Wondering if this is possible.

We have a tag filter, where a customer can choose to see all the themes which match ALL of their selections.

Here's an idea of the XML structure:

<themes>
   <theme>
      <name>Apple</name>
      <tags>
         <tag id="1">
         <tag id="2">
      </tags>
   </theme>
   <theme>
      <name>Banana</name>
      <tags>
         <tag id="2">
         <tag id="3">
      </tags>
   </theme>
   <theme>
      <name>Kiwifruit</name>
      <tags>
         <tag id="2">
         <tag id="3">
      </tags>
   </theme>
</themes>

The customer chooses tags 2 and 3. The result we want is to only show is Banana and Kiwifruit, as they have all the tags the user selected.

We can't use the AND operator as the list of tags is long and unknown. We currently have this list passed into the XSLT and then tokenised:

<xsl:param name="tag_ids"/>   
<xsl:variable name="tag_id_list" select="tokenize($tag_ids,',')"/>

This statement selects any theme that has any of the tag_id_list:

<xsl:for-each select="themes/theme/tags/tag[@id=$tag_id_list]">

But we're trying to find a XPath statement that makes sure the has ALL the s in $tag_id_list

Any ideas?! Thanks in advance.

like image 576
Dave Quested Avatar asked Dec 20 '25 20:12

Dave Quested


1 Answers

You want this if the tags have to be in the right order:

themes/theme/tags[deep-equal($tag_id_list, tag/@id)]

or this if they can be in any order:

themes/theme/tags[
  (every $tag in $tag_id_list satisfies $tag = tag/@id)
    and
  (every $tag in tag/@id satisfies $tag = $tag_id_list)]
like image 158
Michael Kay Avatar answered Dec 23 '25 10:12

Michael Kay



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!