473,394 Members | 1,714 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,394 software developers and data experts.

Nested For-each

Hi,
I'm new to XSLT and XML too, but I need your help with trying to create
an XSLT that will flatten my XML. I know I need to use nested loops
(for-each).
My XML file looks like this:

<File>
<Element1>
<Element2>
<Element3>
<Element31></Element31>
<Element32></Element32>
<Element4>
<Element41></Element41>
<Element42></Element42>
<Element5>
<Element51></Element51>
<Element52></Element52>
</Element5>

<Element6>
<Element61> Data1 </Element61>
<Element62> Data1 </Element62>
</Element6>

<Element6>
<Element61>Data2</Element61>
<Element62>Data2</Element62>
</Element6>
</Element4>
<Element4>
<Element41></Element41>
<Element42></Element42>
<Element5>
<Element51></Element51>
<Element52></Element52>
</Element5>

<Element6>
<Element61> Data3 </Element61>
<Element62> Data3 </Element62>
</Element6>
<Element6>
<Element61> Data4 </Element61>
<Element62> Data4 </Element62>
</Element6>
<Element6>
<Element61> Data5 </Element61>
<Element62> Data5 </Element62>
</Element6>

</Element4>
</Element3>
<Element3>
<Element31></Element31>
<Element32></Element32>
<Element4>
<Element41></Element41>
<Element42></Element42>
<Element5>
<Element51></Element51>
<Element52></Element52>
</Element5>

<Element6>
<Element61></Element61>
<Element62></Element62>
</Element6>

<Element6>
<Element61></Element61>
<Element62></Element62>
</Element6>
</Element4>
<Element4>
<Element41></Element41>
<Element42></Element42>
<Element5>
<Element51></Element51>
<Element52></Element52>
</Element5>

<Element6>
<Element61></Element61>
<Element62></Element62>
</Element6>
<Element6>
<Element61></Element61>
<Element62></Element62>
</Element6>
<Element6>
<Element61></Element61>
<Element62></Element62>
</Element6>
</Element4>
</Element3>
</File>

What I need to get is XML that looks like below:
<File>
<Element6>
<Element1>
<Element2>
<Element31></Element31>
<Element32></Element32>
<Element41></Element41>
<Element42></Element42>
<Element51></Element51>
<Element52></Element52>
<Element61> Data1 </Element61>
<Element62> Data1 </Element62>
</Element6>
<Element6>
<Element1>
<Element2>
<Element31></Element31>
<Element32></Element32>
<Element41></Element41>
<Element42></Element42>
<Element51></Element51>
<Element52></Element52>
<Element61> Data2 </Element61>
<Element62> Data2 </Element62>
</Element6>
<Element6>
<Element1>
<Element2>
<Element31></Element31>
<Element32></Element32>
<Element41></Element41>
<Element42></Element42>
<Element51></Element51>
<Element52></Element52>
<Element61> Data3 </Element61>
<Element62> Data3 </Element62>
</Element6>
<Element6>
<Element1>
<Element2>
<Element31></Element31>
<Element32></Element32>
<Element41></Element41>
<Element42></Element42>
<Element51></Element51>
<Element52></Element52>
<Element61> Data4 </Element61>
<Element62> Data4 </Element62>
</Element6>
...etc..
</File>
Appreciate any input!

Natalie

May 22 '06 #1
7 1328
Hi Natalie,

No, in general flattening a structure is not done in XLST with for-each
loops but using the template rules. Now I cannot understand the
algorithm from your example but in case you want for instance something
like

<File>
<Element1>
<Element2>
<Element3>
<Element31/>
<Element32/>
<Element4>
<Element41/>
<Element42/>
<Element5>
<Element51/>
<Element52/>
</Element5>
<Element6>
<Element61> Data1 </Element61>
<Element62> Data1 </Element62>
</Element6>
<Element6>
<Element61>Data2</Element61>
<Element62>Data2</Element62>
</Element6>
</Element4>
</Element3>
</Element2>
</Element1>
</File>

to be converted to

<?xml version="1.0" encoding="UTF-8"?>
<File>
<Element1/>
<Element2/>
<Element3/>
<Element31/>
<Element32/>
<Element4/>
<Element41/>
<Element42/>
<Element5/>
<Element51/>
<Element52/>

<Element6/>
<Element61> Data1 </Element61>
<Element62> Data1 </Element62>

<Element6/>
<Element61>Data2</Element61>
<Element62>Data2</Element62>
</File>

then you can use a stylesheet like below:

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

<xsl:template match="node() | @*">
<xsl:choose>
<xsl:when test="count(*)=0 or self::File">
<xsl:call-template name="copyInside"/>
</xsl:when>
<xsl:otherwise>
<xsl:call-template name="copyAndFlatten"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>

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

<xsl:template name="copyAndFlatten">
<xsl:copy>
<xsl:apply-templates select="@*"/>
</xsl:copy>
<xsl:apply-templates select="node()"/>
</xsl:template>
</xsl:stylesheet>

Best Regards,
George
---------------------------------------------------------------------
George Cristian Bina
<oXygen/> XML Editor, Schema Editor and XSLT Editor/Debugger
http://www.oxygenxml.com

May 22 '06 #2
Hi George! Thanks so much for your input!
Your XSLT works best if I want to convert the entire file into the
simple elements - completely flattened file. In my case I need to have
an XML that will have a set of repeated <Element6>'s with Element1,
Element2, Element3,Element4 and Element5 with all their children as
part of the Element6, so it would look like this:
<File>
<Element6>
<Element1/>
<Element2/>
< --- <Element3/> --don't need the header element in the
final XML --!>
<Element31/>
<Element32/>
< --- <Element4/> --don't need the header element in the
final XML --!>
<Element41/>
<Element42/>
< --- <Element5/> --don't need the header element in the
final XML --!>
<Element51/>
<Element52/>
<Element61> Data1 </Element61>
<Element62> Data1 </Element62>

