Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > XML > Cand XSLT evaluate XPath in String variable?

Reply
Thread Tools

Cand XSLT evaluate XPath in String variable?

 
 
Son KwonNam
Guest
Posts: n/a
 
      01-04-2005
In XSLT, is this possible to get value from xml using XPath
which is in XSLT variable?

I mean XPath strings can be dynamic while XSL Transforming.

If possible, How??

Because I'm not a native English speaker, it's quite hard
to make the problem clear. Please see the following example.

There are two XML files.
One has data, and the other has xpath. I will transform
the XPath XML.

for example,

* XPath XML
<xpaths>
<xpath>/students/student/name</xpath>
<xpath>/students/student/id</xpath>
</xpaths>

* Data XML : data.xml
<students>
<student>
<name>John Doe</name>
<id>234234</id>
</student>
</students>

* XSL snippet.
<xsl:template match="//xpath">
<xsl:variable name="xpathstring" select="."/>
<B>XPaht : <xsl:value-of select="$xpathstring"/></B>
<B>Value : <xsl:value-of select="document('data.xml')/$xpathstring" />
</B> </xsl:template>


As you know, that <xsl:value-of select(document(bla...)/> line
does not
work.

Any solution for this problem in XSLT?
I use JDOM/Xalan for transforming.

Thanks,
 
Reply With Quote
 
 
 
 
Joris Gillis
Guest
Posts: n/a
 
      01-04-2005
Hi,

> In XSLT, is this possible to get value from xml using XPath
> which is in XSLT variable?
>
> Any solution for this problem in XSLT?


This is not possible in XSLT1.0 (I don't know about 2.0), but you can work around it.

1) Use an extension function that evaluates a string as Xpath
e.g. 'dyn:evaluate' (http://www.exslt.org/dyn/functions/evaluate/index.html)
2) Use a two step tranformation

If you choose the second option, I could help - I have no experience whatsoever with the first option.

regards,
--
Joris Gillis (http://www.ticalc.org/cgi-bin/acct-v...i?userid=38041)
Spread the wiki (http://www.wikipedia.org)
 
Reply With Quote
 
 
 
 
Martin Honnen
Guest
Posts: n/a
 
      01-04-2005


Son KwonNam wrote:


> Any solution for this problem in XSLT?
> I use JDOM/Xalan for transforming.


Xalan should support
<http://www.exslt.org/dyn/functions/evaluate/index.html>

--

Martin Honnen
http://JavaScript.FAQTs.com/
 
Reply With Quote
 
Son KwonNam
Guest
Posts: n/a
 
      01-05-2005
Thank you.

It works perfectly.

I attach the dyn:evaluate example XSL for someone who has the same
problem as mine. With xalan, no need to do anything to use dyn:evaluate.
Just use.

----------------------------------------------------------------------
<xsl:stylesheet xmlnssl="http://www.w3.org/1999/XSL/Transform"
version="1.0"
xmlns:dyn="http://exslt.org/dynamic"
extension-element-prefixes="dyn"
>

<xslutput method="xml"/>

<xsl:template match="/">
<ROOT>
<xsl:apply-templates />
</ROOT>
</xsl:template>

<xsl:template match="xpath">
<RESULT>
<XPATH><xsl:value-of select="."/></XPATH>
<xsl:variable name="xpathstr"
select="concat('document(&quot;data.xml&quot',.) "/>
<VALUE><xsl:value-of select="dyn:evaluate($xpathstr)"/></VALUE>
</RESULT>
</xsl:template>

</xsl:stylesheet>


Son KwonNam wrote:
> In XSLT, is this possible to get value from xml using XPath
> which is in XSLT variable?
>
> I mean XPath strings can be dynamic while XSL Transforming.
>
> If possible, How??
>
> Because I'm not a native English speaker, it's quite hard
> to make the problem clear. Please see the following example.
>
> There are two XML files.
> One has data, and the other has xpath. I will transform
> the XPath XML.
>
> for example,
>
> * XPath XML
> <xpaths>
> <xpath>/students/student/name</xpath>
> <xpath>/students/student/id</xpath>
> </xpaths>
>
> * Data XML : data.xml
> <students>
> <student>
> <name>John Doe</name>
> <id>234234</id>
> </student>
> </students>
>
> * XSL snippet.
> <xsl:template match="//xpath">
> <xsl:variable name="xpathstring" select="."/>
> <B>XPaht : <xsl:value-of select="$xpathstring"/></B>
> <B>Value : <xsl:value-of select="document('data.xml')/$xpathstring" />
> </B> </xsl:template>
>
>
> As you know, that <xsl:value-of select(document(bla...)/> line
> does not
> work.
>
> Any solution for this problem in XSLT?
> I use JDOM/Xalan for transforming.
>
> Thanks,

 
Reply With Quote
 
Joris Gillis
Guest
Posts: n/a
 
      01-05-2005
> With xalan, no need to do anything to use dyn:evaluate. Just use.
Really? Thanks for pointing that out.
--
Joris Gillis (http://www.ticalc.org/cgi-bin/acct-v...i?userid=38041)
"Quot capita, tot sententiae" - Terentius , Phormio 454
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
xpath and Expression must evaluate to a node-set Jack Frillman XML 2 03-17-2011 11:26 AM
Re: xsltproc vs XPath evaluate() method Martin Honnen XML 0 06-03-2008 04:50 PM
ClassDefNotFound error with xpath.evaluate... drakaan@gmail.com Java 9 04-13-2007 08:06 PM
Problem selecting a node with XPATH if attribute value contains backslashes - how to force XPATH string to be treated as literal? Alastair Cameron XML 1 07-08-2003 07:24 PM
cand download any mrssages Wiltsey Computer Support 0 07-02-2003 04:18 PM



Advertisments