Apologies in advance - this is an XSLT question rather than SAXON-specific
but
I'm finding it impossible to send an E-mail which isn't repelled by the XSL-List fortress.


Given these 2 input files

dp-f1.xml

<?xml version="1.0"?>
<fa>
<!-- c2 - Dupl in f1, Uniq in f2 -->
<a name="c2">1</a>

<!-- c3 - Dupl in f1, Dupl in f2 -->
<a name="c3">1</a>

<!-- a2 - Dupl in f1, Not in f2 -->
<a name="a2">1</a>

<a name="c3">3</a> <a name="c2">3</a>

<!-- a1 - Uniq in f1, Not in f2 -->
<a name="a1">1</a>

<a name="a2">3</a> <a name="a2">5</a>
<a name="c2">5</a> <a name="c3">5</a>

<!-- c1 - Uniq in f1, Uniq in f2 -->
<a name="c1">1</a>

<a name="c2">7</a> <a name="c3">7</a>
<a name="c3">9</a>
</fa>

dp-f2.xml

<?xml version="1.0"?>
<fb>
<!-- c3 - Dupl in f1, Dupl in f2 -->
<a name="c3">100</a>

<!-- b2 - Not in f1, Dupl in f2 -->
<a name="b2">100</a>

<!-- c1 - Uniq in f1, Uniq in f2 -->
<a name="c1">100</a>

<a name="b2">300</a> <a name="c3">300</a>

<!-- c2 - Dupl in f1, Uniq in f2 -->
<a name="c2">100</a>
<a name="c3">500</a>

<!-- b1 - Not in f1, Uniq in f2 -->
<a name="b1">100</a>

<a name="c3">700</a> <a name="b2">500</a>
<a name="c3">900</a>

</fb>

====>
====> QUESTION 1 - What is the difference between the results of these 2 stylesheets?
====>

STYLESHEET 1
-----------------------
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:saxon="http://icl.com/saxon"
 extension-element-prefixes="saxon"
>
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/">
<res>
<xsl:variable name="CombinedAlt" select="(document('dp-f1.xml')/fa/a | document('dp-f2.xml')/fb/a)"/>
<Diagnostics>
<xsl:copy-of select ="$CombinedAlt"/>
</Diagnostics>
</res>
</xsl:template>
</xsl:stylesheet>

STYLESHEET2
-----------------------
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:saxon="http://icl.com/saxon"
 extension-element-prefixes="saxon"
>
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/">
<res>
<!-- Create a tree containing all the 'a' nodes from the 2 files -->
<xsl:variable name="CombinedTree">
<xsl:copy-of select="(document('dp-f1.xml')/fa/a | document('dp-f2.xml')/fb/a)"/>
</xsl:variable>
<!-- Convert the tree into a set -->
<xsl:variable name="CombinedSet" select="saxon:node-set($CombinedTree)" />
<Diagnostics>
<xsl:copy-of select ="$CombinedSet"/>
</Diagnostics>
</res>
</xsl:template>
</xsl:stylesheet>

If you run these transformations through Stand-alone SAXON, you might conclude they're identical. It's a list of all the 'a' nodes from the 2 files within <Diagnostics>.

If you run them through MSXML4 using MSXSL (after tweaking the namespaces), you may not be so sure. In the second case, all the 'a' nodes are shown on the same line, although in all other respects the output is identical.

But your doubts will increase when you use $CombinedSet or $CombinedAlt as the basis of a solution to the problem posed in the first article of  dpawson.co.uk/xsl/sect2/merge.html.

The problem posed there is to merge 2 files, with one 'a' node for each distinct 'name' attribute with a value equal to the sum of all the contributing values. The published solution assumes that each 'name' in a single file is unique. I tried to extend the solution to cope with 'names' being repeated within a file.

So now see these stylesheets and their output

STYLESHEET 3
-----------------------
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
        xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
        xmlns:saxon="http://icl.com/saxon"
        extension-element-prefixes="saxon"
>
<xsl:output method="xml" indent="yes"/>

<xsl:template match="/">
<res>

<xsl:variable name="CombinedAlt" select="(document('dp-f1.xml')/fa/a | document('dp-f2.xml')/fb/a)"/>

<!-- For each distinct name, sorted into name order -->
<xsl:for-each select="$CombinedAlt[not(@name=preceding::*/@name)]">
<xsl:sort select="@name" />
<a name="{@name}">

<xsl:value-of select="sum($CombinedAlt[@name=current()/@name])" />
</a>
</xsl:for-each>

</res>
</xsl:template>
</xsl:stylesheet>

RESULT 3
---------------
<?xml version="1.0" encoding="utf-8"?>
<res>
   <a name="a1">1</a>
   <a name="a2">9</a>
   <a name="b1">100</a>
   <a name="b2">900</a>
   <a name="c1">101</a>
   <a name="c1">101</a>
   <a name="c2">116</a>
   <a name="c2">116</a>
   <a name="c3">2525</a>
   <a name="c3">2525</a>
</res>

=====> QUESTION 2
=====> Stylesheet 3 copes with duplicates occurring in the same file but repeats the c1, c2, c3 nodes which occur in both files.

=====> Why are the duplicates not detected in the 'for-each' predicate ?

STYLESHEET 4
------------------------
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
        xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
        xmlns:saxon="http://icl.com/saxon"
        extension-element-prefixes="saxon"
>
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/">
<res>

<!-- Create a tree containing all the 'a' nodes from the 2 files -->
<xsl:variable name="CombinedTree">
<xsl:copy-of select="(document('dp-f1.xml')/fa/a | document('dp-f2.xml')/fb/a)"/>
</xsl:variable>
<!-- Convert the tree into a set -->
<xsl:variable name="CombinedSet" select="saxon:node-set($CombinedTree)" />

<!-- For each distinct name, sorted into name order -->
<xsl:for-each select="$CombinedSet/a[not(@name=preceding-sibling::*/@name)]">
 <xsl:sort select="@name" />
<a name="{@name}">

<xsl:value-of select="sum($CombinedSet/a[@name=current()/@name])" />
</a>
</xsl:for-each>

</res>
</xsl:template>
</xsl:stylesheet>


RESULT 4
---------------
<?xml version="1.0" encoding="utf-8"?>
<res>
   <a name="a1">1</a>
   <a name="a2">9</a>
   <a name="b1">100</a>
   <a name="b2">900</a>
   <a name="c1">101</a>
   <a name="c2">116</a>
   <a name="c3">2525</a>
</res>

=====> QUESTION 3
=====> This solution works but ...
=====> Why did I need $CombinedSet/a and not just $CombinedSet?

This could give a clue for a possible answer to Question 1. Does $CombinedSet contain a Root node which somehow is not visible in Stylesheet 2 diagnostics?


Martin Wills



********************************************************
Attachments in this message have been swept
by NAIs TVD (version 4.0.4215) for the presence
of known computer viruses.
********************************************************

________________________________________________________________________
This e-mail has been scanned for all viruses by Star Internet. The
service is powered by MessageLabs. For more information on a proactive
anti-virus service working around the clock, around the globe, visit:
http://www.star.net.uk
________________________________________________________________________