</Element6>
.....
<Element6>
....
</Element6>
Please also consider the following:
Element1 and Element2 have one instance per <File>, while Element3 and
Element4 can be multiples. Element3 contains its children and complex
Element4. Element4 contains complex Element5 which is only used once
within the Element4 and multiple instances of Element6. Element6 should
have children of Element5 repeated for each instance of Element6.
Basically what I'm trying to do is to build the record so I can use it
for SQL XML insert into the database.
Hope it makes sense. Thanks so much in advance!

Natalie

May 22 '06 #3
Hi Natalie,

Sorry, that's to much for me to follow... The idea was that you should
not use for-each but use instead the XSLT template rules.
You can just add specific rules for each element and decide there if
you want to copy its content inside it or to skip the element and only
copy its content or to output that element without it content and then
the content as following siblings.

Best Regards,
George
---------------------------------------------------------------------
George Cristian Bina
<oXygen/> XML Editor, Schema Editor and XSLT Editor/Debugger
http://www.oxygenxml.com

May 23 '06 #4
Hi Natalie,

If you are really new to XML and XSLT then you must consider using
Stylus Studio Enterprise edition for the purpose. It comes with a very
powerfull WYSIWIG editor to help you out with your problems.

I was strucked with a similar problem and found Stylus Studio immensely
helpful. Even learning Stylus Studio is very easy by dint of their
audio/video tutorials. You can dowload an evaluation copy and see it
solving your problems.

To know more please visit Stylus Studio Website
(http://www.stylusstudio.com/). It's definitely going to help you
rather than waiting for replies to your posts.

Regards,
Pradyumna Roy
http://www.stylusstudio.com/ (XML productivity through innovation)

May 24 '06 #5
Prady wrote:
Hi Natalie,

If you are really new to XML and XSLT then you must consider using
[spammer name deleted]


Please don't use or recommend spammers.

[looks like the spammer itself under a new posting name. Unless
there's more than one of them]

--
Nick Kew
May 24 '06 #6
Prady wrote:
If you are really new to XML and XSLT then you must consider using
Stylus Studio Enterprise edition for the purpose.


Just for clarification, Prady: Do you have a business association
Stylus, or are you just an enthusiastic customer?

(I have no objection to folks enthusing about their own products, but
it's useful to know when that's what's going on.)

That reminds me: I know that folks were experimenting with an
interactive XSLT debugger for Eclipse, but I'm not sure whether that
ever got released. I should check. (No, I don't have a business
relationship with that product, but I do have one with the Apache Xalan
XSLT processor they were using.)

--
() ASCII Ribbon Campaign | Joe Kesselman
/\ Stamp out HTML e-mail! | System architexture and kinetic poetry
May 24 '06 #7
Well, I *do* work for DataDirect on the Stylus Studio team, and I
checked both our employee database and with our marketing people, and
Prady ain't us.

He did copy our tag line; I guess he's happy.

Although we aren't shy about promoting the product (like this:
http://www.stylusstudio.com/buy :) ), we make it clear when we are the
ones posting.

Nick Kew wrote:
Prady wrote:
If you are really new to XML and XSLT then you must consider using
http://www.stylusstudio.com/


Please don't use or recommend spammers.

[looks like the spammer itself under a new posting name. Unless
there's more than one of them]


I think you owe Prady an apology ;), and our marketing department owes
him some thanks.

-- Tony Lavinio

May 24 '06 #8

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

Similar topics

0
by: Glen | last post by:
I have a Struts action form which contains a bean. I am trying to display a bean retrieved from the database in this form using the nested tag. Can anyone help me? I continue to get an error...
6
by: Andy Baker | last post by:
Hi there, I'm learning Python at the moment and trying to grok the thinking behind it's scoping and nesting rules. I was googling for nested functions and found this Guido quote:...
8
by: Robert W. | last post by:
I've almost completed building a Model-View-Controller but have run into a snag. When an event is fired on a form control I want to automatically updated the "connnected" property in the Model. ...
1
by: Tomas Sieger | last post by:
Hi all, I'm in doubt with the following code: class Base { public: class Nested {}; }; class Derived:public Base { public: class Nested {
77
by: Peter Olcott | last post by:
http://www.tommti-systems.de/go.html?http://www.tommti-systems.de/main-Dateien/reviews/languages/benchmarks.html The above link shows that C# is 450% slower on something as simple as a nested loop....
6
by: deko | last post by:
How do I construct an XHTML-compliant nested unordered list? This displays correctly (both FF and IE): <ul> <li>list item</li> <li>list item</li> <li>list item</li> <ul> <li>nested list...
7
by: patrick j | last post by:
Hi I'm wondering about lists with nested lists as one does on a Saturday afternoon. Anyway below is an example of a list with a nested list which the iCab browser's very useful HTML...
3
by: jdurancomas | last post by:
Dear all, I'm trying to declare the operator++ to a nested class. The nested class is not template but the container it is. The code used in teh sample program is included bellow: ...
5
by: Calvin Spealman | last post by:
On Wed, Aug 13, 2008 at 11:32 AM, Cousson, Benoit <b-cousson@ti.comwrote: There is no point of nested classes because nested classes _are not_ supported by python. They are simply an artifact of...
3
by: Cousson, Benoit | last post by:
I don't think so; my original email was mainly a question. I do agree that they are other ways to do what I'm trying to achieve; there are always several ways to solve an issue. Few days ago, I...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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: 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:
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
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.