Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
| Search | Today's Posts | Mark Forums Read
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old December 13th, 2007, 09:18 AM
Registered User
 
Join Date: Nov 2007
Location: , , .
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default Problem with variable in test condition

Hello,

I am starting to doubt in power of xslt, not! :)

My problem is to build a searching engine.
So I have a string like in google: word1 "wo rd2" word3
I was writing a simple java method called search to parse this string into:
contains(.,'word1') or contains(.'wo rd2') or contains(.,'word3')
(I have seen strong difficulties in xslt, and that's why I decided use java)
But when I apply like this:

<xsl:stylesheet version="2.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:myExt="java:JavaClass" >

<xsl:param name="input">word1 "wo rd2" word3</xsl:param>
<xsl:variable name="search"><xsl:value-of select="myExt:search($input)"/></xsl:variable>

...

<xsl:when test="$search"> //this seems to be wrong, but in my opinion should work!
<xsl:copy>
<xsl:if test="@id">
<xsl:attribute name="id"><xsl:value-of select="substring-before(@id,'pl')"/></xsl:attribute>
</xsl:if>
<xsl:copy-of select="*"/>
</xsl:copy>
</xsl:when>

I receive the source document, even that I know that it doesn't contain any of word1, wo rd2 and word3.

Of course
<xsl:value-of select="$search"/> returns my string, that is: "contains(., 'word1') or ..."

So please tell what is wrong with this test attribute?

thanks
Bart


 
Old December 13th, 2007, 11:12 AM
Friend of Wrox
Points: 6,676, Level: 34
Points: 6,676, Level: 34 Points: 6,676, Level: 34 Points: 6,676, Level: 34
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2007
Location: Germany
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

You seem to want to put an XPath expression into a variable and evaluate that XPath expression dynamically. That is not possible, unless you use an extension function.
Saxon 9 provides http://www.saxonica.com/documentatio.../evaluate.html although you might want to explain in some detail what you want to achieve to allow us judge whether you really need to use that approach or whether we can find a pure XSLT/XPath 2.0 solution.

 
Old December 13th, 2007, 01:56 PM
mhkay's Avatar
Wrox Author
Points: 18,487, Level: 59
Points: 18,487, Level: 59 Points: 18,487, Level: 59 Points: 18,487, Level: 59
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Your Java method returns a string, there is no way the XSLT processor can know that this string should be treated as an XPath expression and evaluated as such. You can do that if your processor offers an xx:evaluate() extension function (Saxon does, for example) - but you need to call it explicitly.

However, it's really not difficult to solve this problem in XSLT 2.0. Something like this (not tested):

<xsl:function name="f:matches-one-of" as="xs:boolean">
  <xsl:param name="input" as="xs:string"/>
  <xsl:param name="search" as="xs:string"/>
  <xsl:variable name="results" as="xs:boolean*">
    <xsl:analyze-string select="$search" regex='(\S*)|("[^"]*?)'>
      <xsl:matching-substring>
        <xsl:sequence select='contains($input, regex-group(1))
           or contains($input, translate(regex-group(2), """", ""))'/>
      </xsl:matching-substring>
    </xsl:analyze-string>
  </xsl:variable>
  <xsl:sequence select="some $x in $results satisfies $x=true()"/>
</xsl:function>

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old December 20th, 2007, 07:31 AM
Registered User
 
Join Date: Nov 2007
Location: , , .
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I have spent last week reading your books about xslt and xpath 2.0.
Analyze-string, sequence and the other are powerful instruction. So I don't need to implement an extension functions and can stay by pure xslt.

Really appreciate, mission completed, that is all for now :)





Similar Threads
Thread Thread Starter Forum Replies Last Post
Test if variable is even or odd number Mikect Beginning VB 6 2 June 13th, 2016 08:17 AM
Usage of '*' in test condition of <xsl:when> vikkiefd XSLT 5 March 12th, 2008 10:31 PM
Having issues with a test condition... rbooth XSLT 3 November 11th, 2007 04:53 AM
problem with first test examples _l_ ASP.NET 1.0 and 1.1 Basics 1 July 5th, 2006 09:42 PM
Test MYSQL PROBLEM jhan316 MySQL 2 June 26th, 2006 03:46 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.