473,372 Members | 1,027 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,372 software developers and data experts.

Sort xml and output xml

I have this xml-file. I want to sort it, and create a new xml-file. The
result should be identical to the input except that it's sorted.

<?xml version="1.0" encoding="UTF-8"?>
<levelone>
<child ID="1" sort="5"><name>Paul</name></child>
<child ID="2" sort="1"><name>Adam</name></child>
<child ID="3" sort="2"><name>Will</name></child>
<root>

I now want to apply an xslt file and have the following output:
<?xml version="1.0" encoding="UTF-8"?>
<levelone>
<child ID="2" sort="1"><name>Adam</name></child>
<child ID="3" sort="2"><name>Will</name></child>
<child ID="1" sort="5"><name>Paul</name></child>
<root>

I've been searching for hours, but all the examples I have seen creates an
HTML file.

tia
/jim
Dec 5 '06 #1
10 7105

Jim Andersen wrote:
I have this xml-file. I want to sort it, and create a
new xml-file. The result should be identical to the
input except that it's sorted.
Since you failed to mention what you're using for
processing, I assume it's some XSLT 1.0-compliant
processor.
<?xml version="1.0" encoding="UTF-8"?>
<levelone>
<child ID="1" sort="5"><name>Paul</name></child>
<child ID="2" sort="1"><name>Adam</name></child>
<child ID="3" sort="2"><name>Will</name></child>
<root>
That's not an XML file. It's not well-formed.
I've been searching for hours, but all the examples I
have seen creates an HTML file.
That's mighty strange, especially since XML is the
default output method. All the XSLT tutorials I remember
covered sorting, identity transformation and controlling
your output.

Presuming you meant something like:

<levelone>
<child ID="1" sort="5"><name>Paul</name></child>
<child ID="2" sort="1"><name>Adam</name></child>
<child ID="3" sort="2"><name>Will</name></child>
</levelone>

The following XSLT should do the trick:

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template
match="*[@sort][not(preceding-sibling::*[@sort])]">
<xsl:apply-templates select="../*[@sort]" mode="copy">
<xsl:sort select="@sort"/>
</xsl:apply-templates>
</xsl:template>
<xsl:template
match="*[@sort][preceding-sibling::*[@sort]]"/>
<xsl:template match="node()|@*" mode="copy">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>

--
Pavel Lepin

Dec 5 '06 #2
p.*****@ctncorp.com wrote:
Since you failed to mention what you're using for
processing
I'm using a .NET 2.0 XMLDataSource. I point it to my xml-file and my
xslt-file.
>, I assume it's some XSLT 1.0-compliant
processor.
><?xml version="1.0" encoding="UTF-8"?>
<levelone>
<child ID="1" sort="5"><name>Paul</name></child>
<child ID="2" sort="1"><name>Adam</name></child>
<child ID="3" sort="2"><name>Will</name></child>
<root>

That's not an XML file. It's not well-formed.
Yes. A typo in my posting. The last element "root" should have been
"levelone"

>I've been searching for hours, but all the examples I
have seen creates an HTML file.

That's mighty strange, especially since XML is the
default output method.
And now I found out why. I had (from one of the examples I found) learned
that I could also put an instruction in the XML document that said to always
use a specific XSL-file. Something along the lines of : <xsl-stylesheet
output="htm/txt">
Removing that line made the output into XML.
Presuming you meant something like:

<levelone>
<child ID="1" sort="5"><name>Paul</name></child>
<child ID="2" sort="1"><name>Adam</name></child>
<child ID="3" sort="2"><name>Will</name></child>
</levelone>
I did.
The following XSLT should do the trick:
And it did. Thx a heap. Now I can start to find out why :-)

/jim
Dec 5 '06 #3
This should be trivial. See, for example, http://www.w3.org/TR/xslt#sorting

XSLT doesn't output HTML unless you tell it to output HTML...

Dec 5 '06 #4
Joseph Kesselman wrote:
This should be trivial. See, for example,
http://www.w3.org/TR/xslt#sorting
As you can see from mr p.lepins posting the solution is far from "trivial".
XSLT doesn't output HTML unless you tell it to output HTML...
An unfortunately I (unknowingly) did :-)

/jim
Dec 6 '06 #5
Jim Andersen wrote:
As you can see from mr p.lepins posting the solution is far from "trivial".
I'm not sure I agree; that solution strikes me as overcomplicated for
the question you asked.

"Produce a new levelone document, copying the child elements in sorted
order."

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template select="/levelone">
<levelone>
<xsl:for-each select="child">
<xsl:sort select="@sort"/>
<xsl:copy-of select=".">
</xsl:apply-templates>
</levelone>
</xsl:template>

</xsl:stylesheet>

Yes, things may become more complicated when additional constraints are
applied... but this ought to do the job you asked for.
--
() ASCII Ribbon Campaign | Joe Kesselman
/\ Stamp out HTML e-mail! | System architexture and kinetic poetry
Dec 6 '06 #6

Joe Kesselman wrote:
Jim Andersen wrote:
As you can see from mr p.lepins posting the solution
is far from "trivial".
As Joe Kesselman said, it does a bit more than you asked
for. Partly because I wasn't really sure what you were
asking for, your original post being just a bit sloppy,
and partly because I tend to introduce what I perceive as
useful techniques/good practices when I post solutions to
problems on the usenet. Still, it's trivial enough even
then.

