Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > XML > XSLT
Password Reminder
Register
Register | 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
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old August 16th, 2006, 03:25 PM
Registered User
 
Join Date: Sep 2005
Location: , , .
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default XSLT help: First instance of a node in merged file

I have two questions that can bot be illustrated with the same base files. I have two XML docs I want to merge and generate different lists.

The first list has the nodes related to the first occurrence of a "node" based on the value of an element within that node.


To illustrate: here's cars1.xml

<?xml version="1.0" encoding="utf-8" ?>
<cars>
           <car owner="BILL" >
        <Manufacturer>Mazda</Manufacturer>
        <Color>Black</Color>
        </car>
           <car owner="Sam" >
        <Manufacturer>Caddy</Manufacturer>
        <Color>white</Color>
        </car>
           <car owner="Paul" >
        <Manufacturer>Audi</Manufacturer>
        <Color>Grey</Color>
        </car>
</cars>

and here's cars2.xml
<?xml version="1.0" encoding="utf-8" ?>
<cars>
           <car owner="Paula" >
        <Manufacturer>Audi</Manufacturer>
        <Color>Purple</Color>
        </car>
           <car owner="Jim" >
        <Manufacturer>Buick</Manufacturer>
        <Color>white</Color>
        </car>
           <car owner="tammy" >
        <Manufacturer>Maserati</Manufacturer>
        <Color>Red</Color>
        </car>
</cars>

The result I'm looking for is a list of cars that is the first instance for any manufacturer after merging cars1 and cars2.

I know how to merge two (or more) XML docs into one using xsl:copy and document().

Can I somehow generate the file below without doing a second transform that looks for not(preceding-sibling::...)?

Here's what I hope to get:

<?xml version="1.0" encoding="utf-8" ?>
<cars>
           <car owner="BILL" >
        <Manufacturer>Mazda</Manufacturer>
        <Color>Black</Color>
        </car>
           <car owner="Sam" >
        <Manufacturer>Caddy</Manufacturer>
        <Color>white</Color>
        </car>
           <car owner="Paul" >
        <Manufacturer>Audi</Manufacturer>
        <Color>Grey</Color>
        </car>
           <car owner="Jim" >
        <Manufacturer>Buick</Manufacturer>
        <Color>white</Color>
        </car>
           <car owner="tammy" >
        <Manufacturer>Maserati</Manufacturer>
        <Color>Red</Color>
        </car>
</cars>

Note that there are only five car nodes- not six- the second "Audi" record is missing. Sorting can take place without problem if helpful.

Is there a way to do this in a single XSLT?

Second question: Using the same files above. Is there a way to preserve all manufacturers listed in cars2, but replace "matching manufacturer" records from cars1 wher they apply7 The desired result in that case would be:
<?xml version="1.0" encoding="utf-8" ?>
<cars>
           <car owner="Paul" >
        <Manufacturer>Audi</Manufacturer>
        <Color>Grey</Color>
        </car>
           <car owner="Jim" >
        <Manufacturer>Buick</Manufacturer>
        <Color>white</Color>
        </car>
           <car owner="tammy" >
        <Manufacturer>Maserati</Manufacturer>
        <Color>Red</Color>
        </car>
</cars>


I've seen some nifty code to do similar sorts of things for matches in both, but can't figure this one out.

All help is appreciated.
Reply With Quote
  #2 (permalink)  
Old August 16th, 2006, 04:06 PM
mhkay's Avatar
Wrox Author
Points: 18,420, Level: 59
Points: 18,420, Level: 59 Points: 18,420, Level: 59 Points: 18,420, Level: 59
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,952
Thanks: 0
Thanked 289 Times in 284 Posts
Default

Most of the XSLT 1.0 grouping techniques work only on nodes in a single document, so you have to merge the documents first and then do grouping. The main exception is Ken Holman's "variable grouping" technique described at http://sourceware.org/ml/xsl-list/2001-10/msg00933.html

With XSLT 2.0, of course, xsl:for-each-group solves it easily.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
Reply With Quote
  #3 (permalink)  
Old August 17th, 2006, 10:23 PM
Registered User
 
Join Date: Sep 2005
Location: , , .
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks for saving me some time trying to find a solution that uses only one transform!

Did you/anyone else have any thoughts on how to accomplish the second question?
Reply With Quote
Reply


Thread Tools
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
XSLT Help to get value from Upper Node by compare pixelheart XSLT 10 May 1st, 2008 01:25 PM
changing output for one node in XSLT shane_carty XSLT 3 February 27th, 2008 05:28 AM
modifying a node in xslt the rail dale XSLT 3 July 17th, 2007 10:15 AM
XSLT Going up a level from current node. lafilip XSLT 4 February 23rd, 2007 03:06 PM
Problem regarding finding the node in xslt. rohitjain13 XSLT 1 October 6th, 2005 04:26 AM



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


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