Home. 
.

transparent

transparent

transparent

Altova Mailing List Archives


Re: Merging node-sets in XSL

From: Martin Honnen <mahotrash@-----.-->
To: NULL
Date: 12/26/2007 1:50:00 PM

barnum@b... wrote:

> Is there any easy way to do this in XSL?

With XSLT 1.0 you could either chain two transformations or make use of 
an extension function to convert a result tree fragment into a node-set:

<xsl:stylesheet
   xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
   xmlns:exsl="http://exslt.org/common"
   exclude-result-prefixes="exsl"
   version="1.0">

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

   <xsl:strip-space elements="*"/>

   <xsl:key name="by-name" match="fruits/*" use="name()"/>

   <xsl:variable name="merger">
     <fruits>
       <xsl:copy-of select="fruits/*"/>
       <xsl:copy-of select="document('test2007122603.xml')/fruits/*"/>
     </fruits>
   </xsl:variable>

   <xsl:template match="fruits">
     <xsl:copy>
       <xsl:copy-of 
select="exsl:node-set($merger)/fruits/*[generate-id() = 
generate-id(key('by-name', name())[1])]"/>
     </xsl:copy>
   </xsl:template>

</xsl:stylesheet>

That stylesheet works fine for me for your XML sample documents and 
XslCompiledTransform in .NET 2.0 or Saxon 6.5.5. The "master" document 
is the primary XML input document, the other document is loaded with the 
document('test2007122603.xml').

For MSXML you need to change the namespace of the extension function to 
urn:schemas-microsoft-com:xslt:

<xsl:stylesheet
   xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
   xmlns:msxsl="urn:schemas-microsoft-com:xslt"
   exclude-result-prefixes="msxsl"
   version="1.0">

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

   <xsl:strip-space elements="*"/>

   <xsl:key name="by-name" match="fruits/*" use="name()"/>

   <xsl:variable name="merger">
     <fruits>
       <xsl:copy-of select="fruits/*"/>
       <xsl:copy-of select="document('test2007122603.xml')/fruits/*"/>
     </fruits>
   </xsl:variable>

   <xsl:template match="fruits">
     <xsl:copy>
       <xsl:copy-of 
select="msxsl:node-set($merger)/fruits/*[generate-id() = 
generate-id(key('by-name', name())[1])]"/>
     </xsl:copy>
   </xsl:template>

</xsl:stylesheet>


-- 

	Martin Honnen --- MVP XML
	http://JavaScript.FAQTs.com/


transparent
Print
Mail
Digg
delicious
Disclaimer
.

These Archives are provided for informational purposes only and have been generated directly from the Altova mailing list archive system and are comprised of the lists set forth on www.altova.com/list/index.html. Therefore, Altova does not warrant or guarantee the accuracy, reliability, completeness, usefulness, non-infringement of intellectual property rights, or quality of any content on the Altova Mailing List Archive(s), regardless of who originates that content. You expressly understand and agree that you bear all risks associated with using or relying on that content. Altova will not be liable or responsible in any way for any content posted including, but not limited to, any errors or omissions in content, or for any losses or damage of any kind incurred as a result of the use of or reliance on any content. This disclaimer and limitation on liability is in addition to the disclaimers and limitations contained in the Website Terms of Use and elsewhere on the site.

.
.

transparent

transparent