473,671 Members | 2,168 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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>Regist rieren</Deutsch>
<Englisch-GB>Register</Englisch-GB>
<Englisch-US>Register</Englisch-US>
<Französisch>S' enregistrer</Französisch>
<Italienisch>Re gistra</Italienisch>
<Spanisch>Dar se de alta</Spanisch>
<Portugiesisch> </Portugiesisch>
<Niederländisch >Registreren</Niederländisch>
<Kommentar></Kommentar>
</item>
<item id="5">
<Deutsch>Logi n</Deutsch>
<Englisch-GB>Login</Englisch-GB>
<Englisch-US>Login</Englisch-US>
<Französisch>Lo gin</Französisch>
<Italienisch>Lo gin</Italienisch>
<Spanisch>Inici o 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 2881


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>Regist rieren</Deutsch>
<Englisch-GB>Register</Englisch-GB>
<Englisch-US>Register</Englisch-US>
<Französisch>S' enregistrer</Französisch>
<Italienisch>Re gistra</Italienisch>
<Spanisch>Dar se de alta</Spanisch>
<Portugiesisch> </Portugiesisch>
<Niederländisch >Registreren</Niederländisch>
<Kommentar></Kommentar>
</item>
<item id="5">
<Deutsch>Logi n</Deutsch>
<Englisch-GB>Login</Englisch-GB>
<Englisch-US>Login</Englisch-US>
<Französisch>Lo gin</Französisch>
<Italienisch>Lo gin</Italienisch>
<Spanisch>Inici o 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:styleshe et 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="ascendin g" />
</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">
<Portugiesisc h/>
<Kommentar/>
<Spanisch>Dar se de alta</Spanisch>
<Englisch-GB>Register</Englisch-GB>
<Englisch-US>Register</Englisch-US>
<Italienisch>Re gistra</Italienisch>
<Niederländisch >Registreren</Niederländisch>
<Deutsch>Regist rieren</Deutsch>
<Französisch>S' enregistrer</Französisch>
</item>
<item id="5">
<Portugiesisc h/>
<Kommentar/>
<Spanisch>Inici o de sesión</Spanisch>
<Deutsch>Logi n</Deutsch>
<Englisch-GB>Login</Englisch-GB>
<Englisch-US>Login</Englisch-US>
<Französisch>Lo gin</Französisch>
<Italienisch>Lo gin</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>Regist rieren</Deutsch>
<Englisch-GB>Register</Englisch-GB>
<Englisch-US>Register</Englisch-US>
<Französisch>S' enregistrer</Französisch>
<Italienisch>Re gistra</Italienisch>
<Spanisch>Dar se de alta</Spanisch>
<Portugiesisch> </Portugiesisch>
<Niederländisch >Registreren</Niederländisch>
<Kommentar></Kommentar>
</item>
<item id="5">
<Deutsch>Logi n</Deutsch>
<Englisch-GB>Login</Englisch-GB>
<Englisch-US>Login</Englisch-US>
<Französisch>Lo gin</Französisch>
<Italienisch>Lo gin</Italienisch>
<Spanisch>Inici o 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.
"Italienisc h". 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>Regist rieren</Deutsch>
<Englisch-GB>Register</Englisch-GB>
<Englisch-US>Register</Englisch-US>
<Französisch>S' enregistrer</Französisch>
<Italienisch>Re gistra</Italienisch>
<Spanisch>Dar se de alta</Spanisch>
<Portugiesisch> </Portugiesisch>
<Niederländisch >Registreren</Niederländisch>
<Kommentar></Kommentar>
</item>
<item id="5">
<Deutsch>Logi n</Deutsch>
<Englisch-GB>Login</Englisch-GB>
<Englisch-US>Login</Englisch-US>
<Französisch>Lo gin</Französisch>
<Italienisch>Lo gin</Italienisch>
<Spanisch>Inici o 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.
"Italienisc h". 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:styleshe et 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**********@ac m.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
5192
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 default sorting. Say it's in a table sorted by name with columns age and height. Then, when the user clicks on another column heading, this
0
1443
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 XML <People> <Person DisplayName="George Smith" Priority="3"> <Names> <Name Type="Initials" TypePriority="100" LastUpdated="21/01/2001" Exclusive="False" FullName="Gq" />
1
2341
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... I have am XML file generated on the server that looks something like this: <?xml version="1.0" encoding="utf-8" ?> <?xml-stylesheet type="text/xsl" href="search.xsl"?> <AlbumSearch version="3.0" time="02Apr04 10:54:31 EST"> <SearchResults...
1
1299
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" changed="2005-09-12 13:58:44">c</data> <data meta="16" changed="2005-09-07 16:56:48">d</data> </node>
5
1493
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
2494
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 I want is when someone clicks a title chooses the sorting option, if this is the actual sorting option then reverses the order. Can I do this? How?
3
2364
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 solve the problem then I think I'll have to write a Java program using DOM or something. Here is an example of what I am trying to accomplish - basically, I need to do a lookup on the author's first name and last name and then create the...
1
1817
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 daughter-XSLT's, both very short, and employing
2
2657
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 is how to allow the user to switch between the two stylesheets (and hence two sorting options). I can provide code snippets if that would help, but I'm basically looking for the general concept here - how can you dynamically switch XSLT...
0
8476
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
8393
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
8914
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
8598
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,...
0
8670
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6223
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
5695
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();...
2
2051
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1809
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.