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

XML sorting

People must get sick of answering questions on sorting, but I haven't found an example on the web that matches my situation (Maybe I am asking the wrong question). My XML file has this sort of structure
Expand|Select|Wrap|Line Numbers
  1. <?xml version="1.0"?>
  2. <Building>
  3.     <Block>
  4.         <Id>Ba2150</Id>
  5.         <Elevation>140</Elevation>
  6.         <Piece>
  7.             <Description>Rustic</Description>
  8.             <Elevation>180</Elevation>
  9.             <Qty>1</Qty>
  10.         </Piece>
  11.         <Piece>
  12.             <Description>Rustic</Description>
  13.             <Elevation>1870</Elevation>
  14.             <Qty>1</Qty>
  15.         </Piece>
  16.         <Piece>
  17.             <Description>Red</Description>
  18.             <Elevation>500</Elevation>
  19.             <Qty>1</Qty>
  20.         </Piece>
  21.         <Piece>
  22.             <Description>Blue</Description>
  23.             <Elevation>1430</Elevation>
  24.             <Qty>1</Qty>
  25.         </Piece>
  26.         <Piece>
  27.             <Description>Side</Description>
  28.             <Elevation>140</Elevation>
  29.             <Qty>2</Qty>
  30.         </Piece>
  31.         <Piece>
  32.             <Description>Bottom</Description>
  33.             <Elevation>140</Elevation>
  34.             <Qty>1</Qty>
  35.         </Piece>
  36.         <Piece>
  37.             <Description>Back</Description>
  38.             <Elevation>140</Elevation>
  39.             <Qty>1</Qty>
  40.         </Piece>
  41.         <Piece>
  42.             <Description>Top</Description>
  43.             <Elevation>2190</Elevation>
  44.             <Qty>1</Qty>
  45.         </Piece>
  46.     </Block>
  47. </Building>
All I want to do is sort the pieces within the blocks and output the whole lot. I can sort the blocks, I can sort the pieces and lose the blocks and I can sort the pieces and keep the block tags, but not the block data. My current xsl is

Expand|Select|Wrap|Line Numbers
  1. <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  2.   <xsl:output method="xml" indent="yes"/>
  3.  
  4.  <xsl:template match="Building">
  5.  <xsl:copy>
  6.       <xsl:apply-templates select="Block">
  7.     <xsl:sort select="Elevation" data-type="number"/> <!-- not really necessary, already sorted -->
  8.      </xsl:apply-templates>
  9. </xsl:copy>
  10. </xsl:template>
  11.  
  12.   <xsl:template match="Block">
  13.     <xsl:copy>
  14.     <xsl:apply-templates select="Piece">
  15.         <xsl:sort select="Elevation" data-type="number"/>
  16.     </xsl:apply-templates>
  17.     </xsl:copy>
  18.   </xsl:template>
  19.  
  20.   <xsl:template match="@* | node()">
  21.     <xsl:copy>
  22.       <xsl:apply-templates select="@* | node()"/>
  23.     </xsl:copy>
  24.   </xsl:template>
  25. </xsl:stylesheet>
Any help would be greatly appreciated
Feb 19 '10 #1

✓ answered by pisicuta

I managed to get the answer from somewhere else. For all those interested, here it is.

Expand|Select|Wrap|Line Numbers
  1. <xsl:stylesheet version="1.0"
  2.   xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
  3.   <xsl:template match="Building">
  4.     <xsl:copy>
  5.       <xsl:copy-of select="@*" /> <!-- copy attributes-->
  6.       <xsl:apply-templates />
  7.     </xsl:copy>
  8.   </xsl:template>
  9.  
  10.   <xsl:template match="Block">
  11.     <xsl:copy>
  12.       <xsl:copy-of select="@*" /> <!-- copy attributes-->
  13.       <xsl:copy-of select="*[local-name()!='Piece']" />
  14.       <xsl:for-each select="Piece">
  15.         <!-- http://www.w3.org/TR/xslt#sorting -->
  16.         <xsl:sort select="Elevation" data-type="number" order="ascending" />
  17.         <xsl:copy-of select="." />
  18.       </xsl:for-each>
  19.     </xsl:copy>
  20.   </xsl:template>
  21.  
  22.     <xsl:template match="Panel">
  23.         <xsl:copy-of select="." />
  24.   </xsl:template>
  25. </xsl:stylesheet>

6 2408
Dormilich
8,658 Expert Mod 8TB
just for a better understanding, what shall it look like afterwards?
Feb 19 '10 #2
Sorry, I didn't it clear what the output should be. The output should look like this, so the Piece elements are in ascending elevation order:

