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

XSLT Sorting this XML...

I have this XML file which I would like to XSLT-sort based on the
string contained within the item-children elements using basic MSXML
(no recent version of IIS, so it might be an outdated MSXML -- pretty
sure not MSXML4, though tips on how to do it with that are appreciated
too):

----------- Translation Dictionary (German) ----
<?xml version="1.0" encoding="iso-8859-1"?>
<items>
<item id="4">
<Deutsch>Registrieren</Deutsch>
<Englisch-GB>Register</Englisch-GB>
<Englisch-US>Register</Englisch-US>
<Französisch>S'enregistrer</Französisch>
<Italienisch>Registra</Italienisch>
<Spanisch>Darse de alta</Spanisch>
<Portugiesisch></Portugiesisch>
<Niederländisch>Registreren</Niederländisch>
<Kommentar></Kommentar>
</item>
<item id="5">
<Deutsch>Login</Deutsch>
<Englisch-GB>Login</Englisch-GB>
<Englisch-US>Login</Englisch-US>
<Französisch>Login</Französisch>
<Italienisch>Login</Italienisch>
<Spanisch>Inicio de sesión</Spanisch>
<Portugiesisch></Portugiesisch>
<Niederländisch>Login</Niederländisch>
<Kommentar></Kommentar>
</item>
<!-- ... and many more item-elements -->
</items>
------------------------------------------------

Thanks for any tips!
(P.S.: Am always disappointed by lack of XPath sorting features. It's a
hassle compared to e.g. SQL. Last time I had to implement an object
with an object sorter to sort some integers in XML. I also tried
XSLT-sorting from examples found online but often ran into troubles, I
believe it also has to do with limited capabilities of basic-MSXML as
installed with older versions of IIS.)
Jul 20 '05 #1
3 2855


Philipp Lenssen wrote:
I have this XML file which I would like to XSLT-sort based on the
string contained within the item-children elements using basic MSXML
(no recent version of IIS, so it might be an outdated MSXML -- pretty
sure not MSXML4, though tips on how to do it with that are appreciated
too):

----------- Translation Dictionary (German) ----
<?xml version="1.0" encoding="iso-8859-1"?>
<items>
<item id="4">
<Deutsch>Registrieren</Deutsch>
<Englisch-GB>Register</Englisch-GB>
<Englisch-US>Register</Englisch-US>
<Französisch>S'enregistrer</Französisch>
<Italienisch>Registra</Italienisch>
<Spanisch>Darse de alta</Spanisch>
<Portugiesisch></Portugiesisch>
<Niederländisch>Registreren</Niederländisch>
<Kommentar></Kommentar>
</item>
<item id="5">
<Deutsch>Login</Deutsch>
<Englisch-GB>Login</Englisch-GB>
<Englisch-US>Login</Englisch-US>
<Französisch>Login</Französisch>
<Italienisch>Login</Italienisch>
<Spanisch>Inicio de sesión</Spanisch>
<Portugiesisch></Portugiesisch>
<Niederländisch>Login</Niederländisch>
<Kommentar></Kommentar>
</item>
<!-- ... and many more item-elements -->
</items>

Assuming you want to copy all nodes while ordering the children of
<item> by their content the following is an XSLT 1.0 stylesheet that
does that:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:output method="xml" indent="yes" encoding="ISO-8859-1" />

<xsl:template match="item">
<xsl:copy>
<xsl:apply-templates select="@*" />
<xsl:apply-templates select="*">
<xsl:sort data-type="text" order="ascending" />
</xsl:apply-templates>
</xsl:copy>
</xsl:template>

<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()" />
</xsl:copy>
</xsl:template>

</xsl:stylesheet>

The result of the example file transformed with Xalan is

