473,395 Members | 1,343 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 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 2276
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.