473,398 Members | 2,212 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,398 software developers and data experts.

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


Mar 10 '06 #1
2 1315


davidw wrote:
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?


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/
Mar 10 '06 #2
Nice! that is exactly I need, you are the man!
"Martin Honnen" <ma*******@yahoo.de> wrote in message
news:Oc*************@TK2MSFTNGP12.phx.gbl...


davidw wrote:
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?


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/

Mar 10 '06 #3

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

Similar topics

0
by: Charles Alexander | last post by:
Hello I am new to php & MySQL - I am trying to retrieve some records from a MySQL table and redisplay them. The data in list form looks like this: Sample_ID Marker_ID Variation ...
3
by: deko | last post by:
Problem: why doesn't this With block work? is it possible to have a For Each loop within a With block? With objWord .Documents.Add Template:=strTemplate, NewTemplate:=False, DocumentType:=0...
2
by: Sandy | last post by:
See line beginning with WHAT GOES HERE ..... Do Until ......................... Code .... .... If .............. Then WHAT GOES HERE TO JUMP TO THE END OF THE LOOP AND LOOP AGAIN End If...
8
by: ben | last post by:
i have a bit of code, that works absolutely fine as is, but seems over complicated/long winded. is there anyway to shorten/simplify it? the code is below. description of it: it's like strcpy in...
3
by: Ben R. | last post by:
In an article I was reading (http://www.ftponline.com/vsm/2005_06/magazine/columns/desktopdeveloper/), I read the following: "The ending condition of a VB.NET for loop is evaluated only once,...
6
by: John Pass | last post by:
What is the difference between a While and Do While/Loop repetition structure. If they is no difference (as it seems) why do both exist?
32
by: cj | last post by:
When I'm inside a do while loop sometimes it's necessary to jump out of the loop using exit do. I'm also used to being able to jump back and begin the loop again. Not sure which language my...
7
by: gmou | last post by:
Dear group, I am building a translator from C++ into VB (and into C#). At the moment, I have a hard time figuring out the equivalent of a 'for' loop in VB. Given C++ code: for( int i=0;...
2
by: fniles | last post by:
I am using .Net 2003 and querying a SQL Server 2000 database. I read the database in a loop, but on every iteration I set the SQLCommand to new, set it, execute it, dispose and set it to nothing....
8
by: SaltyBoat | last post by:
Needing to import and parse data from a large PDF file into an Access 2002 table: I start by converted the PDF file to a html file. Then I read this html text file, line by line, into a table...
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: 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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.