Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to re-arrange xml content

Status
Not open for further replies.
Sep 5, 2006
37
0
0
US
Hi,

How can I rearrange contents of xml. For example in the following sample under <details> I have several <body> tags and each body tag contains <linenume> tag that shows the sequence of <body> tags. In the following sample I have <linenume>2</linenume> then <linenume>1</linenume> then <linenume>4</linenume> and finally <linenume>3</linenume>. I need <linenume>1</linenume> then <linenume>2</linenume> then <linenume>3</linenume> and finally <linenume>4</linenume> to be arrange in that sequence in the final output.


<?xml version="1.0" encoding="ISO-8859-1"?>
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<details>
<body>
<linenume>2</linenume>
<linevalue>222</linevalue>
<linedesc>Two</linedesc>
</body>
<body>
<linenume>1</linenume>
<linevalue>111</linevalue>
<linedesc>One</linedesc>
</body>
<body>
<linenume>4</linenume>
<linevalue>444</linevalue>
<linedesc>Four</linedesc>
</body>
<body>
<linenume>3</linenume>
<linevalue>333</linevalue>
<linedesc>Three</linedesc>
</body>
</details>
</note>

Here is what I need to have the final output. Any small sample will help alot.


<?xml version="1.0" encoding="ISO-8859-1"?>
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<details>
<body>
<linenume>1</linenume>
<linevalue>111</linevalue>
<linedesc>One</linedesc>
</body>
<body>
<linenume>2</linenume>
<linevalue>222</linevalue>
<linedesc>Two</linedesc>
</body>
<body>
<linenume>3</linenume>
<linevalue>333</linevalue>
<linedesc>Three</linedesc>
</body>
<body>
<linenume>4</linenume>
<linevalue>444</linevalue>
<linedesc>Four</linedesc>
</body>
</details>
</note>

thanks
 
You can use xslt to transform the xml document. In the xslt document, the critical ingredient is to use <xsl:sort> element to sort child nodes of "details" according to sub-node linenume. That gives you the keywords to google.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top