xslt question: using variable in xpath not allowed?

G

Guest

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
 
R

roy axenov

??? said:
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'] ?> said:
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.
 
M

Martin Honnen

张韡武 said:
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'"/>
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,981
Messages
2,570,188
Members
46,733
Latest member
LonaMonzon

Latest Threads

Top