Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
| 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 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
  #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,
  #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)
  #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

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

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