473,395 Members | 1,608 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.

Combining XML elements with XSL

I am having trouble combining similar elements. Elements can be
combined if they have the same name and the same attribute values. I
can handle single level elements but am having problems with
multi-level elments (level of element nesting is unbound). I cannot
rely on fixed element names in the translator code since the translator
will be general purpose, elements names are not fixed.

Example input

<test>
<A x="0">
<B>1</B>
</A>
<A x="0">
<C>2</C>
</A>
<A x="0">
<D x="0">
<E>3</E>
</D>
</A>
<A x="0">
<D x="0">
<E>3</E>
</D>
</A>
<A x="0">
<D x="1">
<E>5</E>
</D>
</A>
<A x="1">
<B>4</B>
</A>
</test>

example output:

<test>
<A x="0">
<B>1</B>
<C>2</C>
<D x="0">
<E>3</E>
</D>
<D x="1">
<E>5</E>
</D>
</A>
<A x="1">
<B>4</B>
</A>
</test>

Any help would be greatly appreciated - still an XSL newbie

Jul 20 '05 #1
8 4144
On 15 Feb 2005 13:50:10 -0800, mikea_59 <mi******@yahoo.com> wrote:
I am having trouble combining similar elements. Elements can be
combined if they have the same name and the same attribute values. I
can handle single level elements but am having problems with
multi-level elments (level of element nesting is unbound). I cannot
rely on fixed element names in the translator code since the translator
will be general purpose, elements names are not fixed.

Hi,

I'm afraid you'll have to use an 'xx:node-set' extension function to solve
this (in XSLT1.0).

Here's one solution (tested with Saxon), gives correct output for your
input.
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0" xmlns:exsl="http://exslt.org/common"
extension-element-prefixes="exsl">

<xsl:output method="xml" indent="yes"/>

<xsl:strip-space elements="*"/>
<xsl:key name="element" match="*" use="concat(local-name(),@*)"/>

<xsl:template match="*">
<xsl:copy>
<xsl:copy-of select="@*"/>
<xsl:variable name="set">
<xsl:copy-of select="key('element',concat(local-name(),@*))/*"/>
</xsl:variable>
<xsl:apply-templates select="text()"/>
<xsl:apply-templates
select="exsl:node-set($set)/*[generate-id()=generate-id(key('element',concat(local-name(),@*)))]"/>
</xsl:copy>
</xsl:template>

</xsl:stylesheet>

regards,
Joris Gillis
--
Using Opera's revolutionary e-mail client: http://www.opera.com/m2/
Jul 20 '05 #2

That worked - thanks a lot! What a great NG.

So, is this strictly a 1.0 solution? Doesn't 2.0 eliminate the need
to use node-set() by enabling temporary templates? Is node-set() even
available in 2.0 - in Saxon? How would I change this for 2.0?

Sorry for all the ???

Thanks again
mikea

Jul 20 '05 #3
On 17 Feb 2005 11:56:09 -0800, mikea_59 <mi******@yahoo.com> wrote:
So, is this strictly a 1.0 solution? Everything that can be achieved in XSLT1.0 certainly can in XSLT2.0, isn't
that logic;)
Doesn't 2.0 eliminate the need
to use node-set() by enabling temporary templates? Excerpt form the latest XSLT2.0 working draft:
"The result tree fragment data-type is eliminated. A variable-binding
element with content (and no as attribute) now constructs a temporary
tree, and the value of the variable is the root node of this tree (see 9.3
Values of Variables and Parameters). With an as attribute, a
variable-binding element may be used to construct an arbitrary sequence.
These features eliminate the need for the xx:node-set extension function
provided by many XSLT 1.0 implementations."
available in 2.0 - in Saxon? How would I change this for 2.0?


I don't have experience with 2.0, but I guess writing '$set' in stead of
'exsl:node-set($set)' will do...
regards,
Joris Gillis
--
Using Opera's revolutionary e-mail client: http://www.opera.com/m2/
Jul 20 '05 #4

Joris Gillis wrote:
On 17 Feb 2005 11:56:09 -0800, mikea_59 <mi******@yahoo.com> wrote:
So, is this strictly a 1.0 solution? Everything that can be achieved in XSLT1.0 certainly can in XSLT2.0,

isn't that logic;)
Yes, very logical, but when I set stylesheet to version=2.0 and
re-run the translation in Saxon, I get an error:

