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 August 16th, 2006, 02:25 PM
Registered User
 
Join Date: Sep 2005
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.
 
Old August 16th, 2006, 03:06 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 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
 
Old August 17th, 2006, 09:23 PM
Registered User
 
Join Date: Sep 2005
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?





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 12: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 09: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 03:26 AM





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