![]() |
|
|
|
#1 |
|
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 |
|
|
|
|
#2 |
|
Posts: n/a
|
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:template match="node() | @*"> <xsl:choose> <xsl:when test="count(*)=0 or self::File"> <xsl:call-template name="copyInside"/> </xsl:when> <xsl <xsl:call-template name="copyAndFlatten"/> </xsl </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 |
|
|
|
#3 |
|
Posts: n/a
|
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 |
|
|
|
#4 |
|
Posts: n/a
|
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 |
|
|
|
#5 |
|
Posts: n/a
|
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) |
|
|
|
#6 |
|
Posts: n/a
|
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 |
|
|
|
#7 |
|
Posts: n/a
|
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 |
|
|
|
#8 |
|
Posts: n/a
|
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 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 him some thanks. -- Tony Lavinio |
|