Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > XML > XSLT
Password Reminder
Register
| FAQ | Members List | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old January 19th, 2005, 01:11 PM
Authorized User
 
Join Date: Sep 2004
Location: , , .
Posts: 36
Thanks: 0
Thanked 0 Times in 0 Posts
Default Passing a variable into the id() function

<xsl:variable name="navDivID">
<xsl:value-of select="ancestor::NavigationDivision[1]/@PEDivRef" />
</xsl:variable>

<xsl:value-of select="$navDivID" />

This works so far - it pulls in the correct PEDivRef. Now I need to find the corresponding ID. If I hard-code the ID (value returned by navDivID) into a select with id(), like so:

<xsl:value-of select="id('c3')/PETOCEntry/Title" />

I get nothing. So, two questions: first, is the above syntax correct for pulling in the following (I should get Feelin' Fine):

<PEDiv id="c3">
<PETOCEntry id="c3a">
<Title CrossAlign="No">Feelin' Fine!</Title>
</PETOCEntry>
</PEDiv>

And second, how do I pass the variable $navDivID into the id function? I've tried this:

<xsl:value-of select="id('{$navDivID}')/PETOCEntry/Title" />

but I get an error: "This name may not contain the '{' character:-->{<--$navDivID}"

I've tried
<xsl:value-of select="id('$navDivID')/PETOCEntry/Title" />

and get "This name may not contain the '$' character: -->$<--navDivID"

I've tried
<xsl:value-of select="id({navDivID})/PETOCEntry/Title" />

and get "Expression expected. id(-->{<--navDivID})/PETOCEntry/Title"



Reply With Quote
  #2 (permalink)  
Old January 19th, 2005, 01:30 PM
mhkay's Avatar
Wrox Author
Points: 18,487, Level: 59
Points: 18,487, Level: 59 Points: 18,487, Level: 59 Points: 18,487, Level: 59
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

You're making it far too difficult!

Just do select="id($navDivID)/foo/bar"

Note also, when you write

<xsl:variable name="x">
  <xsl:value-of select="foo/bar"/>
</xsl:variable>

this is a very convoluted and inefficient way of saying

<xsl:variable name="x" select="foo/bar"/>

The two aren't absolutely identical, which is why the optimizer can't simple rewrite the first as the second, but if you use the value as a string then you'll get the same result.



Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
Reply With Quote
  #3 (permalink)  
Old January 19th, 2005, 02:00 PM
Authorized User
 
Join Date: Sep 2004
Location: , , .
Posts: 36
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks for the shortcut. I rewrote the variable, but I'm still getting nothing, even when I hard-code the value the variable returns into the id() function:

<xsl:value-of select="id('c3')/PETOCEntry/Title" />

Is this the correct syntax for pulling in Feelin' Fine from this code (yes, the id param is really an ID):

<PEDiv id="c3">
<PETOCEntry id="c3a">
<Title CrossAlign="No">Feelin' Fine!</Title>
</PETOCEntry>
</PEDiv>

This, however, does work, as long as I hard-code the values, which I can't do:

<xsl:value-of select="//PEDiv[@id = 'c3']/PETOCEntry/Title" />
Reply With Quote
  #4 (permalink)  
Old January 19th, 2005, 02:16 PM
Authorized User
 
Join Date: Sep 2004
Location: , , .
Posts: 36
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Well, I ended up getting it to work by doing this:

<xsl:value-of select="//PEDiv[@id = $navDivID]/PETOCEntry/Title" />

I'm pretty new to XSL, so maybe I'm not understanding the correct use of the id() function. I had thought that if I had an idref (which the @PEDivRef param is), that I could take it and find the corresponding ID by using id(). Is that wrong?

Reply With Quote
  #5 (permalink)  
Old January 19th, 2005, 02:17 PM
mhkay's Avatar
Wrox Author
Points: 18,487, Level: 59
Points: 18,487, Level: 59 Points: 18,487, Level: 59 Points: 18,487, Level: 59
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Does id('c3') return anything?

If not, you either haven't declared the attribute as an ID, or somewhere in the processing pipeline the knowledge that it was once an ID has been lost. This can happen for example if you use an object model like JDOM that doesn't retain ID information.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
Reply With Quote
  #6 (permalink)  
Old January 19th, 2005, 02:32 PM
Authorized User
 
Join Date: Sep 2004
Location: , , .
Posts: 36
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Nope, id('c3') doesn't return anything. I'm not the DTD developer, but I've looked at the file(s), and the params in question are definitely declared as ID and IDREF.

<!ELEMENT PEDiv (%pe-div.mdl;)>
<!ATTLIST PEDiv
    %pe.att;
    %common-idreq.att;>

<!ENTITY % common-idreq.att '
id ID #REQUIRED
Role NMTOKENS #IMPLIED
xml:lang NMTOKEN #IMPLIED'>

<!ATTLIST NavigationDivision
    TitlePage (Yes | No) 'No'
    PEDivRef IDREF #IMPLIED
    NavDivNumStyle (%num.vals;) #IMPLIED
    %common.att;>

I don't know what object model is being used. In any case, I did get it to work, although not the way it was intended to. Thanks for all your help.


Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Passing ID query rsm42 ASP.NET 1.0 and 1.1 Basics 2 January 7th, 2007 10:45 AM
Passing the Id to another form louie001 ASP.NET 1.0 and 1.1 Basics 1 July 14th, 2006 06:45 AM
passing variable to function Boszky Pro PHP 2 December 15th, 2005 03:01 AM
passing variable to function in innerHTML crmpicco Javascript How-To 5 October 19th, 2005 10:25 AM
Trying to establish an ID variable tkdk88 Dreamweaver (all versions) 3 January 25th, 2004 07:31 AM



All times are GMT -4. The time now is 11:23 AM.


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