By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,190 Members | 1,503 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,190 IT Pros & Developers. It's quick & easy.

stack values in xsl or substract element from tree

P: n/a
Hi,

In a complex merging of two (non ordered) xml files
i need to keep track of the elements of the second tree that were already
merged with first tree, to copy only unused elements at the end.

I tried two solutions:

* first is to 'substract' the used element from existing tree

<param name="elem" />
<param name="tree" />

<call-template name="recurse">
<with-param name="tree"
select="$tree/*[generate-id() != generate-id($elem)]" />
</call-template>

(elem was found among $tree/*)

Last copy would be
<copy-of select="$tree" />

* second was to stack values

<key name="uid" match="*" use="used-id" />
....
<param name="elem" />
<param name="tree" />

<param name="used-tree">
<used used-id="{generate-id($elem)}" />
<copy-of select="$used-tree" />
</param>

Last copy would be
<apply-templates mode="last-copy"
select="$tree[count(. | key('uid', generate-id())=0]" />
<template mode="last-copy" match="node()|@*">
<copy>
<apply-templates mode="last-copy"
select="./*[count(. | key('uid', generate-id())=0]" />
</copy>
</template>

(maybe using exslt:node-set($tree) to have a node-set)
*****

Which way should work best ? what are the standard solutions for this ?
Jul 20 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
I cannot understand either the problem or the solutions.

You are speaking about "trees", but you are using your $tree variable just
as a node-set (tree with a depth of one).

Also, it is not at all clear what the second method does.

I suspect that your original problem may have a simpler solution, in which
it is not necessary to "copy only unused elements at the end".

Probably if you describe your problem here, many people will be able to
help.

Dimitre Novatchev.
FXSL developer, XML Insider,

http://fxsl.sourceforge.net/ -- the home of FXSL
Resume: http://fxsl.sf.net/DNovatchev/Resume/Res.html
"Jean-Christophe Michel" <jc**************@online.fr> wrote in message
news:pa****************************@online.fr...
Hi,

In a complex merging of two (non ordered) xml files
i need to keep track of the elements of the second tree that were already
merged with first tree, to copy only unused elements at the end.

I tried two solutions:

* first is to 'substract' the used element from existing tree

<param name="elem" />
<param name="tree" />

<call-template name="recurse">
<with-param name="tree"
select="$tree/*[generate-id() != generate-id($elem)]" />
</call-template>

(elem was found among $tree/*)

Last copy would be
<copy-of select="$tree" />

* second was to stack values

<key name="uid" match="*" use="used-id" />
...
<param name="elem" />
<param name="tree" />

<param name="used-tree">
<used used-id="{generate-id($elem)}" />
<copy-of select="$used-tree" />
</param>

Last copy would be
<apply-templates mode="last-copy"
select="$tree[count(. | key('uid', generate-id())=0]" />
<template mode="last-copy" match="node()|@*">
<copy>
<apply-templates mode="last-copy"
select="./*[count(. | key('uid', generate-id())=0]" />
</copy>
</template>

(maybe using exslt:node-set($tree) to have a node-set)
*****

Which way should work best ? what are the standard solutions for this ?

Jul 20 '05 #2

P: n/a
On Wed, 24 Dec 2003 12:05:39 +0100, Dimitre Novatchev wrote:
I cannot understand either the problem or the solutions.
Sorry, i was probably not clear.
You are speaking about "trees", but you are using your $tree variable just
as a node-set (tree with a depth of one).
It contains the document() loaded.
Probably if you describe your problem here, many people will be able to
help.


Would be happy if such solution existed. I've been searching for a while.

We have a main xml file containing a recursive structure:

<definition id="first" ... >
<param name="p1">value1</param>
<param name="p2">value2</param>
<section name="s1">
<param name="p3">value3</param>
<param name="p4">value4</param>
</section>

<children>
<definition id="second" ...>
</definition>

<include id="overridden-id" definition="myfile">
<param name="p5">value5</param>
<section name="s2">
<param name="p6">value6</param>
</section>
<children>
<definition id="ignored" .../>
</children>
</include>

</children>
</definition>

The included 'myfile' contains itself a definition:

<definition id="included" other-attrib="foo">
<param name="p5">overridden value5</param>
<section name="s2">
<param name="p7">value7</param>
</section>
<children>
<definition id="included-too" .../>
</children>
</definition>

The purpose is to include the file(s) and override the elems/attribs with
the values contained in <include> elems, _only_ for some elemes/attribs
that are authorized to be overriden :-)
Here is the wished result for the example here:

<definition id="first" ... >
<param name="p1">value1</param>
<param name="p2">value2</param>
<section name="s1">
<param name="p3">value3</param>
<param name="p4">value4</param>
</section>

<children>
<definition id="second" ...>
</definition>

<definition id="overridden-id" other-attrib="foo">
<param name="p5">overridden value5</param>
<section name="s2">
<param name="p6">value6</param>
<param name="p7">value7</param>
</section>
<children>
<definition id="included-too" .../>
</children>
</definition>

</children>
</definition>

Note that include/children are ignored because children cannot be
overridden.
The included file can contain other include elements too.
One more constraint: our xml format can be extended with other ns and the
xsl sheet should be easily extendable (ie by adding only a template that
could be included if possible).

My solution (?) currently is (sorry for the length):
(not working completely :(

<xsl:template match="/">
<xsl:apply-templates />
</xsl:template>

<!-- ::: start to merge on include only ::: -->
<xsl:template match="include">
<!-- try to retrieve the included ndf -->
<xsl:param name="ndf" select="@definition" />

<xsl:call-template name="merge-trees">
<xsl:with-param name="included-ndf" select="document($ndf)/definition" />
<xsl:with-param name="main-ndf" select="." />
</xsl:call-template>
</xsl:template>

<!-- ::: merge two trees, some elements and attribs can be overridden ::: -->
<xsl:template name="merge-trees">
<xsl:param name="included-ndf" />
<xsl:param name="main-ndf" />

<xsl:choose>
<!-- loop recursively on included elements -->
<xsl:when test="count($included-ndf)&gt;0">
<xsl:variable name="included-elt" select="$included-ndf[1]" />

<!-- find overriding element from main -->
<xsl:variable name="main-elt">
<xsl:apply-templates select="$main-ndf" mode="find-elem-in-main">
<xsl:with-param name="ie" select="$included-elt" />
</xsl:apply-templates>
</xsl:variable>

<xsl:choose>
<!-- handle overriding (if any) -->
<xsl:when test="boolean($main-elt)">
<xsl:apply-templates select="exslt:node-set($main-elt)"
mode="mix-with-main">
<xsl:with-param name="ie" select="$included-elt" />
</xsl:apply-templates>

<!-- recurse on following -->
<!-- commented because leads to infinite loop :(
xsl:call-template name="merge-trees">
<xsl:with-param name="included-ndf"
select="$included-ndf[position() &gt; 1]" />
<xsl:with-param name="main-ndf"
select="$main-ndf[generate-id() !=
generate-id(exslt:node-set($main-elt))]" />
</xsl:call-template-->
</xsl:when>

<!-- else simply copy current included element -->
<xsl:otherwise>
<xsl:apply-templates select="." />
</xsl:otherwise>
</xsl:choose>
</xsl:when>

<!-- copy remaining main elements -->
<xsl:otherwise>
<xsl:apply-templates select="$main-ndf" />
</xsl:otherwise>
</xsl:choose>
</xsl:template>

<!-- ::::::::::: extendable part ::::::::::::
(we can include similar templates later) -->

<!-- ::: find the element in main that could override $ie (included element) ::: -->
<xsl:template match="include" mode="find-elem-in-main">
<xsl:param name="ie" />

<xsl:if test="name($ie)='definition'">
<xsl:copy-of select="." />
</xsl:if>
</xsl:template>

<xsl:template match="param" mode="find-elem-in-main">
<xsl:param name="ie" />

<xsl:if test="name($ie)='param' and $ie/@name = @name">
<xsl:copy-of select="." />
</xsl:if>
</xsl:template>

<!-- ::: merge included element with overriding in main ::: -->
<xsl:template match="include" mode="mix-with-main">
<xsl:param name="ie" />

<xsl:element name="definition">
<xsl:attribute name="id">
<xsl:choose>
<xsl:when test="boolean(@id)">
<xsl:value-of select="@id" />
</xsl:when>
<xsl:when test="boolean($ie/@id)">
<xsl:value-of select="$ie/@id" />
</xsl:when>
<xsl:otherwise>
<xsl:text>undefined</xsl:text>
</xsl:otherwise>
</xsl:choose>
</xsl:attribute>

<xsl:apply-templates select="$ie/@*[name()!='definition' and name()!='id']" />
<xsl:apply-templates select="@*[name()!='definition' and name()!='id']" />

<!-- recurse on children -->
<xsl:call-template name="merge-trees">
<xsl:with-param name="included-ndf" select="$ie/*" />
<xsl:with-param name="main-ndf" select="./*" />
</xsl:call-template>
</xsl:element>
</xsl:template>

<xsl:template match="param" mode="mix-with-main">
<xsl:param name="ie" />

<xsl:copy>
<xsl:apply-templates select="$ie" />
</xsl:copy>
</xsl:template>
<!-- /::::::::::: extendable part :::::::::::: -->

<!-- ::: copy all others and don't forget attributes ::: -->
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*" />
</xsl:copy>
</xsl:template>

Jul 20 '05 #3

P: n/a
Hey,

ANswering myself to tell i found a solution:
<!-- find position of overriding element from main -->
<xsl:variable name="main-elt-pos">
<xsl:apply-templates select="$main-ndf"
mode="find-elem-in-main">
<xsl:with-param name="ie" select="$included-elt" />
</xsl:apply-templates>
</xsl:variable>
now returns the position() in $main-ndf instead of the element;
so the recursion call is simply
<xsl:with-param name="main-ndf"
select="$main-ndf[position() != $main-elt-pos]" />


All seems possible in xsl, just the time to find it that's long!
Jul 20 '05 #4

P: n/a

"Jean-Christophe Michel" <jc**************@online.fr> wrote in message
news:pa****************************@online.fr...
Hey,

ANswering myself to tell i found a solution:
<!-- find position of overriding element from main -->
<xsl:variable name="main-elt-pos">
<xsl:apply-templates select="$main-ndf"
mode="find-elem-in-main">
<xsl:with-param name="ie" select="$included-elt" />
</xsl:apply-templates>
</xsl:variable>


now returns the position() in $main-ndf instead of the element;
so the recursion call is simply
<xsl:with-param name="main-ndf"
select="$main-ndf[position() != $main-elt-pos]" />


All seems possible in xsl, just the time to find it that's long!


So you see how important it is to try to explain the problem well -- often
one also finds the solution while doing so ... :o)

Dimitre Novatchev.
FXSL developer, XML Insider,

http://fxsl.sourceforge.net/ -- the home of FXSL
Resume: http://fxsl.sf.net/DNovatchev/Resume/Res.html

Jul 20 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.