Go Back   Velocity Reviews > Newsgroups > XML
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

Reply

XML - Nested For-each

 
Thread Tools Search this Thread
Old 05-22-2006, 07:52 AM   #1
Default 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



NatDonin@gmail.com
  Reply With Quote
Old 05-22-2006, 08:59 AM   #2
George Bina
 
Posts: n/a
Default Re: Nested For-each

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"
xmlnssl="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>
<xsltherwise>
<xsl:call-template name="copyAndFlatten"/>
</xsltherwise>
</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

  Reply With Quote
Old 05-22-2006, 07:43 PM   #3
NatDonin@gmail.com
 
Posts: n/a
Default Re: Nested For-each

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

  Reply With Quote
Old 05-23-2006, 09:45 AM   #4
George Bina
 
Posts: n/a
Default Re: Nested For-each

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

  Reply With Quote
Old 05-24-2006, 09:33 AM   #5
Prady
 
Posts: n/a
Default Re: Nested For-each

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)

  Reply With Quote
Old 05-24-2006, 10:06 AM   #6
Nick Kew
 
Posts: n/a
Default Re: Nested For-each

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
  Reply With Quote
Old 05-24-2006, 01:20 PM   #7
Joe Kesselman
 
Posts: n/a
Default Re: Nested For-each

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
  Reply With Quote
Old 05-24-2006, 06:25 PM   #8
Stylus Studio
 
Posts: n/a
Default Re: Nested For-each

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

  Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump