Home. 
.

transparent

transparent

transparent

Altova Mailing List Archives


Re: Merging node-sets in XSL

From: "Dimitre Novatchev" <dimitren@---.---.-->
To: NULL
Date: 12/26/2007 10:59:00 AM

No extension functions are necessary to produce the desired result.

This transformation:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

 <xsl:variable name="vS1" select="/*/fruits[1]"/>
 <xsl:variable name="vS2" select="/*/fruits[2]"/>

 <xsl:template match="/">
   <mergedFruits>
     <xsl:copy-of select="$vS1/*"/>
     <xsl:for-each select="$vS2/*">
       <xsl:variable name="velName" select="name()"/>
       <xsl:copy-of select=
           "self::*[not($vS1/*[name()=$velName])]"/>
     </xsl:for-each>
   </mergedFruits>
 </xsl:template>
</xsl:stylesheet>

when applied on this source xml document:

<sets>
 <fruits>
  <apple>1</apple>
  <lemon>33</lemon>
 </fruits>
 <fruits>
  <apple>3</apple>
  <banana>22</banana>
 </fruits>
</sets>

produces the desired result:

<mergedFruits>
  <apple>1</apple>
  <lemon>33</lemon>
  <banana>22</banana>
</mergedFruits>

Cheers,
Dimitre Novatchev


"Martin Honnen" <mahotrash@y...> wrote in message 
news:OFq5l37RIHA.4880@T......
> 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