"A sequence of more than one item is not allowed as the first
argument of generate-id()"

I guess it's not related to node-set()
Doesn't 2.0 eliminate the need
to use node-set() by enabling temporary templates? Excerpt form the latest XSLT2.0 working draft:
"The result tree fragment data-type is eliminated. A variable-binding

element with content (and no as attribute) now constructs a temporary tree, and the value of the variable is the root node of this tree (see 9.3 Values of Variables and Parameters). With an as attribute, a
variable-binding element may be used to construct an arbitrary sequence. These features eliminate the need for the xx:node-set extension function provided by many XSLT 1.0 implementations."
available in 2.0 - in Saxon? How would I change this for 2.0?
I don't have experience with 2.0, but I guess writing '$set' in stead

of 'exsl:node-set($set)' will do...
regards,
Joris Gillis
--
Using Opera's revolutionary e-mail client: http://www.opera.com/m2/


Jul 20 '05 #5
On 17 Feb 2005 13:21:49 -0800, mikea_59 <mi******@yahoo.com> wrote:
"A sequence of more than one item is not allowed as the first
argument of generate-id()"


Really? Then they have changed the behaviour of generate-id().

Solution:
change
<xsl:apply-templates
select="exsl:node-set($set)/*[generate-id()=generate-id(key('element',concat(local-name(),@*)))]"/>
into
<xsl:apply-templates
select="exsl:node-set($set)/*[generate-id()=generate-id(key('element',concat(local-name(),@*))[1])]"/>

--
Using Opera's revolutionary e-mail client: http://www.opera.com/m2/
Jul 20 '05 #6
Really? Then they have changed the behaviour of generate-id().


it's a more or less pervasive change, as part of the stricter tying
introduced in 2 most instances of functions that expect a single node
complain if given more than one node rather than silently taking the
first in doc order.

David
Jul 20 '05 #7

It looks like that did the trick, thanks a lot. I'm still not sure I
follow the syntax of the select attribute, I guess I'll go back to my
Kay book...

Thanks again,
mikea

Jul 20 '05 #8
Tempore 23:15:45, die Thursday 17 February 2005 AD, hinc in foro {comp.text.xml} scripsit mikea_59 <mi******@yahoo.com>:

It looks like that did the trick, thanks a lot. I'm still not sure I
follow the syntax of the select attribute


you mean this line?

<xsl:apply-templates select="$set/*[generate-id()=generate-id(key('element',concat(local-
name(),@*))[1])]"/>

well, the trick is to let the xslt processor create a key lookup table based on the node-set in the variable in stead of a lookup table indexing all nodes in the input XML document.

The 'generate-id()' + 'key()' stuff is just basic Muenchian grouping technique...

--
Joris Gillis (http://www.ticalc.org/cgi-bin/acct-v...i?userid=38041)
"Φιλήκοον ειναι μαλλον η Ï*ιλόλαλον" - Κλεόβουλος
Jul 20 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: CES | last post by:
All, I sorry for the 101 question but how do you combine 2 var into a document statement i.e. This Line works:
2
by: Chris Mullins | last post by:
I've spent a bit of time over the last year trying to implement RFC 3454 (Preparation of Internationalized Strings, aka 'StringPrep'). This RFC is also a dependency for RFC 3491...
10
by: mike | last post by:
If I have 2 object arrays like: var txtobj = theform.getElementsByTagName("input"); var selobj = theform.getElementsByTagName("select"); and i want to iterate over them I'd like to combine...
3
by: IRAS Blues | last post by:
Hi all, I've got a form that consists of a bunch of textboxes and also file upload inputs. For both sets of fields, I need to be able to add in additional elements on the fly. This is done by an...
2
by: Stefan | last post by:
Hi there! I'm very new to XML and XSLT, so while working i encountered the following, probably very easy to solve, problem. i've got 2 XML-files, which look like this: LinesOfCode.xml:...
11
by: Alan | last post by:
Is there an easy and efficient way to combine two <vector>s, rather than taking each element from one and adding it to the other? I haven`t been able to find any guidance on or examples of this...
4
by: nallen05 | last post by:
Is there a standardized recommendation for combining names and namespaces into a single URI? I found a post on the Stylus Studio forum asking the same question, the response was "use James...
1
by: TAL651 | last post by:
Hello, I am using XML to create a library of files, which I then display as a table using XSL. This works fine, but I would like the last column to be a link to the file. Rather than store the...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing,...

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.