p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   XSLT (http://p2p.wrox.com/forumdisplay.php?f=86)
-   -   Passing a variable into the id() function (http://p2p.wrox.com/showthread.php?t=24140)

EstherMStrom January 19th, 2005 01:11 PM

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"




mhkay January 19th, 2005 01:30 PM

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

EstherMStrom January 19th, 2005 02:00 PM

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" />

EstherMStrom January 19th, 2005 02:16 PM

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?


mhkay January 19th, 2005 02:17 PM

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

EstherMStrom January 19th, 2005 02:32 PM

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.




All times are GMT -4. The time now is 02:47 AM.

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