Martin
Martin

Reputation: 2300

Checking if two arrays has matching item in XSLT

I have these two arrays:

<xsl:variable name="array1" select="umbraco.library:Split($currentPage/websiteThemes, ',')//value"/>
<xsl:variable name="array2" select="umbraco.library:Split($currentPage/websiteThemes2, ',')//value"/>

I would like to see which elements inside the arrays which matches. How can I accomplish that?

This is the output from copy-of array1 and array2:

array1:

<value>1087</value><value>1002</value><value>3202</value>

array2:

<value>1087</value><value>1577</value>

In this example, I would like to get the value 1087 as my result.

Upvotes: 1

Views: 1554

Answers (1)

Dimitre Novatchev
Dimitre Novatchev

Reputation: 243529

The question isn't too-clear, but I hope that this answers what is being asked:

This transformation:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:variable name="vArr1" select="/*/arr1/*"/>
 <xsl:variable name="vArr2" select="/*/arr2/*"/>

 <xsl:template match="/">
     <xsl:copy-of select="$vArr1[. = $vArr2]"/>
 </xsl:template>
</xsl:stylesheet>

when applied on this XML document (none provided in the question!):

<t>
 <arr1>
  <value>1087</value>
  <value>1002</value>
  <value>3202</value>
 </arr1>
 <arr2>
  <value>1087</value>
  <value>1577</value>
 </arr2>
</t>

produces (what I guess is) the wanted result:

<value>1087</value>

Upvotes: 2

Related Questions