[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

[xsl] Re: Problems merging two xml documents


Subject: [xsl] Re: Problems merging two xml documents
From: sp <spamthis@xxxxxxxxxxxxxx>
Date: Tue, 19 May 2009 07:52:00 -0700

Ahh I understand, very enlightening thank you soo much!

all the best

Date: Mon, 04 May 2009 19:07:03 +0200
To:  xsl-list@xxxxxxxxxxxxxxxxxxxxxx
From: Martin Honnen <Martin.Honnen@xxxxxx>
Subject: Re: [xsl] Problems merging two xml documents
Message-ID: <49FF20B7.3070809@xxxxxx>

sp wrote:

> final doc, which is just a new version of doc1:
>
>
> <doc1>
>
> <a>
>  <a1></a1>
>  <a2></a2>
>  <!-- etc -->
> </a>
>
> <b>
>  <b1></b1>
>  <b2></b2>
>  <!--etc -->
> </b>
>
> <c>
>  <c1>more recent stuff</c1>
>  <c2>more recent stuff</c2>
>  <!--etc -->
> </c>
>
> <d> new element not in doc1 </d>
>
> </doc1>

Here is a sample XSLT 2.0 stylesheet that does the job with Saxon 9 and
the XML input samples you provided:

<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="2.0">

  <xsl:output indent="yes"/>

  <xsl:param name="url2" select="'test2009050404.xml'"/>
  <xsl:variable name="d2" select="document($url2)"/>

  <xsl:template match="/*">
    <xsl:copy>
      <xsl:apply-templates select="@*"/>
      <xsl:apply-templates select="*"/>
      <xsl:copy-of select="$d2/*/*[not(some $c in current()/* satisfies
(node-name(.) eq node-name($c)))]"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="/*/*">
    <xsl:copy>
      <xsl:copy-of select="*[not(some $c in $d2/*/*[node-name(.) eq
node-name(current())]/* satisfies (node-name(.) eq node-name($c)))]"/>
      <xsl:copy-of select="$d2/*/*[node-name(.) eq
node-name(current())]/*"/>
    </xsl:copy>
  </xsl:template>

</xsl:stylesheet>

--

       Martin Honnen
       http://msmvps.com/blogs/martin_honnen/

On Mon, May 4, 2009 at 9:31 AM, sp <spamthis@xxxxxxxxxxxxxx> wrote:
> HI,
>
> I'm trying to merge and copy two xml documents, into a new xml
> document and in a nutshell no matter what xsl I write I have yet to
> been able to get it to merge as I desire.
>
> I don't just want the content of the elements, I want to make a copy
> of each element making a new xml document. I've been utilizing the xsl
> copy-of and the document() to try and do this, with merge and copy
> examples as my guide.
>
> After a day and a half of trying, I've gotten close, but still can't
> get any one example to do what I need.  I don't know what of my many
> efforts to provide as an example, so I thought I should just
> illustrate below what I want and see if anyone can offer help.
>
> thanks in advance for any suggestions or examples.
>
> This document is merged with doc2, to form a new version of doc1.  Any
> common elements from doc2 replace doc1s elements of the same name, any
> new elements from doc2 are added
>
> <doc1>
>
> <a>
>  <a1></a1>
>  <a2></a2>
>  <!-- etc -->
> </a>
>
> <b>
>  <b1></b1>
>  <b2></b2>
>  <!--etc -->
> </b>
>
> <c>
>  <c1></c1>
>  <c2></c2>
>  <!--etc -->
> </c>
>
> </doc1>
>
> All except root element should be merged into doc1,  if element
> already exists it replaces the ones in doc1
>
> <doc2>
>
> <c>
>  <c1>more recent stuff</c1>
>  <c2>more recent stuff</c2>
>  <!--etc -->
> </c>
>
> <d> new element not in doc1 </d>
>
> </doc2>
>
>
> final doc, which is just a new version of doc1:
>
>
> <doc1>
>
> <a>
>  <a1></a1>
>  <a2></a2>
>  <!-- etc -->
> </a>
>
> <b>
>  <b1></b1>
>  <b2></b2>
>  <!--etc -->
> </b>
>
> <c>
>  <c1>more recent stuff</c1>
>  <c2>more recent stuff</c2>
>  <!--etc -->
> </c>
>
> <d> new element not in doc1 </d>
>
> </doc1>
>
>
> thank you


Current Thread
Keywords