0

Is there a way within a single XSLT file to first open one XML file and extract an element with children and then stuff that element into a second XML file?

If it can't be done in one XSLT, then my choice of languages would be vb script.

I have seen many different examples here, but I am just confused and don't understand the majority of them.

Here is the XSL i have working for extracting a node from XML1:

  <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output 
      method="xml"
      omit-xml-declaration="yes" 
      indent="yes"
      encoding="ISO-8859-1"/>
    <xsl:template match="/">
    <xsl:copy-of select="root/Collection/Item/." />
    </xsl:template> 
    </xsl:stylesheet> 

I want to take the resulting Item and append it to XML2 in the same XPATH, root/Collection.

I have a vb script from this answer How to save XML to a file (Thank you Andrew Cooper) that returns the XML to a memvar.

XML1:

<root>
 <collection1>
  <item attr1, attr2...>  ---this is what I am getting
   <more>
   </more>
  </item>
 <collection1>
 <collection2>
  <item attr1, attr2...>
   <more>
   </more>
  </item>
 <collection2>
 ...
 <collection_n>
  <item attr1, attr2...>
   <more>
   </more>
  </item>
 <collection_n>
</root>

XML2:

<root>
 <collection1>
  <item attr1, attr2...>
   <more>
   </more>
  </item>
---- i want to insert node from XML1 here, only in collection1
 <collection1>
 <collection2>
  <item attr1, attr2...>
   <more>
   </more>
  </item>
 <collection2>
 ...
 <collection_n>
  <item attr1, attr2...>
   <more>
   </more>
  </item>
 <collection_n>
</root>

so the new XML2:

<root>
 <collection1>
  <item attr1, attr2...>
   <more>
   </more>
  </item>
  <item attr1, attr2...>
   <more>
   </more>
  </item>
 <collection1>
 <collection2>
  <item attr1, attr2...>
   <more>
   </more>
  </item>
 <collection2>
 ...
 <collection_n>
  <item attr1, attr2...>
   <more>
   </more>
  </item>
 <collection_n>
</root>
Community
  • 1
  • 1
dashrader
  • 317
  • 3
  • 14

1 Answers1

2

You would use the document() function to load external documents so you can merge them with the one you're running the transformation on. Alternatively you can supply them as node-set to a template parameter (xsl:param) if you have them as DOM trees in memory when you're calling the XSLT.

Look at this question that I've answered about a week ago. It's exactly about how to merge content located at the same merging point (think same XPath) from multiple documents.

Hope it helps. If it does not, please ask a more specific question about what is not clear, what have you attempted, and what isn't working for you

Community
  • 1
  • 1
Pavel Veller
  • 6,085
  • 1
  • 26
  • 24
  • Thank you. I didn't see that one. There are many of these questions but not all of them are easy to find. Thanks again. – dashrader May 17 '12 at 22:51