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 July 11th, 2007, 11:15 AM
Registered User
 
Join Date: Jul 2007
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!!!







 
Old July 11th, 2007, 11:39 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
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
 
Old July 11th, 2007, 12:00 PM
Registered User
 
Join Date: Jul 2007
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

 
Old July 11th, 2007, 12:42 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
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
 
Old July 11th, 2007, 01:09 PM
Registered User
 
Join Date: Jul 2007
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

 
Old July 11th, 2007, 01:54 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
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





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





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