![]() |
![]() | ![]() | ![]() | Altova Mailing List Archives>Archive Index >microsoft.public.xsl Archive Home >Recent entries >Thread Prev - Re: Merging node-sets in XSL [Thread Next] Re: Merging node-sets in XSLTo: 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/
| ![]() | ![]() | ![]() |
| Company | Legal | Press | Partners | Careers | Sitemap | Contact Us | Altova Blog | |||||
|