<?xml version="1.0" encoding="ISO-8859-1"?>
<items>
<item id="4">
<Portugiesisch/>
<Kommentar/>
<Spanisch>Darse de alta</Spanisch>
<Englisch-GB>Register</Englisch-GB>
<Englisch-US>Register</Englisch-US>
<Italienisch>Registra</Italienisch>
<Niederländisch>Registreren</Niederländisch>
<Deutsch>Registrieren</Deutsch>
<Französisch>S'enregistrer</Französisch>
</item>
<item id="5">
<Portugiesisch/>
<Kommentar/>
<Spanisch>Inicio de sesión</Spanisch>
<Deutsch>Login</Deutsch>
<Englisch-GB>Login</Englisch-GB>
<Englisch-US>Login</Englisch-US>
<Französisch>Login</Französisch>
<Italienisch>Login</Italienisch>
<Niederländisch>Login</Niederländisch>
</item>
<!-- ... and many more item-elements -->
</items>
As for MSXML, as far as I know MSXML 3.0 is the first to support XSLT
1.0 (which is indentified by the namespace URI
http://www.w3.org/1999/XSL/Transform). Earlier versions only support
some working draft (WD) with a different namespace and different
elements and semantics. I stronly suggest to get your ISP to install at
least MSXML 3.0 if you want to do XSLT 1.0 transformations.
--

Martin Honnen
http://JavaScript.FAQTs.com/

Jul 20 '05 #2
Martin Honnen wrote:
Philipp Lenssen wrote:

<?xml version="1.0" encoding="iso-8859-1"?>
<items>
<item id="4">
<Deutsch>Registrieren</Deutsch>
<Englisch-GB>Register</Englisch-GB>
<Englisch-US>Register</Englisch-US>
<Französisch>S'enregistrer</Französisch>
<Italienisch>Registra</Italienisch>
<Spanisch>Darse de alta</Spanisch>
<Portugiesisch></Portugiesisch>
<Niederländisch>Registreren</Niederländisch>
<Kommentar></Kommentar>
</item>
<item id="5">
<Deutsch>Login</Deutsch>
<Englisch-GB>Login</Englisch-GB>
<Englisch-US>Login</Englisch-US>
<Französisch>Login</Französisch>
<Italienisch>Login</Italienisch>
<Spanisch>Inicio de sesión</Spanisch>
<Portugiesisch></Portugiesisch>
<Niederländisch>Login</Niederländisch>
<Kommentar></Kommentar>
</item>
<!-- ... and many more item-elements -->
</items>

Assuming you want to copy all nodes while ordering the children of
<item> by their content the following is an XSLT 1.0 stylesheet that
does that:


I'm sorry, I should have made this clear: I want to sort by a certain
language (the children of "item"-element are language-names in German).
For example, I pass the parameter "Deutsch", and then it should sort
alphabetically by the "Deutsch"-element's text-content. Same for e.g.
"Italienisch". I will keep your current solution for another day.
Thanks for your help so far!
Jul 20 '05 #3
"Philipp Lenssen" <in**@outer-court.com> writes:
Martin Honnen wrote:
Philipp Lenssen wrote:
<?xml version="1.0" encoding="iso-8859-1"?>
<items>
<item id="4">
<Deutsch>Registrieren</Deutsch>
<Englisch-GB>Register</Englisch-GB>
<Englisch-US>Register</Englisch-US>
<Französisch>S'enregistrer</Französisch>
<Italienisch>Registra</Italienisch>
<Spanisch>Darse de alta</Spanisch>
<Portugiesisch></Portugiesisch>
<Niederländisch>Registreren</Niederländisch>
<Kommentar></Kommentar>
</item>
<item id="5">
<Deutsch>Login</Deutsch>
<Englisch-GB>Login</Englisch-GB>
<Englisch-US>Login</Englisch-US>
<Französisch>Login</Französisch>
<Italienisch>Login</Italienisch>
<Spanisch>Inicio de sesión</Spanisch>
<Portugiesisch></Portugiesisch>
<Niederländisch>Login</Niederländisch>
<Kommentar></Kommentar>
</item>
<!-- ... and many more item-elements -->
</items>


[snip]
I'm sorry, I should have made this clear: I want to sort by a certain
language (the children of "item"-element are language-names in German).
For example, I pass the parameter "Deutsch", and then it should sort
alphabetically by the "Deutsch"-element's text-content. Same for e.g.
"Italienisch". I will keep your current solution for another day.
Thanks for your help so far!


I'm not familiar with MSXML (I'm using Xalan), but something like this
should work...

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" encoding="ISO-8859-1"/>

<xsl:template match="items">
<xsl:apply-templates select="item">
<xsl:sort select="item/Deutsch"/>
</xsl:apply-templates>
</xsl:template>

<!-- insert all other templates to process the items -->

</xsl:stylesheet>

Best wishes,
Mats Kindahl
--
IAR Systems in Uppsala, Sweden.

Any opinions expressed are my own and not those of my company.

Spam prevention: contact me at mN**********@acm.org or
mN****************@iar.se, removing the *NO SPAM* from the address.
Jul 20 '05 #4

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

Similar topics

6
by: Ben Fitzgerald | last post by:
Hi I feel I'll be asking for someone to turn water into wine before this happens, but just in case! I'd like to have a page that provides a basic xml document with an xslt that defines the...
0
by: Mark Ritchie | last post by:
I'm trying to sort and filter some xml using XSLT, sorting the data has been easy, but filtering it while maintaining the sort order has turned out to be a lot harder. Here is an example of the...
1
by: David | last post by:
I would like to be able to re-sort data in an HTML table on the without returning to the server. It seems like an XSLT should be able to accomplish this, but I can't find enough information... ...
1
by: ralf321 | last post by:
hello! i have this xml: <object> <node meta="27" > <data meta="13" changed="2005-09-05 16:16:43">a</data> <data meta="14" changed="2005-11-06 19:26:49">b</data> <data meta="15" ...
5
by: RicercatoreSbadato | last post by:
I want to load a table with 2 columns from an xml file. Is it possible with xslt to sort the columns by clicking on their header ?
6
by: bcochofel | last post by:
I'm using xsl to list an xml file that contains something like: sites, tag and weight. I'm listing this in a table with the following titles: | URL | TAG | WEIGHT (each title his a link) What...
3
by: silver_sabrina | last post by:
Hey everyone, I'm having some trouble with this. I need to convert one xml doc into another and I think that XSLT may be the answer, so I'd like some help from the gurus out here :) If XSLT cannot...
1
by: aplonis | last post by:
I have an XML (Atom) doc at this URL... Atom ...which displays as HTML using any one of three XSLT stylesheets, the principal one being at this URL... XSLT ...with two...
2
by: TAL651 | last post by:
Hello, I'd like to use XML + XSLT to create a sortable table. The XML stores the data, and two different XSL stylesheets display the table, sorted in two different ways. What I don't understand...
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: 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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.