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 February 11th, 2009, 10:21 PM
Authorized User
 
Join Date: Feb 2009
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
Default How to match multiple xml tags separately?

I have an xml file that is organized like a book. The root tag is <book> and it has multiple <chapter> tags with multiple <section> tags, some nested, within each chapter. Each section has multiple tables, lists, paragraphs, etc.

What I'm trying to do is use xslt to parse this xml file and output it as html and eventually pdf, keeping basically the same structure that exists within the xml source file. The problem I'm having is that I don't know how to match a single chapter, section, table, etc. without also matching all of the others. For example, if I want to match on the first table in the xml source file, I also end up matching all of the others and they get merged all together in the output. I want to keep them separate from each other.

So my question is, how do you keep the xml elements separate when their match is exactly the same: <xsl:template match="book/chapter/section">, <xsl:template match="book/chapter/section/table">, etc.?

I know that this must be a trivial issue for experienced coders, but I'm not finding the answer. If anyone can point me in the right direction I would appreciate it.

Thank you,
Josh
 
Old February 11th, 2009, 11:52 PM
Friend of Wrox
 
Join Date: Jun 2008
Posts: 291
Thanks: 9
Thanked 29 Times in 29 Posts
Default

You should use position value.

For example:

to get the first <section> under <chapter>, use <xsl:template match="book/chapter/section[1]">
and to get the second <table> under <section>, use <xsl:template match="book/chapter/section/table[2]">

Try this.
__________________
Rummy
 
Old February 12th, 2009, 05:31 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Please note that "tag" and "element" are not synonyms - an element usually has two tags, a start tag and an end tag. Getting the terminology right avoids misunderstandings.

I'm not sure exactly what your problem is. If you want to apply different processing to different chapters, you can distinguish them by a predicate, for example match="chapter[1]" or match="chapter[title='Introduction']". If that's not your issue, it might help if you show some of your code so we can see where you've gone wrong.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
 
Old February 13th, 2009, 07:39 PM
Authorized User
 
Join Date: Feb 2009
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks to both of you for your answers. I think I'm on the right track now. In a few hours this afternoon I wrote an xslt style sheet converting my xml document to xhtml. It just took me a few days to wrap my head around how to put all of the xslt together. Now I need to write xslt to transform it to PDF output. So far so good.

By the way, the XSLT Programmer's Reference was helpful. The example of how the XML specification was transformed was particularly useful.

Thanks again,
Josh





Similar Threads
Thread Thread Starter Forum Replies Last Post
Analyze-String and A Multiple-Match Example mphare XSLT 2 January 27th, 2009 05:21 PM
template match doesnt match the required node Tomi XSLT 2 March 12th, 2007 06:24 AM
UserControls and multiple form tags shenku C# 0 October 14th, 2005 09:08 AM
Getting selected item in dropdown to match XML juliaPGATOUR XSLT 1 December 11th, 2003 05:56 AM
Linking form and a table that are build separately takawera Access VBA 2 October 8th, 2003 10:18 AM





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