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 March 19th, 2008, 07:22 AM
Registered User
 
Join Date: Mar 2008
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Combining string paths elements into another XML

Hello,

I'm currently a bit stumped on how to solve the following issue correctly. Having gone round in circles numerous times I'm admitting defeat and seeking help.

Summary: I need to combine the stuff.xml document elements into the main.xml document. The "name" attrubute of the <stuff> child elements identifies a dotted path of "name" attributes in main.xml (clash of naming there). The children and additional attributes of the <stuff> child elements are what get combined.

I'm experimenting using XML Spy software at the moment, but ultimately this will run inside a .NET 2.0 app.

I've used keys to convert the dotted path into a node from main.xml. This worked but I think my lack of understanding left me stuck there (I wasn't sure how to use the resulting element). I also tried generating the dotted paths in a function while iterating though main.xml, for comparing against the <stuff> child elements, but this is hugely inefficient.

Any prods in the right direction would be greatly appreaciated!

-- main.xml --
<?xml version="1.0" encoding="UTF-8"?>
<alpha name="one">
        <beta name="two">
                <gamma name="three" />
                <delta name="four" />
        </beta>
        <beta name="five">
                <gamma name="six" />
                <delta name="seven" />
        </beta>

        <epsilon name="eight" />
</alpha>


-- stuff.xml --
<?xml version="1.0" encoding="UTF-8"?>

<stuff>
        <gamma name="one.two.three" id="10000">foobar</gamma>
        <gamma name="one.five.six" id="10001">wibble</gamma>
        <delta name="one.two.four" id="10002">gargle</delta>
        <delta name="one.five.seven" id="10003">blarg</delta>
        <epsilon name="one.eight" id="10004">biggles</epsilon>
</stuff>


-- required output --
<?xml version="1.0" encoding="UTF-8"?>

<alpha name="one">
        <beta name="two">
                <gamma name="three" id="10000">foobar</gamma>
                <delta name="four" id="10002">gargle</delta>
        </beta>
        <beta name="five">
                <gamma name="six" id="10001">wibble</gamma>
                <delta name="seven" id="10003">blarg</delta>
        </beta>

        <epsilon name="eight" id="10004">biggles</epsilon>
</alpha>

Many thanks,

Paul

(Hopefully no typos into the above test xml)

 
Old March 19th, 2008, 07:46 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

As always this kind of thing is easier in XSLT 2.0. You don't state any constraints that prevent a 2.0 solution so I'll go with that.

It feels like this:

<xsl:key name="dp" match="stuff/*" use="@name"/>

<xsl:template match="*">
  <xsl:copy>
  <xsl:copy-of select="key('dp', string-join(ancestor-or-self::*/@name, '.'), doc('stuff.xml'))/(@id, text())"/>
  <xsl:apply-templates/>
  </xsl:copy>
</xsl:template>



Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old March 19th, 2008, 11:06 AM
Registered User
 
Join Date: Mar 2008
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Michael,

I guessed it would be an embarrassingly short answer :-)

That works flawlessly. Thanks very much for your time!

Paul







Similar Threads
Thread Thread Starter Forum Replies Last Post
JDOM Combining two XML documents Clone help [email protected] XML 2 July 25th, 2006 06:01 PM
Basic doubt in combining XML Documents rajatake XSLT 2 May 4th, 2006 04:30 AM
beginners problems (combining xml feeds etc) bewise XSLT 0 February 7th, 2006 06:39 AM
Using XSLT arguments for in XML paths jacob XSLT 7 November 7th, 2005 11:04 AM
Combining adjacent elements with the same name susanne_broeenhorst XSLT 2 March 10th, 2005 03:49 AM





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