Groups | Blog | Home
all groups > dotnet xml > november 2004 >

dotnet xml : SORTING A FILE XML


Siu
11/25/2004 7:05:07 AM
Hi,
is it possible to sort a XML file without using a XSLT or XSL file??
For example, my XML is:

<A>
<1>
<3>
<4>
<2>
</A>
<C>
<2>
<1>
</C>
<B>
<3>
<2>
<1>
</B>

The result of the sorting method should by:
<A>
<1>
<2>
<3>
<4>
</A>
<B>
<1>
<2>
<3>
</B>
<C>
<1>
<2>
</C>

Can anyone help me ?
Siu
11/25/2004 8:31:03 AM
Hi Martin,
thanks for answering. You're right... I've written a bat example.... my real
XML file is like this:

- <APPLICATIONLIST>
- <APPLICATION APPNAME="ActiveSync">
- <ICONGROUP SOURCE="ActiveSync.lnk">
- <DEST TARGET="START" />
</ICONGROUP>
</APPLICATION>
- <APPLICATION APPNAME="AutoCAD">
- <ICONGROUP SOURCE="AutoCAD.lnk">
<DEST TARGET="START" />
</ICONGROUP>
</APPLICATION>
- <APPLICATION APPNAME="Banana">
- <ICONGROUP SOURCE="Banana.lnk">
<DEST TARGET="START" />
</ICONGROUP>
</APPLICATION>
- <APPLICATION APPNAME="Bridge Workstation">
</APPLICATION>
<APPLICATIONLIST>

I would like to order my XML file by APPNAME of each APPLICATION node: the
latter can be also nested into other APPLICATION node, like this:

- <APPLICATION APPNAME="MicrosoftOffice">
- <ICONGROUP SOURCE="ActiveSync.lnk">
- <DEST TARGET="START" />
</ICONGROUP>
- <APPLICATION APPNAME="Excel"/>
- <APPLICATION APPNAME="Word"/>
- <APPLICATION APPNAME="PowerPoint"/>
</APPLICATION>

Can you help me, please

[quoted text, click to view]
Martin Honnen
11/25/2004 4:26:37 PM


[quoted text, click to view]


[quoted text, click to view]

Yes, there are other ways to programmatically manipulate XML, DOM
programming for instance, see System.Xml.XmlDocument. But XSLT has a
sort instruction while with DOM programming you need to implement that
yourself.

[quoted text, click to view]

That is not XML, you have no root element and element names need to
start with a letter and not a digit so you need to provide a sample that
is XML.

[quoted text, click to view]

So you want to sort by element names and not element content? Or is
<1>
supposed to be
<data>1</data>
?

--

Martin Honnen
Martin Honnen
11/25/2004 6:36:17 PM


[quoted text, click to view]


[quoted text, click to view]

Here is an XSLT 1.0 stylesheet that sorts that:

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

<xsl:output method="xml" encoding="UTF-8" indent="yes" />

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

<xsl:template match="APPLICATIONLIST">
<xsl:copy>
<xsl:apply-templates select="APPLICATION">
<xsl:sort select="@APPNAME" data-type="text" order="ascending"
lang="en" case-order="upper-first" />
</xsl:apply-templates>
</xsl:copy>
</xsl:template>

<xsl:template match="APPLICATION">
<xsl:copy>
<xsl:apply-templates select="@*" />
<xsl:apply-templates select="ICONGROUP" />
<xsl:apply-templates select="APPLICATION">
<xsl:sort select="@APPNAME" data-type="text" order="ascending"
lang="en" case-order="upper-first" />
</xsl:apply-templates>
</xsl:copy>
</xsl:template>

</xsl:stylesheet>

It is easily possible with .NET to apply the transformation to the
source input, see the XslTransform class in the .NET documentation.

--

Martin Honnen
Siu
11/26/2004 2:45:02 AM
Hi Martin,
your suggestion is perfect, thank you:)

I've have another little question: I've used the XslTransform class in order
to sort that xml file into another new xml file, and it works, but the
problem is that the method or the xslt file writes into the output file as
first row this:

<?xml version="1.0" encoding="utf-8" ?>

How can I avoid writing this row?

Thank you again

[quoted text, click to view]
Siu
11/26/2004 2:47:06 AM
Hi Martin,
your suggestion is perfect, thank you :)

I've another question: I've used the method XslTransform in order to sort my
xml file into another xml output file: in the latter appears as first
row/sentence this:

<?xml version="1.0" encoding="utf-8" ?>

How can I avoid writing this first row/sentence? is it possible?

Thank you again :)


[quoted text, click to view]
Siu
11/26/2004 2:47:09 AM
Hi Martin,
your suggestion is perfect, thank you :)

I've another question: I've used the method XslTransform in order to sort my
xml file into another xml output file: in the latter appears as first
row/sentence this:

<?xml version="1.0" encoding="utf-8" ?>

How can I avoid writing this first row/sentence? is it possible?

Thank you again :)

[quoted text, click to view]
Siu
11/26/2004 4:39:04 AM
Thank you very much, Martin ;)

[quoted text, click to view]
Martin Honnen
11/26/2004 12:53:20 PM


[quoted text, click to view]


[quoted text, click to view]

It is not a row, it is the XML declaration that usually doesn't do any
harm in an XML file.
If you want to avoid it you can do so from XSLT with

<xsl:output method="xml" encoding="UTF-8" indent="yes"
omit-xml-declaration="yes" />


--

Martin Honnen
AddThis Social Bookmark Button