xslt question: using variable in xpath not allowed?

Discussion in 'XML' started by =?UTF-8?b?5byg6Z+h5q2m?=, Oct 22, 2006.

  1. We have preffered language set as variable in xslt:

    <xsl:variable name="preferred_language">
    zh
    </xsl:variable>

    Data:
    <name xml:lang="de">Raw Materials (Mining incl.)</name>
    <name xml:lang="zh">原æ料(包括采矿业) </name>

    This works:
    <h2><xsl:value-of select="name[@xml:lang='zh']"/></h2>

    This will not work (produce result looks like "<h2></h2>"):
    <h2><xsl:value-of select="name[@xml:lang=$preferred_language]"/></h2>

    For me it's no problem if I have to use <choose> and <when> to do the same
    task (for each <name>, output <h2>xx</h2> only if the xml:lang equal to
    $preferred_language). Just I wish to confirm that is it "in all places
    variable cannot be used in xpath" or "I have used xpath with variable in
    wrong format".

    another question: is it possible to use URI parameter in xslt? I am
    developing in php so I can write:

    <xsl:variable name="preferred_language">
    <?= $_GET['lang'] ?>
    </xsl:variable>

    but I am stupid to do this if XSLT itself can use URI parameter. By URI
    parameter I mean the "?lang=zh" section in following URI:
    http://www.mysite.com/businessCategory.xml?lang=zh
     
    =?UTF-8?b?5byg6Z+h5q2m?=, Oct 22, 2006
    #1
    1. Advertising

  2. =?UTF-8?b?5byg6Z+h5q2m?=

    roy axenov Guest

    ??? wrote:
    > We have preffered language set as variable in xslt:
    >
    > <xsl:variable name="preferred_language">
    > zh
    > </xsl:variable>
    >
    > Data:
    > <name xml:lang="de">Raw Materials (Mining
    > incl.)</name>
    > <name xml:lang="zh">???(?????) </name>
    >
    > This works:
    > <h2><xsl:value-of
    > select="name[@xml:lang='zh']"/></h2>
    >
    > This will not work (produce result looks like
    > "<h2></h2>"):
    > <h2><xsl:value-of
    > select="name[@xml:lang=$preferred_language]"/></h2>


    The usual advice: if you want people to help you, make it
    easier for them. Post something people can feed to their
    XSLT processors without going through the motions of
    writing all the usual <xsl:stylesheet>s, <xsl:template>s
    etc.

    Your problem is that $preferred_language!='zh'. Instead, it
    contains 'zh' and a lot of useless whitespace.

    <xsl:value-of
    select="name[contains($preferred_language,@xml:lang)]"/>

    It's ugly, but it works. Also, see below.

    > another question: is it possible to use URI parameter in
    > xslt? I am developing in php so I can write:
    >
    > <xsl:variable name="preferred_language">
    > <?= $_GET['lang'] ?>
    > </xsl:variable>


    Don't do that. That's what created your problem in the
    first place. If you would've passed a parameter to your
    stylesheet instead of tinkering with the *source*, you
    wouldn't have needed any advice. What you should use to
    pass parameters depends on whether you're using PHP4+XSLT
    or PHP5+XSL. In either case, it's all in the docs on

    http://php.net/

    A temporary solution:

    <xsl:variable name="preferred_language"><?=
    $_GET['lang'] ?></xsl:variable>

    > but I am stupid to do this if XSLT itself can use URI
    > parameter. By URI parameter I mean the "?lang=zh" section
    > in following URI:
    > http://www.mysite.com/businessCategory.xml?lang=zh


    Oh, I see now. You're using client-side transformations.
    It'd be better to leave that to the server, especially
    since it's easy to do in PHP.

    --
    roy axenov
     
    roy axenov, Oct 22, 2006
    #2
    1. Advertising

  3. 张韡武 wrote:
    > We have preffered language set as variable in xslt:
    >
    > <xsl:variable name="preferred_language">
    > zh
    > </xsl:variable>


    Better make that e.g.
    <xsl:variable name="preferred_language" select="'zh'"/>



    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
     
    Martin Honnen, Oct 22, 2006
    #3
    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. Anna
    Replies:
    0
    Views:
    542
  2. Benjamin Hillsley
    Replies:
    3
    Views:
    1,706
    Dimitre Novatchev
    Sep 25, 2003
  3. Son KwonNam
    Replies:
    4
    Views:
    7,113
    Joris Gillis
    Jan 5, 2005
  4. Replies:
    2
    Views:
    5,115
    Stryder
    Feb 25, 2009
  5. Iñaki Baz Castillo
    Replies:
    13
    Views:
    521
    Iñaki Baz Castillo
    May 1, 2011
Loading...

Share This Page