[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

RE: [xsl] search against index


Subject: RE: [xsl] search against index
From: "Michael Kay" <mhk@xxxxxxxxx>
Date: Thu, 7 Aug 2003 16:50:56 +0100

In XSLT 2.0, you can write a function:

<xsl:function name="my:filter" as="node()*">
  <xsl:param name="input as="node()*"/>
  <xsl:param name="key" as="xs:string"/>
  <xsl:sequence select="$input[contains(@value,$key)]"/>
</xsl:function>

And then you can extend this to handle multiple keys:

<xsl:function name="my:multi-filter" as="node()*">
  <xsl:param name="input as="node()*"/>
  <xsl:param name="keys" as="xs:string*"/>
  <xsl:choose>
    <xsl:when test="count($keys)=0">
      <xsl:sequence select="$input"/>
    </xsl:when>
    <xsl:otherwise>
      <xsl:sequence select="my:multi-filter(
                               $input(contains(@value, $keys[1])),
                               $keys[position() gt 1])"/>
    </xsl:otherwise>
  </xsl:choose>
</xsl:function>

It's less feasible to do this in 1.0 because named templates can't
return the nodes that were passed to them as input, they can only create
new nodes, which is inefficient, as you pointed out.

Michael Kay

> -----Original Message-----
> From: owner-xsl-list@xxxxxxxxxxxxxxxxxxxxxx 
> [mailto:owner-xsl-list@xxxxxxxxxxxxxxxxxxxxxx] On Behalf Of bryan
> Sent: 07 August 2003 15:05
> To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
> Subject: [xsl] search against index
> 
> 
> 
> I have a parameter $string
> 
> And I do <xsl:apply-templates 
> select="index[contains(@value,$string)]"/>
> 
> This works great, but I want to do a search with string + 
> string, in other words given limitations of xslt, a search 
> with multiple contains.
> 
> So for example:
> <list>
> <index value="a string of values here">
> result
> </index>
> <index value="another string here">
> result2
> </index>
> </list>
> 
> 
> if $string = "another"
> 
> this returns the second index node, if $string  = "another + here"
> 
> then it still returns the second index node, if $string = 
> "string + here" it returns both nodes.
> 
> What I figured I'd have to do is the following
> <xsl:template match="/">
> <return>
> <xsl:variable name="returnNode">
> <xsl:choose>
> <xsl:when test="contains($string,'+')">
> <xsl:apply-templates select="index[contains(@value,$string)]"/>
> </xsl:when>
> <xsl:otherwise>
> <xsl:call-template name="nodesetreturner">
> <xsl:with-param name="searchstring" select="$string"/> 
> </xsl:call-template> </xsl:otherwise> </xsl:choose> 
> </xsl:variable> <xsl:copy-of select="gen:node-set($returnNode)"/>
> </return>
> </xsl:template>
> 
> <xsl:template name="nodesetreturner">
> <xsl:param name="searchstring"/>
> <xsl:param name="tempnodeset"/>
> <xsl:param name="exit"/>
> 
> <xsl:choose when test="contains($searchstring,'+') and 
> not(gen:node-set($tempnodeset)/*)">
> <xsl:variable name="locnodeset">
> <list>
> <xsl:apply-templates 
> select="index[contains(@value,substring-before($searchstring,'+'))]"/>
> </list>
> </xsl:variable>
> <xsl:call-template name="nodeseteval">
> <xsl:with-param name="locnodeset" select="$locnodeset"/> 
> <xsl:with-param name="searchstring" 
> select="substring-after(@value,'+')"/>
> </xsl:call-template>
> 
> </xsl:when>
> <xsl:when test="contains($searchstring,'+') and 
> gen:node-set($tempnodeset)/*"> <xsl:variable 
> name="locnodeset"> <list> <xsl:apply-templates
> select="gen:node-set($tempnodeset)/list/index[contains(@value,
> substring-
> before($searchstring,'+'))]"/>
> </list>
> </xsl:variable>
> 
> ..............and so forth I don't want to write the whole 
> thing out here, it's pretty obvious what the procedure is. 
> 
> The thing is I'm wondering if there's a better way to go 
> through it, this is pretty resource intensive, not to mention 
> tedious coding. 
> 
> But really I can't see anyway but building a rtf and then 
> trimming that with successive passes. 
> 
> 
> Also would like if anyone has examples of how this would be 
> improved in xslt 2.0 as I think it should be, perhaps via grouping.
> 
> </xsl:when>
> <xsl:when test="$exit = 'true' and not(gen:node-set($tempnodeset)/*)">
> <p>there were no search results</p>
> </xsl:when>
> 
> 
> </xsl:template>
> 
> <xsl:template name="nodeseteval">
> <xsl:param name="$locnodeset"/>
> <xsl:param name="$locsearchstring"/>
> <xsl:choose>
> <xsl:when test="gen:node-set($locnodeset)/list/*">
> <xsl:call-template name="nodesetreturner">
> <xsl:with-param name="searchstring" 
> select="$locsearchstring"/> <xsl:with-param 
> name="tempnodeset" select="$locnodeset"/> 
> </xsl:call-template> </xsl:when> <xsl:otherwise> 
> <xsl:call-template name="nodesetreturner"> <xsl:with-param 
> name="exit" select="'true'"/> </xsl:call-template> 
> </xsl:otherwise> </xsl:choose> </xsl:template>
> 
> 
> 
> 
> 
> 
>  XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
> 


 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list



Current Thread
Keywords