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 | Calendar | 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 July 11th, 2007, 11:15 AM
Registered User
 
Join Date: Jul 2007
Location: , , .
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default Position

here's the xml
<?xml version="1.0" standalone="yes"?>
<Budget>
  <Dollars Jan_2007="10000" Feb_2007="11000" Mar_2007="12000">
    <Store>11111</Store>
  </Dollars>
  <Salaried Jan_2007="3" Feb_2007="3" Mar_2007="2">
    <Store>11111</Store>
  </Salaried>
  <Hourly Jan_2007="10" Feb_2007="12" Mar_2007="11">
    <Store>11111</Store>
  </Hourly>
</Budget>


for simplicty's sake desired output
Jan_2007 10000 3 10
etc...

here's the pertintent xsl i have

<xsl:for-each select="Budget/Dollars/@*">
         <p><xsl:value-of select="name()"/></p>
         <p><xsl:value-of select="."/></p>
         <p><xsl:value-of select='../../Hourly/@*[position()]' /></p>
         <p><xsl:value-of select="position()" /></p>
         <p><xsl:value-of select="../../Hourly/@*[4]" /></p>
</xsl:for-each>

the last two lines i put in for test purposes.

the next to last line outputs 1,2,3,4 etc... like i would expect

the last line where the position is hard coded return the attribute
value for that position one again like i would expect

the third line isn't giving me what i would expect. If position() is return the appropriate number as demonstated by line 4 and you can return the appropriate values based on position as demonstrated by line 5. THEN WHY DOESN'T LINE THREE WORK? I'm no expert at xslt as the above demonstrates but i've been banging my head on this one for over a day now and i just can't see why this shouldn't work. ANY HELP WOULD BE GREATLY APPRECIATED!!!







Reply With Quote
  #2 (permalink)  
Old July 11th, 2007, 11:39 AM
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: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

You've forgotten that the value of position() changes inside a predicate. x[position()] means x[position()=position()] which selects everything.

You can't reliably select attributes by position anyway. Just because Jan2007 is attribute 1 of Dollars doesn't mean it will be attribute 1 of Hourly. Attribute order is intrinsically unpredictable. You need to select the attribute with the corresponding name, i.e.

<p><xsl:value-of select='../../Hourly/@*[name()=name(current())]' /></p>

And there's still no guarantee that Jan will precede Feb in your output.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
Reply With Quote
  #3 (permalink)  
Old July 11th, 2007, 12:00 PM
Registered User
 
Join Date: Jul 2007
Location: , , .
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default

thanks for the info. the xml is not created by me so it is what it is. However "they" have guaranteed me that the attributes will always be in the same order and length so position 1, 2, 3 etc... for the nodes dollars,salaried,hourly will always relate to the same period. I guess i'll see if that holds true.

thanks again for the help

Reply With Quote
  #4 (permalink)  
Old July 11th, 2007, 12:42 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: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

It doesn't matter what order the attributes appear in the lexical XML. They won't necessarily be in that order by the time the XSLT stylesheet sees them. Many XML parsers use a hash table for attributes which doesn't retain order.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
Reply With Quote
  #5 (permalink)  
Old July 11th, 2007, 01:09 PM
Registered User
 
Join Date: Jul 2007
Location: , , .
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default

thanks for clarifying that. I'll look that up in the documentation for the parser i'm using. It appears to be working but that could be a fluke

Reply With Quote
  #6 (permalink)  
Old July 11th, 2007, 01:54 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: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

If it works with your current parser, just be aware it could stop working when you move to a different one. A lot of people got caught by this when they moved applications from JDK 1.4 to 1.5, which has a different XML parser.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
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
How to get the position of an element nazran Javascript How-To 3 February 2nd, 2010 06:56 AM
mouse position angelboy C# 2005 0 April 9th, 2007 04:07 PM
Position: relative; czambran BOOK: Beginning CSS: Cascading Style Sheets for Web Design ISBN: 978-0-7645-7642-3 2 April 7th, 2005 11:22 AM



All times are GMT -4. The time now is 07:33 AM.


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