Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > XML > xslt question: using variable in xpath not allowed?

Reply
Thread Tools

xslt question: using variable in xpath not allowed?

 
 
=?UTF-8?b?5byg6Z+h5q2m?=
Guest
Posts: n/a
 
      10-22-2006
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
 
Reply With Quote
 
 
 
 
roy axenov
Guest
Posts: n/a
 
      10-22-2006

??? 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

 
Reply With Quote
 
 
 
 
Martin Honnen
Guest
Posts: n/a
 
      10-22-2006
*韡* 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/
 
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
Cand XSLT evaluate XPath in String variable? Son KwonNam XML 4 01-05-2005 08:47 AM
Using XSLT and XPath for graph data structure processing? Ramon M. Felciano XML 6 01-15-2004 12:43 AM
Using XSLT and XPath for graph data structure processing? Ramon M. Felciano XML 1 01-12-2004 08:47 AM
[XSLT]Passing values from Javascript to a XSLT variable Benjamin Hillsley XML 3 09-25-2003 04:50 AM
XPath that does not include other XPath Anna XML 0 07-31-2003 07:55 AM



Advertisments