Cand XSLT evaluate XPath in String variable?

Discussion in 'XML' started by Son KwonNam, Jan 4, 2005.

  1. Son KwonNam

    Son KwonNam Guest

    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,
     
    Son KwonNam, Jan 4, 2005
    #1
    1. Advertising

  2. Son KwonNam

    Joris Gillis Guest

    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-view.cgi?userid=38041)
    Spread the wiki (http://www.wikipedia.org)
     
    Joris Gillis, Jan 4, 2005
    #2
    1. Advertising

  3. 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/
     
    Martin Honnen, Jan 4, 2005
    #3
  4. Son KwonNam

    Son KwonNam Guest

    Re: Cand XSLT evaluate XPath in String variable? - Thanks.

    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 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="1.0"
    xmlns:dyn="http://exslt.org/dynamic"
    extension-element-prefixes="dyn"
    >

    <xsl:eek:utput 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,
     
    Son KwonNam, Jan 5, 2005
    #4
  5. Son KwonNam

    Joris Gillis Guest

    Re: Cand XSLT evaluate XPath in String variable? - Thanks.

    > 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-view.cgi?userid=38041)
    "Quot capita, tot sententiae" - Terentius , Phormio 454
     
    Joris Gillis, Jan 5, 2005
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. philips
    Replies:
    2
    Views:
    690
    philips
    Jul 17, 2004
  2. Replies:
    9
    Views:
    1,456
  3. Martin Honnen
    Replies:
    0
    Views:
    592
    Martin Honnen
    Jun 3, 2008
  4. Mir Nazim
    Replies:
    2
    Views:
    361
    Mir Nazim
    Dec 21, 2008
  5. Jack Frillman
    Replies:
    2
    Views:
    2,794
    Martin Honnen
    Mar 17, 2011
Loading...

Share This Page