The transformation I posted copies any XML document fed to
it, sorting any siblings with sort attribute by the value
of that attribute (it breaks just a bit if there are
sibling elements without sort attribute interspersed with
elements it's trying to sort).
I'm not sure I agree; that solution strikes me as
overcomplicated for the question you asked.

"Produce a new levelone document, copying the child
elements in sorted order."

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template select="/levelone">
<levelone>
<xsl:for-each select="child">
<xsl:sort select="@sort"/>
<xsl:copy-of select=".">
</xsl:apply-templates>
</levelone>
</xsl:template>
</xsl:stylesheet>
Being my usual obnoxious self, I cannot resist the
temptation to point out that, in my opinion:

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/levelone">
<xsl:copy>
<xsl:apply-templates>
<xsl:sort select="@sort"/>
</xsl:apply-templates>
</xsl:copy>
</xsl:template>
<xsl:template match="child">
<xsl:copy-of select="."/>
</xsl:template>
</xsl:stylesheet>

is only a bit more complicated, and better from the
standpoint of teaching the Arguably Right Thing.

--
Pavel Lepin

Dec 6 '06 #7
p.*****@ctncorp.com wrote:
[example]
is only a bit more complicated, and better from the
standpoint of teaching the Arguably Right Thing.
Depends on the user's intent. Yes, we want to encourage folks to learn
to take advantage of XSLT's pattern-matching and recursion capabilities.
But sometimes processing the nodes directly really does do everything
that's needed.

If the question had been posed differently, I might have solved it
differently. Your milage will vary.

--
Joe Kesselman / Beware the fury of a patient man. -- John Dryden
Dec 6 '06 #8
Joseph Kesselman wrote:
Depends on the user's intent.
My intent was to have my immediate problem solved.
AND to learn something about xslt.
AND to get a piece of code I can re-use in other scenarios, as I will
problably need to sort stuff again.

These things, I think, is something many of us posting and answering q's
want.
So p.lepin guessed right. Even if I was sloppy when asking the q.
But this was my first post in this subject, so I do net yet know what is
important, and what isn't.

/jim
Dec 7 '06 #9
Jim Andersen wrote:
My intent was to have my immediate problem solved.
AND to learn something about xslt.
AND to get a piece of code I can re-use in other scenarios, as I will
problably need to sort stuff again.
Either solution addresses those. Mine focuses on the specifics of
sorting, and trusts that you will learn to think recursively through
other examples. The other attempts to show some other aspects of the
language as well... which may be useful, or may risk obscuring the key
point.

Personally, I still think the p.lepin's solution is overcomplicated; the
use of a separate mode with its own copy of the identity template is
something worth understanding in the long run but I consider it a
distraction here.

Both are reusable. Which one is more reusable -- or teaches you more
about reuse -- is debatable.

As I say, there's always a question of just how far to digress when
answering a question. One of the nice things about asking on a newsgroup
is that you may get multiple solutions, as here, in which case you can
pick the one that matches your own needs... or, if you don't yet know
your needs, you'll at least be exposed to the fact that multiple valid
answers exist.

Have fun.

--
() ASCII Ribbon Campaign | Joe Kesselman
/\ Stamp out HTML e-mail! | System architexture and kinetic poetry
Dec 7 '06 #10
Joe Kesselman wrote:
Personally, I still think the p.lepin's solution is overcomplicated;
I have yet to fully comprehend it :-)
Both are reusable. Which one is more reusable -- or teaches you more
about reuse -- is debatable.
I tend to go with the p.lepin solution. Your solution demands that I
hard-code the names of the nodes "levelone" and "child".
p.lepins only need the "sort"-attribute to be hardcoded.

After I understand p.lepins solution better, I hope to find out if it will
work with a more deeper nested XML lilke

<levelone>
<child ID="1"
sort="5"><names><name>Paul</name><name>Anka</name></names></child>
<child ID="2" sort="1"><name>Adam</name></child>
<child ID="3" sort="2"><name>Will</name></child>
</levelone>
One of the nice things about asking on a
newsgroup is that you may get multiple solutions, as here, in which
case you can pick the one that matches your own needs... or, if you
don't yet know your needs, you'll at least be exposed to the fact
that multiple valid answers exist.

Have fun.
Thanks. To both of you. For guiding me from wet feet, to knee-deep :-)

/jim
Dec 8 '06 #11

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

Similar topics

18
by: googleboy | last post by:
I didn't think this would be as difficult as it now seems to me. I am reading in a csv file that documents a bunch of different info on about 200 books, such as title, author, publisher, isbn,...
1
by: Derek Tinney | last post by:
Hi, I'm having difficulty building an XLST file that allows me to sort a list of log records. I put together an XSL file that allows me to output a copy of the input file and then I attempted...
9
by: Raymond Lewallen | last post by:
I have a dataview in which the sort property will not sort the dataview. Here's is a simple scenario similar to what I am doing: Class Foo Private Function Retrieve() As DataView ' Returns a...
2
by: BjoernJackschina | last post by:
Hello, I just look for a capability to sort several words in view of alphabet. An example: stop is 'opst' reach is 'aechr' This should read in a new file so that I can look for same letter...
6
by: Mark Miller | last post by:
I have a scheduled job that uses different XSL templates to transform XML and save it to disk. I am having problems with the code below. The problem shows up on both my development machine (Windows...
4
by: PCHOME | last post by:
Hi! I have questions about qsort( ). Is anyone be willing to help? I use the following struct: struct Struct_A{ double value; ... } *AA, **pAA;
48
by: Alex Chudnovsky | last post by:
I have come across with what appears to be a significant performance bug in ..NET 2.0 ArrayList.Sort method when compared with Array.Sort on the same data. Same data on the same CPU gets sorted a...
4
by: Gaijinco | last post by:
I'm not quite sure why this code doesn't works: #include <iostream> #include <algorithm> #include <vector> #include <string> using namespace std; class word {
0
by: octavia | last post by:
Hi everyone. I am having trouble redirecting the standard output from using the sort command on an existing file. When I do the following sort -t- -k 1.1,2.2 -k 3.2,3.3n $file.txt >temp.txt The...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
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...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

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.