Re: [xsl] Evaluate XPath from String

Subject: Re: [xsl] Evaluate XPath from String
From: Dimitre Novatchev <dnovatchev@xxxxxxxxx>
Date: Fri, 5 Jun 2009 10:53:17 -0700
On Fri, Jun 5, 2009 at 9:55 AM, James Cummings<james@xxxxxxxxxxxxxxxxx>
wrote:
> Hi there.
>
> I'm wondering about processing strings as XPaths in XSLT2. B I know
> about saxon:evaluate() but what I'm wondering is a way to do this in
> XSLT2 _without_ extensions. B (A possible answer is "don't do it, way
> too hard, just use the extensions".)

You can parse the XPath 2.0 expression using the lr-parse() function
of FXSL (The parser I wrote 1-2 years ago is not published , but I can
provide it to whoever is interested, and have done so), however this
is only half of the way to the required solution.

In fact, the problem as stated is underspecified with important parts
missing. To put it simply, as XPath is an embedded language, it must
be hosted and the host must provide the evaluation context (static and
dynamic). In this particular case, a number of even simple thing are
not defined:

What is the current documrn and the current node?

What variables are defined?

What functions are available?

.......

etc., ..., etc.

One could also develop the host in XSLT (wondering if there is reallu
any use in doing so, unless the underlying XSLT processor can produce
compiled binaries that can be executed independently of it), but even
that host will rely on some underlying XSLT processor and will
probably have to use a number of extension functions it provides).

In view of all this, it seems obvious that the much more direct way of
accomplishing this is to use directly the extensions provided by the
XSLT processor.



Cheers,
Dimitre Novatchev
---------------------------------------
Truly great madness cannot be achieved without significant intelligence.
---------------------------------------
To invent, you need a good imagination and a pile of junk
-------------------------------------
Never fight an inanimate object
-------------------------------------
You've achieved success in your field when you don't know whether what
you're doing is work or play




>
> So if I was using saxon:evaluate() I might do something like:
> ====input===
> <?xml version="1.0" encoding="UTF-8"?>
> <foo>
> B  B <paths>
> B  B  B  B <path>/foo/blort/wibble[1]</path>
> B  B  B  B <path>/foo/blort/wibble[2]</path>
> B  B  B  B  <path>//*[@xml:id='wibNum2']/splat/@att</path>
> B  B </paths>
> B  B <blort>
> B  B  B  B <wibble>test text 1</wibble>
> B  B  B  B <wibble>Another wibble </wibble>
> B  B  B  B <wibble xml:id="wibNum2">This is <splat att="value1">a
> test</splat></wibble>
> B  B </blort>
> B  B </foo>
> ====
>
> and a script like:
>
> ====xsl===
> <?xml version="1.0" encoding="UTF-8"?>
> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
> B  B  B version="2.0" xmlns:saxon="http://saxon.sf.net/";
> exclude-result-prefixes="#all">
> <xsl:output indent="yes"/>
> <xsl:template match="/foo">
> B  B <foo>
> B  B  B  B <xsl:for-each select="paths/path">
> B  B  B  B <out><xsl:value-of select="saxon:evaluate(.)"/></out>
> B  B </xsl:for-each>
> B  B  B  B </foo>
> </xsl:template>
>
> </xsl:stylesheet>
> ====
>
> I should get the output:
>
> ====output===
> <?xml version="1.0" encoding="UTF-8"?>
> <foo>
> B  <out>test text 1</out>
> B  <out>Another wibble </out>
> B  <out>value1</out>
> </foo>
> ====
>
> Hurrah, it works. B But I guess my question is how hard is it to create
> fn:myEvaluate() in XSLT2 which does basically the same thing?
>
> Thanks for any suggestions.
>
> -James
>
>



--
Cheers,
Dimitre Novatchev
---------------------------------------
Truly great madness cannot be achieved without significant intelligence.
---------------------------------------
To invent, you need a good imagination and a pile of junk
-------------------------------------
Never fight an inanimate object
-------------------------------------
You've achieved success in your field when you don't know whether what
you're doing is work or play

Current Thread