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 September 25th, 2007, 02:49 AM
Friend of Wrox
 
Join Date: Jun 2007
Posts: 477
Thanks: 10
Thanked 19 Times in 18 Posts
Default relative XPath references

I'm experimenting with ways of putting together a bibliography. In my XML file I have <item> objects which have a number of children including <title>, <series>, and <volume>. I want the output to appear as...

Title1. Series1. Volume 1.
Title2. Series1. Volume 2.
Title3. Series2. Volume 1.
Title4. Series3. Volume 1.
Title5. Series3. Volume 2.
etc.

Here is the template I've built so far in my XSLT...

<xsl:template match="/">
   <xsl:for-each select="/Bibliography/item/title">
      <p><xsl:value-of select="." />. <xsl:value-of select="/Bibliography/item/series" />. Volume <xsl:value-of select="/Bibliography/item/volume" />.</p>
   </xsl:for-each>
</xsl:template>

I am successfully retrieving a list of titles. However, every single one is returning the very first series for the series and the number "1" for the volume number. How do I retrieve the sibling elements?

-------------------------

Whatever you can do or dream you can, begin it. Boldness has genius, power and magic in it. Begin it now.
-Johann von Goethe

When Two Hearts Race... Both Win.
-Dove Chocolate Wrapper

Chroniclemaster1, Founder of www.EarthChronicle.com
A Growing History of our Planet, by our Planet, for our Planet.
__________________
-------------------------

Whatever you can do or dream you can, begin it. Boldness has genius, power and magic in it. Begin it now.
-Johann von Goethe

When Two Hearts Race... Both Win.
-Dove Chocolate Wrapper

Chroniclemaster1, Founder of www.EarthChronicle.com
A Growing History of our Planet, by our Planet, for our Planet.
 
Old September 25th, 2007, 03:41 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

You don't specify how the title, series and volume elements are related. The following might work if you want the next series and volume elements.

Code:
<p><xsl:value-of select="." />. <xsl:value-of select="following-sibling::series" />. Volume <xsl:value-of select="following-sibling::volume" />.</p>
/- Sam Judson : Wrox Technical Editor -/
 
Old September 25th, 2007, 04:41 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

A path expression starting with "/" selects from the root of the document, not from the current node. So doing

for each /a/b/c
  select /a/b/c

is saying "for every c, process every c".

Which is exactly what you're seeing.

What you want is something more like

for each /Bibliography/item
  select title
  select series
  select volume

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old September 26th, 2007, 01:25 AM
Friend of Wrox
 
Join Date: Jun 2007
Posts: 477
Thanks: 10
Thanked 19 Times in 18 Posts
Default

Excellent, I have a variation on this that's working much better. I have a minor problem left in the sort though. A couple of the series I'm working with are quite long. When I sort them by volume I'm getting this...

Volume 1
Volume 10
Volume 11
Volume 2
.
.
.

I can make it work if I renumber all the single digit volume numbers into double digits (01, 02, 03...) but is there workable alternative?

-------------------------

Whatever you can do or dream you can, begin it. Boldness has genius, power and magic in it. Begin it now.
-Johann von Goethe

When Two Hearts Race... Both Win.
-Dove Chocolate Wrapper

Chroniclemaster1, Founder of www.EarthChronicle.com
A Growing History of our Planet, by our Planet, for our Planet.
 
Old September 26th, 2007, 01:40 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

<xsl:sort data-type="number"> should do it for you.

/- Sam Judson : Wrox Technical Editor -/
 
Old September 26th, 2007, 03:45 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Saxon offers an alphanumeric collation that will get this right:

<xsl:sort select="."
collation="http://saxon.sf.net/collation?alphanumeric=yes"/>

If you sort keys are literally as you describe then a more portable alternative is to split them:

<xsl:sort select="substring-before(., ' ')"/>
<xsl:sort select="number(substring-after(., ' ')"/>

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old September 27th, 2007, 01:16 AM
Friend of Wrox
 
Join Date: Jun 2007
Posts: 477
Thanks: 10
Thanked 19 Times in 18 Posts
Default

:)Thanks for all the advice on how to apply these techniques.

-------------------------

Whatever you can do or dream you can, begin it. Boldness has genius, power and magic in it. Begin it now.
-Johann von Goethe

When Two Hearts Race... Both Win.
-Dove Chocolate Wrapper

Chroniclemaster1, Founder of www.EarthChronicle.com
A Growing History of our Planet, by our Planet, for our Planet.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Relative Control Positioning rodmcleay C# 2005 0 January 24th, 2007 12:25 AM
relative size query noam Access 4 December 12th, 2005 06:00 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
Relative URLs? groupmatch BOOK: ASP.NET Website Programming Problem-Design-Solution 21 December 16th, 2004 10:53 AM
HELP with relative paths in FileSystemObject stalker Javascript 5 September 4th, 2003 02:38 PM





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