472,374 Members | 1,337 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,374 software developers and data experts.

stack values in xsl or substract element from tree

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
4 2197
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
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
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

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Allen | last post by:
Hi all, What are some different approaches to dealing with stack overflows in C++? I'm especially interested in approaches concerned with speed for infrequent overflows. -- Best wishes,...
12
by: Casper | last post by:
I've been told that structs, being value types, always reside on the stack and thus need not be deleted manually but simple go out of scope in due time. I have also read that all C++ *new*...
5
by: Patient Guy | last post by:
In my reading of the Strict and Transitional DTD for HTML 4.0, the table row (TR) elements are contained within table section elements: THEAD, TFOOT, and TBODY. The table section elements are...
16
by: michael | last post by:
Is it possible to get all href URLs contained in a unordered list and place them in an array? Or in fact two different arrays, differently named one for each <ul> group? <ul> <li><a...
20
by: Sushil | last post by:
Hi gurus I was reading FAQ "alloca cannot be written portably, and is difficult to implement on machines without a conventional stack." I understand that the standard does not mandate...
3
by: Robert Schuldenfrei | last post by:
Dear NG, I am making progress processing trees. I can "span" a tree if I make wise use of the C# stack methods, push() and pop(). What I want to place on the stack are rows from a data table. ...
23
by: Antoon Pardon | last post by:
Now slices are objects in python, I was wondering if slice notation will be usable outside subscribtion in the future. Will it ever be possible to write things like: a = 4:9 for key, value in...
5
by: Joel | last post by:
I would like to learn if there is any difference between the stack that MSIL uses for each method for executing instructions and the stack that it uses for storing Value types. Are they the same?...
9
by: Tarique | last post by:
Hello all.I am trying to implement a stack which can store either integer or float values. The code is given below: #include<stdio.h> #include<stdlib.h> #include<string.h> #define...
2
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines and...
1
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web server and have made sure to enable curl. I get a...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...
2
by: Ricardo de Mila | last post by:
Dear people, good afternoon... I have a form in msAccess with lots of controls and a specific routine must be triggered if the mouse_down event happens in any control. Than I need to discover what...
1
by: Johno34 | last post by:
I have this click event on my form. It speaks to a Datasheet Subform Private Sub Command260_Click() Dim r As DAO.Recordset Set r = Form_frmABCD.Form.RecordsetClone r.MoveFirst Do If...
1
by: ezappsrUS | last post by:
Hi, I wonder if someone knows where I am going wrong below. I have a continuous form and two labels where only one would be visible depending on the checkbox being checked or not. Below is the...
0
DizelArs
by: DizelArs | last post by:
Hi all) Faced with a problem, element.click() event doesn't work in Safari browser. Tried various tricks like emulating touch event through a function: let clickEvent = new Event('click', {...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.