Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > XML > XSLT
Password Reminder
| 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
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old May 21st, 2007, 12:02 AM
Registered User
Join Date: May 2007
Location: , , .
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Good way to select & process only one node


This is newbie question. Though I know one way to get what i want, but i would like a more better way to do it.

I have need to find and process only one node out of thousands and generate output for it.
The node needs to be selected based upon querystring value for book id and then processed by a template.

A simplified book structure is given below -

<?xml version="1.0" encoding="ISO-8859-1"?>
   <title lang="eng">Harry Potter</title>
   <title lang="eng">Learning XML</title>

One (IMO) non performant way of doing it is to have a template that matches books, select book with the correct id and pass it for processing to named template.
I am sure there is way to avoid matching all book nodes and then find the correct node to pass to named template.

For simplicity sake, i am assuming that the querystring book id value gets set in xslt variable (using xslt extensions) but right now is hardcoded to value 1.
<xslt:variable name="book_id" select="1">

<xslt:template match="book">
 <xslt:call-template name="GetBookDetails" >
   <xslt:with-param name="book_node" select ="bookstore/books/book[id=$book_id]"></xslt:with-param>
 </xslt:call-template >

Ideally, i would like to be able to do something like
<xslt:template match="bookstore/books/book[id=$book_id]">
   <xslt:apply templates/>
But i can not use variables in match statement. I did search on forums, but could not find related question.

Thanks for you help,
Reply With Quote
  #2 (permalink)  
Old May 21st, 2007, 02:07 AM
joefawcett's Avatar
Wrox Author
Points: 9,763, Level: 42
Points: 9,763, Level: 42 Points: 9,763, Level: 42 Points: 9,763, Level: 42
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
Join Date: Jun 2003
Location: Exeter, , United Kingdom.
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts

You have a template that matches book and then in another template, let's say one which matches /, you have:
<xsl:apply-templates select="*/books/book[bookId = $book_id]"/>

Joe (Microsoft MVP - XML)
Reply With Quote
  #3 (permalink)  
Old May 21st, 2007, 05:08 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

For the record you asked the same question on the mulberrytech list and received an answer there. Please don't waste people's time by posting on more than one forum, unless your first choice fails to deliver an answer.

Michael Kay
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
Reply With Quote

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
Process all nested child node tims XSLT 4 April 8th, 2008 07:45 AM
"&" is not a good join char! erme2 BOOK: Professional Ajax ISBN: 978-0-471-77778-6 2 April 15th, 2007 12:43 PM
Select ancestor Node bonekrusher XSLT 2 October 16th, 2006 07:07 AM
Help: Select every node except these nodes arcuza XSLT 8 May 25th, 2005 08:07 AM
select single node bcadmin BOOK: XSLT Programmer's Reference, 2nd Edition 1 November 10th, 2004 12:15 PM

All times are GMT -4. The time now is 10:02 PM.

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