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 July 25th, 2007, 12:50 PM
Registered User
 
Join Date: Jul 2007
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default Grabbing content from 2 XML files and merging

Hello,

I am having some issues trying to merge two files, and am hoping someone may be able to help me out...

So, for example, lets say I have 2 XML files:

File1.xml
<parts>
  <part partNumber="123456" description="Widget" />
  <part partNumber="121212" description="Widget2" />
</parts>

File2.xml

<partInformation>
  <part partNumber="123456" description="Widget" />
    <specification name="12" version="2" />
    <type="Widget" />
  </part>
  <part partNumber="121212" description="Widget2" />
    <specification name="11" version="4" />
    <type="Widget" />
  </part>
</partInformation>

The second file will have a list of a ton of different parts and specifics about them. The output I need would be something like:

<parts>
  <part partNumber="123456" description="Widget" />
    <specification name="12" version="2" />
    <type="Widget" />
  </part>
  <part partNumber="121212" description="Widget2" />
    <specification name="11" version="4" />
    <type="Widget" />
  </part>
</parts>

If anyone could guide me in the right direction for figuring this out, It would be greatly appreciated!

Thanks in advance!


 
Old July 25th, 2007, 04:39 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

You need something along the lines of this.

Use File1.xml as the principal source document.

<xsl:key name="p" match="part" use="partNumber">

<xsl:template match="/">
<parts>
  <xsl:for-each select="parts/part">
    <part partNumber="{@partNumber}" description="{@description}">
      <xsl:copy-of select="key('p', @partNumber, document('File2.xml'))/*"/>
    </part>
  </xsl:for-each>
</parts>

Thats XSLT 2.0 syntax (key() with 3 arguments). In 1.0 you have to change the xsl:copy-of to

<xsl:variable name="partNr" select="@partNumber"/>
<xsl:for-each select="document('File2.xml')"/>
   <xsl:copy-of select="key('p', $partNr)/*"/>
</xsl:for-each>

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old August 6th, 2007, 08:47 AM
Registered User
 
Join Date: Jul 2007
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hello, thank you for your reply! Sorry for the slow response, was out last week.

I have given the code segment a shot that is listed below, but it appears key() accepts only two arguments, not three. I am not sure how to incorporate the second document in the XSLT so it will look there. Any suggestions?

 
Old August 6th, 2007, 08:59 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

If you read the reply more carefully you'll see that you need XSLT 2.0 for the three argument version, if you are using version 1.0 you need the xsl:for-each kludge.

--

Joe (Microsoft MVP - XML)
 
Old August 6th, 2007, 10:07 AM
Registered User
 
Join Date: Jul 2007
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Sorry, I was assuming the processor with VS2005 would process using 2.0. Apparently not. Saxon works better, but not giving me the correct output. It has put me in the right direction, however. Thanks!






Similar Threads
Thread Thread Starter Forum Replies Last Post
BizTalK - Merging of files BTUser Biztalk 0 November 14th, 2008 10:55 PM
Merging files ajindal General .NET 2 January 10th, 2007 06:42 AM
problem in merging of XML Files rajatake XSLT 5 April 29th, 2006 12:57 PM
Merging XML Help Please sam666 XML 7 March 8th, 2006 02:50 AM
protecting xml files (content) icopec XML 0 March 2nd, 2005 08:48 AM





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