Expand|Select|Wrap|Line Numbers
  1. <?xml version="1.0"?>
  2. <Building>
  3.     <Block>
  4.         <Id>Ba2150</Id>
  5.         <Elevation>140</Elevation>
  6.         <Piece>
  7.             <Description>Side</Description>
  8.             <Elevation>140</Elevation>
  9.             <Qty>2</Qty>
  10.         </Piece>
  11.         <Piece>
  12.             <Description>Bottom</Description>
  13.             <Elevation>140</Elevation>
  14.             <Qty>1</Qty>
  15.         </Piece>
  16.         <Piece>
  17.             <Description>Back</Description>
  18.             <Elevation>140</Elevation>
  19.             <Qty>1</Qty>
  20.         </Piece>
  21.         <Piece>
  22.             <Description>Rustic</Description>
  23.             <Elevation>180</Elevation>
  24.             <Qty>1</Qty>
  25.         </Piece>
  26.         <Piece>
  27.             <Description>Red</Description>
  28.             <Elevation>500</Elevation>
  29.             <Qty>1</Qty>
  30.         </Piece>
  31.         <Piece>
  32.             <Description>Blue</Description>
  33.             <Elevation>1430</Elevation>
  34.             <Qty>1</Qty>
  35.         </Piece>        
  36.         <Piece>
  37.             <Description>Rustic</Description>
  38.             <Elevation>1870</Elevation>
  39.             <Qty>1</Qty>
  40.         </Piece>
  41.         <Piece>
  42.             <Description>Top</Description>
  43.             <Elevation>2190</Elevation>
  44.             <Qty>1</Qty>
  45.         </Piece>
  46.     </Block>
  47. </Building>
Feb 19 '10 #3
Dormilich
8,658 Expert Mod 8TB
maybe Munchian sorting can help.
Feb 19 '10 #4
Spent all day with Munchian sorting and am no further forward. In fact I've probably got a bigger headache. I just can't get any elements copied from the <Piece>..</Piece> nodes and have the Block details as well. I end up with:

Expand|Select|Wrap|Line Numbers
  1. <Building>
  2.     <Block>
  3.         <Id>Ba2150</Id>
  4.         <Elevation>140</Elevation>
  5.                 <Piece />
  6.         <Piece />
  7.                 <Piece />
  8.         <Piece />
  9.                 <Piece />
  10.         <Piece />
  11.                 <Piece />
  12.         <Piece />
  13.     <Block>
  14. <Building>
Feb 19 '10 #5
Dormilich
8,658 Expert Mod 8TB
I wish jkmyoung were here, he’s the sort & copy expert …

I’ll try if I can shed some light on it, but it will take some time.
Feb 19 '10 #6
I managed to get the answer from somewhere else. For all those interested, here it is.

Expand|Select|Wrap|Line Numbers
  1. <xsl:stylesheet version="1.0"
  2.   xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
  3.   <xsl:template match="Building">
  4.     <xsl:copy>
  5.       <xsl:copy-of select="@*" /> <!-- copy attributes-->
  6.       <xsl:apply-templates />
  7.     </xsl:copy>
  8.   </xsl:template>
  9.  
  10.   <xsl:template match="Block">
  11.     <xsl:copy>
  12.       <xsl:copy-of select="@*" /> <!-- copy attributes-->
  13.       <xsl:copy-of select="*[local-name()!='Piece']" />
  14.       <xsl:for-each select="Piece">
  15.         <!-- http://www.w3.org/TR/xslt#sorting -->
  16.         <xsl:sort select="Elevation" data-type="number" order="ascending" />
  17.         <xsl:copy-of select="." />
  18.       </xsl:for-each>
  19.     </xsl:copy>
  20.   </xsl:template>
  21.  
  22.     <xsl:template match="Panel">
  23.         <xsl:copy-of select="." />
  24.   </xsl:template>
  25. </xsl:stylesheet>
Feb 22 '10 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: dont bother | last post by:
This is really driving me crazy. I have a dictionary feature_vectors{}. I try to sort its keys using #apply sorting on feature_vectors sorted_feature_vector=feature_vectors.keys()...
0
by: ck388 | last post by:
For some reason when I enable the callback feature of the gridview I still get a page refresh, that is it seems like there is a postback that occurs, not a callback which is just supposed to update...
7
by: Federico G. Babelis | last post by:
Hi All: I have this line of code, but the syntax check in VB.NET 2003 and also in VB.NET 2005 Beta 2 shows as unknown: Dim local4 As Byte Fixed(local4 = AddressOf dest(offset)) ...
19
by: Owen T. Soroke | last post by:
Using VB.NET I have a ListView with several columns. Two columns contain integer values, while the remaining contain string values. I am confused as to how I would provide functionality to...
10
by: Sjaakie | last post by:
Hi, I'm, what it turns out to be, fooling around with 3-tier design. At several websites people get really enthusiastic about using custom dataobjects instead of datasets/-tables. While trying to...
4
by: Ambica Jain | last post by:
Hi, I want custom sorting on some of the columns in the datagrid. And i am able to do the same by overriding MouseDown event. However, i need to rebind my datatable to reflect the changes in...
7
by: Kamal | last post by:
Hello all, I have a very simple html table with collapsible rows and sorting capabilities. The collapsible row is hidden with css rule (display:none). When one clicks in the left of the...
1
KevinADC
by: KevinADC | last post by:
Introduction In part one we discussed the default sort function. In part two we will discuss more advanced techniques you can use to sort data. Some of the techniques might introduce unfamiliar...
5
by: lemlimlee | last post by:
hello, this is the task i need to do: For this task, you are to develop a Java program that allows a user to search or sort an array of numbers using an algorithm that the user chooses. The...
5
by: jrod11 | last post by:
hi, I found a jquery html table sorting code i have implemented. I am trying to figure out how to edit how many colums there are, but every time i remove code that I think controls how many colums...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.