Subject: merging 2 xml with xslt
Posted By: gbordel Post Date: 4/23/2008 10:47:18 AM
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.




Reply By: Martin Honnen Reply Date: 4/23/2008 11:00:04 AM
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:

<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
Reply By: mhkay Reply Date: 4/23/2008 12:57:42 PM
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
Reply By: gbordel Reply Date: 4/24/2008 4:34:09 AM
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.

Reply By: samjudson Reply Date: 4/24/2008 4:41:30 AM
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 -/
Reply By: mhkay Reply Date: 4/24/2008 5:12:30 AM
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
Reply By: mhkay Reply Date: 4/24/2008 5:13:38 AM
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
Reply By: gbordel Reply Date: 4/24/2008 5:27:35 AM
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!


Go to topic 70662

Return to index page 1