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 April 23rd, 2008, 10:47 AM
Registered User
 
Join Date: Apr 2008
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default merging 2 xml with xslt

First of all, please accept my apologies if my question is a naif one,
I am learning xslt googling the web and I am being unable to find the answer by myself


I need to merge two xml files.
I can express my problem simplifying it to the next:

My principal xml is basically a sequence of same elements with a
couple of attributes

<a1>
  <a2 attr1="a" attr2="1"/>
  <a2 attr1="b" attr2="7"/>
  <a2 attr1="a" attr2="4"/>
</a1>


Another xml presents exactly the same sequence of elements
regarding to the attr1, but these have a different second attribute associated
(attr3 instead attr2)

<b1>
  <b2 attr1="a" attr3="x"/>
  <b2 attr1="b" attr3="y"/>
  <b2 attr1="a" attr3="z"/>
</b1>


What I need is my original xml enriched by the info in the second one:
<a1>
  <a2 attr1="a" attr2="1" attr3="x"/>
  <a2 attr1="b" attr2="7" attr3="y"/>
  <a2 attr1="a" attr2="4" attr3="z"/>
</a1>

I would appreciate any help on this.
Than you very much.




 
Old April 23rd, 2008, 11:00 AM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

If you want to simply merge based on the position (e.g. add /b1/b2[1]/@attr3 to /a1/a2[1] then the following stylesheet does that:
Code:
<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="1.0">

  <xsl:param name="f2" select="'test2008042302.xml'"/>

  <xsl:output method="xml" indent="yes"/>

  <xsl:variable name="b2" select="document($f2)/b1/b2"/>

  <xsl:template match="a1">
    <xsl:copy>
      <xsl:apply-templates select="a2"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="a2">
    <xsl:variable name="pos" select="position()"/>
    <xsl:copy>
      <xsl:apply-templates select="@*"/>
      <xsl:apply-templates select="$b2[$pos]/@attr3"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="@*">
    <xsl:copy/>
  </xsl:template>

</xsl:stylesheet>
The code uses the XSLT function named 'document' to access the second document, in my case the file name is 'test2008042302.xml' but you can set the parameter 'f2' as needed to your local file name.

--
  Martin Honnen
  Microsoft MVP - XML
 
Old April 23rd, 2008, 12:57 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

So the association between the elements in the two documents is entirely by their position?

Then I would suggest

<xsl:variable name="doc1" select="/"/>
<xsl:variable name="doc2" select="document('doc2.xml')"/>
<a1>
  <xsl:for-each select="$doc1/*/*">
    <xsl:variable name="p" select="position()"/>
    <xsl:copy>
      <xsl:copy-of select="@*"/>
      <xsl:copy-of select=$doc2/*/*[$p]/@*"/>
    </xsl:copy>
  </xsl:for-each>
</a1>


Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old April 24th, 2008, 04:34 AM
Registered User
 
Join Date: Apr 2008
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thank you Martin, Michael,

I think your answers to solve my "canonical problem" gave me the necessary hints to solve the real one (which is more complicated on structure)

One of many things a learnt from you is about the "document" function.
I read about it and I have seen that it accepts URLs, so it seems I am forced to use the file system. Initially I am thinking about avoiding using it because I will use the xslt to build a transformer into a java application, and both xml came as streams from another thread and one external process. Anyway, if necessary, I can use the file system. Or I would try to obtain an xslt on the fly instead of the second xml using what I learnt from your answers.

Thank you very much again.

 
Old April 24th, 2008, 04:41 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

I'm not a java person myself but I believe that the JAXP APi specifies a UrlResolver interface that can be used to resolve any URL, and return anything you want (i.e. your java stream).

/- Sam Judson : Wrox Technical Editor -/
 
Old April 24th, 2008, 05:12 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Most Java XSLT processors support the JAXP interface, which allows you to register a URIResolver class of your own devising. The means that when a URI is supplied to the document() function, you take responsibility for handling the URI and returning a document - it doesn't have to come from the web or from filestore.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old April 24th, 2008, 05:13 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Also, as an alternative to calling document(), you can supply additional documents as values for stylesheet parameters declared using xsl:param.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old April 24th, 2008, 05:27 AM
Registered User
 
Join Date: Apr 2008
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Great!

Thanks Sam, Michael,

I was just learning about URIresolver after reading Sam’s answer and was figuring out that, effectively, what I need can be done in the way you explains, Michael.

Thank you very much you all.
You made this forum incredible!






Similar Threads
Thread Thread Starter Forum Replies Last Post
merging xml using xslt sormel XSLT 1 July 9th, 2007 06:07 PM
Merging XML Help Please sam666 XML 7 March 8th, 2006 02:50 AM
merging a node from one XML to another pravind XSLT 3 February 21st, 2006 05:04 AM
Merging 2 Xml docs nkuar XML 3 December 8th, 2005 05:22 AM
merging 2 xml documents espider4u XSLT 0 August 31st, 2004 11:48 AM





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