Home. 
.

transparent

transparent

transparent

Altova Mailing List Archives


Re: Node-set variable and Contains function

From: Martin Honnen <mahotrash@-----.-->
To: NULL
Date: 11/6/2004 3:45:00 PM

gilly3 wrote:
> I want to filter my results to only return nodes that contain any one of a 
> set of strings.  I pass in a parameter that contains a node-set and then 
> filter like this:
> 
> <xsl:foreach select="myNode
>     [
>         contains(.,msxml:node-set($SearchString)//searchParam[1])
>         or contains(.,msxml:node-set($SearchString)//searchParam[2])
>         or contains(.,msxml:node-set($SearchString)//searchParam[3])
>         or contains(.,msxml:node-set($SearchString)//searchParam[4])
>         or contains(.,msxml:node-set($SearchString)//searchParam[5])
>         or contains(.,msxml:node-set($SearchString)//searchParam[6])
>         or contains(.,msxml:node-set($SearchString)//searchParam[7])
>         or contains(.,msxml:node-set($SearchString)//searchParam[8])
>         or contains(.,msxml:node-set($SearchString)//searchParam[9])
>         or contains(.,msxml:node-set($SearchString)//searchParam[10])
>     ]">
> 
> I'd like to come up with a more elegant way of doing this.  Right now, I've 
> hard-coded a limit of 10 searchParam's to search on.  I'd like to be able 
> to search on a dynamic number of searchParam's, depending on the number of 
> nodes in the parameter.  Is there a way to do this?

You can write a recursive template that checks each element in that 
nodeset parameter till it finds a match.

Here is an example stylesheet:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                 version="1.0">

<xsl:output method="xml" encoding="UTF-8" />

<xsl:param name="searchTerms" />

<xsl:template match="@* | node()">
   <xsl:copy>
     <xsl:apply-templates select="@* | node()" />
   </xsl:copy>
</xsl:template>

<xsl:template match="sentences">
   <xsl:copy>
     <xsl:for-each select="sentence">
       <xsl:variable name="contains">
         <xsl:call-template name="containsOr">
           <xsl:with-param name="element" select="." />
           <xsl:with-param name="terms" select="$searchTerms/terms/term" />
         </xsl:call-template>
       </xsl:variable>
       <xsl:if test="$contains = 'true'">
         <xsl:apply-templates select="." />
       </xsl:if>
     </xsl:for-each>
   </xsl:copy>
</xsl:template>

<xsl:template name="containsOr">
   <xsl:param name="element" />
   <xsl:param name="terms" />
   <xsl:variable name="head" select="$terms[1]" />
   <xsl:variable name="tail" select="$terms[position() &gt; 1]" />
   <xsl:choose>
     <xsl:when test="contains($element, $head)">
       <xsl:value-of select="true()" />
     </xsl:when>
     <xsl:otherwise>
       <xsl:choose>
         <xsl:when test="$tail">
           <xsl:call-template name="containsOr">
             <xsl:with-param name="element" select="$element" />
             <xsl:with-param name="terms" select="$tail" />
           </xsl:call-template>
         </xsl:when>
         <xsl:otherwise>
           <xsl:value-of select="false()" />
         </xsl:otherwise>
       </xsl:choose>
     </xsl:otherwise>
   </xsl:choose>
</xsl:template>

</xsl:stylesheet>

Here is an example XML input:

<?xml version="1.0" encoding="UTF-8"?>
<sentences>
   <sentence>Kibology for all.</sentence>
   <sentence>Xibo is the devil.</sentence>
   <sentence>Does Armstrong go for a 7th win?</sentence>
</sentences>

Here is an example file passed in as a nodeset:

<?xml version="1.0" encoding="UTF-8"?>
<terms>
   <term>Kibo</term>
   <term>Xibo</term>
   <term>Armstrong</term>
</terms>

And finally here is JScript code performing the transformation:

var xsltTemplate = new ActiveXObject('Msxml2.XSLTemplate.4.0');
var xslDocument = new ActiveXObject('Msxml2.FreeThreadedDOMDocument.4.0');
xslDocument.async = false;
xslDocument.load('test2004110602Xsl.xml');
xsltTemplate.stylesheet = xslDocument;

var xsltProcessor = xsltTemplate.createProcessor();

var xmlDocument = new ActiveXObject('Msxml2.DOMDocument.4.0');
xmlDocument.async = false;
xmlDocument.load('test2004110602.xml');

xsltProcessor.input = xmlDocument;

var params = new ActiveXObject('Msxml2.DOMDocument.4.0');
params.async = false;
params.load('test2004110603.xml');

xsltProcessor.addParameter('searchTerms', params);

xsltProcessor.transform();

xsltProcessor.output




-- 

	Martin Honnen
	http://JavaScript.FAQTs.com/


transparent
Print
Mail
Like It
Disclaimer
.

These Archives are provided for informational purposes only and have been generated directly from the Altova mailing list archive system and are comprised of the lists set forth on www.altova.com/list/index.html. Therefore, Altova does not warrant or guarantee the accuracy, reliability, completeness, usefulness, non-infringement of intellectual property rights, or quality of any content on the Altova Mailing List Archive(s), regardless of who originates that content. You expressly understand and agree that you bear all risks associated with using or relying on that content. Altova will not be liable or responsible in any way for any content posted including, but not limited to, any errors or omissions in content, or for any losses or damage of any kind incurred as a result of the use of or reliance on any content. This disclaimer and limitation on liability is in addition to the disclaimers and limitations contained in the Website Terms of Use and elsewhere on the site.

.
.

transparent

transparent