473,657 Members | 2,571 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1323


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:styleshe et
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="followi ng-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="followi ng-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*******@yaho o.de> wrote in message
news:Oc******** *****@TK2MSFTNG P12.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:styleshe et
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="followi ng-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="followi ng-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
2930
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 G23_NA17192.fsa rs7374540 A/C I23_Control.fsa rs7374540 C/C
3
3047
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 For Each varBookmark In Array("ReturnAddress", "NameAddressBlock", "Salutation", "FullName") If DLookup(varBookmark, "tblTemplate", "template_ID = " & strTid) = -1 Then
2
3389
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 Processing Code
8
3664
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 that it copies one block of data to another block of data until the block that is being copied contains a zero/null. the difference with this code is that it's doing 4bits at a time (all the values are 4bits) and the two blocks of data may not be...
3
3515
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, while the C# for loop ending condition is evaluated on every iteration." Is this accurate? I don't understand how you could get away without evaluating the ending condition at every iteration. Otherwise, how would you
6
71964
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
2585
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 memories are of but I think I just said loop somewhere inside the loop and it immediately jumped back to the start of the loop and began again. I can't seem to do that in .net. I this functionality available?
7
2907
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; test_fct(i); increment_fct(i) ) { ... // body of the loop }
2
5178
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. Is there any way so that I do not have to do that ? In VB6, I can set a recordset to new once, set it and execute it within the loop, and close it after the loop. Thanks. Dim m_cmdSQLT As SqlClient.SqlCommand Dim m_drSQLTop As...
8
6480
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 using a code loop and an INSERT INTO query. About 800,000 records of raw text. Later, I can then loop through and parse these 800,000 strings into usable data using more code. The problem I have is that the conversion of the text file, using a...
0
8392
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8305
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8823
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8503
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6163
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5632
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4151
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4301
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1607
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.