473,473 Members | 2,114 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

xsl to flatten xml nodes

Hi,

I have generated an xml document, and would like to be able to transform it
to another such that the contents of a chosen node type are flattened (i.e.
tags removed). e.g.

<shop>
<name>super</name>
<sells>
<drink>squash</drink>
<drink>beer</drink>
<food>
<fresh>bread</fresh>
<fresh>apples</fresh>
<frozen>peas</frozen>
</food>
</sells>
</shop>

apply 'flatten' to <sells> to give...

<shop>
<name>super</name>
<sells>
squash
beer
bread
apples
peas
</sells>
</shop>

This seems like the kind of thing I could use xsl for. Could someone please
give me a xsl example showing how this would be done?

Thanks for your time,
Simon
Jul 20 '05 #1
3 4194

"Simon" <no@no.no> wrote in message
news:c5**********@sparta.btinternet.com...
Hi,

I have generated an xml document, and would like to be able to transform it to another such that the contents of a chosen node type are flattened (i.e. tags removed). e.g.

<shop>
<name>super</name>
<sells>
<drink>squash</drink>
<drink>beer</drink>
<food>
<fresh>bread</fresh>
<fresh>apples</fresh>
<frozen>peas</frozen>
</food>
</sells>
</shop>

apply 'flatten' to <sells> to give...

<shop>
<name>super</name>
<sells>
squash
beer
bread
apples
peas
</sells>
</shop>

This seems like the kind of thing I could use xsl for. Could someone please give me a xsl example showing how this would be done?

This is quite straightforward. This transformation:

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*" />

<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>

<xsl:template match="*[ancestor::sells]">
<xsl:apply-templates/>
</xsl:template>

<xsl:template match="text()[ancestor::sells]">
<xsl:value-of select="concat('&#xA;',.)"/>
</xsl:template>

</xsl:stylesheet>

when applied on your source.xml:

<shop>
<name>super</name>
<sells>
<drink>squash</drink>
<drink>beer</drink>
<food>
<fresh>bread</fresh>
<fresh>apples</fresh>
<frozen>peas</frozen>
</food>
</sells>
</shop>

produces the wanted result:

<shop>
<name>super</name>
<sells>
squash
beer
bread
apples
peas</sells>
</shop>

Cheers,

Dimitre Novatchev [XML MVP],
FXSL developer, XML Insider,

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

Jul 20 '05 #2
> This is quite straightforward. This transformation:

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*" />

<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>

<xsl:template match="*[ancestor::sells]">
<xsl:apply-templates/>
</xsl:template>

<xsl:template match="text()[ancestor::sells]">
<xsl:value-of select="concat('&#xA;',.)"/>
</xsl:template>

</xsl:stylesheet>


Thanks for such a quick response! Is there any chance you could talk me
through it so I fully understand how it works? My xsl book hasn't arrived
yet!
Thanks again,
Simon
Jul 20 '05 #3


Simon wrote:
This is quite straightforward. This transformation:

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*" />

<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
That template above is an identity transformation, it copies nodes from
the source document to the result document.
Many XSLT transformations use that template if much of the source
document has to be copied over to the result document.
Now you only need to write templates for those elements that are not to
be copied.
<xsl:template match="*[ancestor::sells]">
<xsl:apply-templates/>
</xsl:template>
That template matches any element nodes whose ancestor is a <sells>
element, that is your <drink> or <food> or <fresh> element. For those
you don't want to copy the element but only their text content so all
the template does is call xsl:apply-templates on the child nodes.
<xsl:template match="text()[ancestor::sells]">
<xsl:value-of select="concat('&#xA;',.)"/>
</xsl:template>


This template then deals with all the text nodes which have a <sells>
element as an ancestor and outputs any of them on a line of its own.
--

Martin Honnen
http://JavaScript.FAQTs.com/

Jul 20 '05 #4

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

Similar topics

23
by: Francis Avila | last post by:
Below is an implementation a 'flattening' recursive generator (take a nested iterator and remove all its nesting). Is this possibly general and useful enough to be included in itertools? (I know...
0
by: Francis Avila | last post by:
A few days ago (see the 'itertools.flatten()?' thread from October 28) I became obsessed with refactoring a recursive generator that yielded the leaves of nested iterables. When the dust settled,...
10
by: bearophile | last post by:
This is my first Python program (it's an improvement of a recursive version by Luther Blissett). Given a list like this: , ]]] It produces the "flatted" version: I think this operation is...
3
by: Bengt Richter | last post by:
What am I missing? (this is from 2.4b1, so probably it has been fixed?) def flatten(list): l = for elt in list: ^^^^--must be expecting list instance or other sequence t = type(elt) if t...
18
by: Ville Vainio | last post by:
For quick-and-dirty stuff, it's often convenient to flatten a sequence (which perl does, surprise surprise, by default): ]]] -> One such implementation is at ...
181
by: Tom Anderson | last post by:
Comrades, During our current discussion of the fate of functional constructs in python, someone brought up Guido's bull on the matter: http://www.artima.com/weblogs/viewpost.jsp?thread=98196 ...
3
by: for.fun | last post by:
Hi everybody, I am looking for a XML comparison tool (I do not mean a standard char-by-char diff tool but a tool which understand XML syntax) More precisely, I can have serveral XML...
25
by: beginner | last post by:
Hi, I am wondering how do I 'flatten' a list or a tuple? For example, I'd like to transform or ] to . Another question is how do I pass a tuple or list of all the aurgements of a function to...
1
by: farooq.omar | last post by:
Is there a way to specify to Flatten() method as to how many points it should return. example I just want the Flatten() method to give back 100 points (basicallt gp.PointCount==100). Thanks
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...
1
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...
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 project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.