Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old April 8th, 2007, 04:53 PM
Authorized User
 
Join Date: Apr 2007
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Default (newbie) using a variable

I have this simple XML:

Code:
<info>
    <uid>
        <from_session>1002</from_session>
    </uid>
    <userlist>
        <users>
            <user>
                <uid>1001</uid>
                <group>group1</group>
            </user>
            <user>
                <uid>1002</uid>
                <group>group2</group>
            </user>
        </users>
    </userlist>
</info>
and want to get the content of <group> for <user> that has <uid> equal to <from_session>, so I tried this:

Code:
<xsl:variable name="session_uid" select="/info/uid/from_session"/>
    <xsl:template match="/">
        <group>
            <xsl:apply-templates select="/info/userlist/users/user[uid='{$session_uid}']/group"/>
        </group>
    </xsl:template>
    <xsl:template match="group">
        <xsl:value-of select="."/>
    </xsl:template>
but it doesn't work :(

Is there a way to do it..?

 
Old April 8th, 2007, 05:04 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

You're making things too complicated. Replace:

'{$session_uid}'

by

$session_uid

A couple of points:

(a) curly braces are never used inside an XPath expression, they are only used to surround an XPath expression that is embedded in literal characters (an "attribute value template")

(b) variables in XPath don't work by textual substitution. A variable reference like $x is an expression and it can be used everywhere (and only where) an expression can appear. For example if $x holds a string then you can use it wherever you might validly use a string literal such as 'thing'.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old April 9th, 2007, 05:13 AM
Authorized User
 
Join Date: Apr 2007
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Default

This works fine. I appreciate the time you spent to answer and give my these tips :)

 
Old May 5th, 2007, 12:06 AM
Authorized User
 
Join Date: May 2007
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Will hi-jack the thread with similar question.

What the problem in defining a variable like this:
Code:
<xsl:variable name="lang_hu" select="//identificationInfo//title[@locale="#xpointer(//*[@id='hu'])"]"/>
Xalan processor doesn't like the middle of the bolded part:
Element type "xsl:variable" must be followed by either attribute specifications, ">" or "/>".


Tried and changed the attribute to:
Code:
@locale='#xpointer(//*[@id='hu'])']
Still no luck...

 
Old May 5th, 2007, 02:21 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

>select="//identificationInfo//title[@locale="#xpointer(//*[@id='hu'])"]"

XML doesn't allow quotes within an attribute value (when the attribute value is delimited by quotes) unless escaped as &quot;

>
@locale='#xpointer(//*[@id='hu'])']

XPath does not allow ' within a string literal (when the string literal is delimited by ')

The best way to do this is

<xsl:variable name="s">#xpointer(//*[@id='hu'])</xsl:variable>

<xsl:variable name="lang_hu" select="//identificationInfo//title[@locale=$s]"/>


Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old May 5th, 2007, 02:34 AM
Authorized User
 
Join Date: May 2007
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks Mike, everything's OK, I'm done with that at the moment.





Similar Threads
Thread Thread Starter Forum Replies Last Post
ASSIGNING A JAVA SCRIPT VARIABLE TO A XSL VARIABLE SOMANATHAN10 XSLT 1 February 21st, 2007 04:26 AM
"object variable or with block variable not set" netfresher ASP.NET 1.0 and 1.1 Basics 1 June 12th, 2006 03:50 PM
Newbie : How do you .. without setting a variable rqaran XSLT 2 August 6th, 2005 04:09 AM
Object variable or with block variable not set spacy ASP.NET 1.x and 2.0 Application Design 0 September 21st, 2004 12:19 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.