Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old March 10th, 2006, 05:55 PM
davidw
Guest
 
Posts: n/a
Default How can I loop through XML like this? thanks

I have flat xml like this

<field name="a1"/>
<field name="a2" merge="true"/>
<field name="a3"/>
<field name="a4"/>
<field name="a5" merge="true"/>
<field name="a6" merge="true"/>
<field name="a7"/>

but I want to loop through them like this
<field name="a1">
<field name="a2" merge="true"/>
</field>
<field name="a3"/>
<field name="a4">
<field name="a5" merge="true"/>
<field name="a6" merge="true"/>
</field>
<field name="a7"/>

It means nodes with merge attribute is child of the previous node. I only
need handle one level for now, any suggestion?

thanks




  #2  
Old March 10th, 2006, 06:25 PM
Martin Honnen
Guest
 
Posts: n/a
Default Re: How can I loop through XML like this? thanks



davidw wrote:
[color=blue]
> I have flat xml like this
>
> <field name="a1"/>
> <field name="a2" merge="true"/>
> <field name="a3"/>
> <field name="a4"/>
> <field name="a5" merge="true"/>
> <field name="a6" merge="true"/>
> <field name="a7"/>
>
> but I want to loop through them like this
> <field name="a1">
> <field name="a2" merge="true"/>
> </field>
> <field name="a3"/>
> <field name="a4">
> <field name="a5" merge="true"/>
> <field name="a6" merge="true"/>
> </field>
> <field name="a7"/>
>
> It means nodes with merge attribute is child of the previous node. I only
> need handle one level for now, any suggestion?[/color]

You could apply an XSLT transformation to have the nested structure:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">

<xsl:output method="xml" indent="yes" />

<xsl:template match="fields">
<xsl:copy>
<xsl:apply-templates select="@*" />
<xsl:apply-templates select="field[not(@merge)]" />
</xsl:copy>
</xsl:template>

<xsl:template match="field">
<xsl:copy>
<xsl:apply-templates select="@*" />
<xsl:apply-templates
select="following-sibling::field[1][@merge = 'true']" mode="child" />
</xsl:copy>
</xsl:template>

<xsl:template match="field" mode="child">
<xsl:copy>
<xsl:apply-templates select="@* | node()" />
</xsl:copy>
<xsl:apply-templates
select="following-sibling::field[1][@merge = 'true']" mode="child" />
</xsl:template>

<xsl:template match="@*">
<xsl:copy />
</xsl:template>

</xsl:stylesheet>

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
  #3  
Old March 10th, 2006, 07:45 PM
davidw
Guest
 
Posts: n/a
Default Re: How can I loop through XML like this? thanks

Nice! that is exactly I need, you are the man!


"Martin Honnen" <mahotrash@yahoo.de> wrote in message
news:Octhb2GRGHA.424@TK2MSFTNGP12.phx.gbl...[color=blue]
>
>
> davidw wrote:
>[color=green]
> > I have flat xml like this
> >
> > <field name="a1"/>
> > <field name="a2" merge="true"/>
> > <field name="a3"/>
> > <field name="a4"/>
> > <field name="a5" merge="true"/>
> > <field name="a6" merge="true"/>
> > <field name="a7"/>
> >
> > but I want to loop through them like this
> > <field name="a1">
> > <field name="a2" merge="true"/>
> > </field>
> > <field name="a3"/>
> > <field name="a4">
> > <field name="a5" merge="true"/>
> > <field name="a6" merge="true"/>
> > </field>
> > <field name="a7"/>
> >
> > It means nodes with merge attribute is child of the previous node. I[/color][/color]
only[color=blue][color=green]
> > need handle one level for now, any suggestion?[/color]
>
> You could apply an XSLT transformation to have the nested structure:
>
> <?xml version="1.0" encoding="UTF-8"?>
> <xsl:stylesheet
> xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
> version="1.0">
>
> <xsl:output method="xml" indent="yes" />
>
> <xsl:template match="fields">
> <xsl:copy>
> <xsl:apply-templates select="@*" />
> <xsl:apply-templates select="field[not(@merge)]" />
> </xsl:copy>
> </xsl:template>
>
> <xsl:template match="field">
> <xsl:copy>
> <xsl:apply-templates select="@*" />
> <xsl:apply-templates
> select="following-sibling::field[1][@merge = 'true']" mode="child" />
> </xsl:copy>
> </xsl:template>
>
> <xsl:template match="field" mode="child">
> <xsl:copy>
> <xsl:apply-templates select="@* | node()" />
> </xsl:copy>
> <xsl:apply-templates
> select="following-sibling::field[1][@merge = 'true']" mode="child" />
> </xsl:template>
>
> <xsl:template match="@*">
> <xsl:copy />
> </xsl:template>
>
> </xsl:stylesheet>
>
> --
>
> Martin Honnen --- MVP XML
> http://JavaScript.FAQTs.com/[/color]


 

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

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

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles