Nested For-each

N

NatDonin

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
 
G

George Bina

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:eek:therwise>
<xsl:call-template name="copyAndFlatten"/>
</xsl:eek:therwise>
</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
 
N

NatDonin

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
 
G

George Bina

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
 
P

Prady

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)
 
N

Nick Kew

Prady said:
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]
 
J

Joe Kesselman

Prady said:
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.)
 
S

Stylus Studio

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 said:
Prady said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

[SCHEMA]Is there any way...? 4
Tricky XSL question (?) 4
Question on Python Function 8
Ignoring Elements in XSD 0
XML to Struct 1
XSLT Needed??? 0
Recursive nested elements woe 3
Text File Parsing 7

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top