How to sort a XML file??

R

richardkreidl

I heard of importing your xml doc into a dataset, then sort the
dataset and re-populate your xml, but I'm not sure how to code this or
is there another way of sorting a XML file? I not familiar with
Dataset coding and how that would work.
Here is my XML file:

?xml version="1.0"?>
<IndividualListing>
<Individual>
<Name>Aaron, Scott</Name>
<Pager>[email protected]</Pager>
</Individual>
<Individual>
<Name>Boston, Jeff</Name>
<Pager>[email protected]</Pager>
</Individual>
<Individual>
<Name>Adams, Ryan</Name>
<Pager>[email protected]</Pager>
</Individual>
<Individual>
<Name>Thomas, Naveed</Name>
<Pager>[email protected]</Pager>
</Individual>
<Individual>
<Name>Baker, Srinivas</Name>
<Pager>[email protected]</Pager>
</Individual>
<Individual>
<Name>Wilson, Don</Name>
<Pager>[email protected]</Pager>
</Individual>
<Individual>
<Name>Naveed Reddy</Name>
<Pager>[email protected]</Pager>
</Individual>
<Individual>
<Name>Sanders, William</Name>
<Pager>[email protected]</Pager>
</Individual>
<Individual>
<Name>Anderson, Karl R.</Name>
<Pager>[email protected]</Pager>
</Individual>
</IndividualListing>


thanks
 
M

Martin Honnen

I heard of importing your xml doc into a dataset, then sort the
dataset and re-populate your xml, but I'm not sure how to code this or
is there another way of sorting a XML file? I not familiar with
Dataset coding and how that would work.
Here is my XML file:

?xml version="1.0"?>
<IndividualListing>
<Individual>
<Name>Aaron, Scott</Name>
<Pager>[email protected]</Pager>
</Individual>

Here is an XSLT stylesheet that sorts your Individual elements on the
Name child elements:

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:strip-space elements="*"/>
<xsl:blush:utput method="xml" indent="yes"/>
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="IndividualListing">
<xsl:copy>
<xsl:apply-templates select="Individual">
<xsl:sort select="Name" data-type="text"/>
</xsl:apply-templates>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>

Result then starts like this

<IndividualListing>
<Individual>
<Name>Aaron, Scott</Name>
<Pager>[email protected]</Pager>
</Individual>
<Individual>
<Name>Adams, Ryan</Name>
<Pager>[email protected]</Pager>
</Individual>
<Individual>
<Name>Anderson, Karl R.</Name>
<Pager>[email protected]</Pager>
</Individual>

You can run XSLT stylesheets from inside Visual Studio 2005 or with .NET
code where you use System.Xml.Xsl.XslCompiledTransform with .NET 2.0.

See
<http://msdn2.microsoft.com/en-us/library/system.xml.xsl.xslcompiledtransform.aspx>
VB.NET example is as easy as

Dim xslt As New XslCompiledTransform()
xslt.Load("stylesheet.xsl")

' Execute the transform and output the results to a file.
xslt.Transform("file.xml", "sorted.xml")
 
S

Susie DBA [MSFT]

If I were you.. I would keep your DATA in a DATABASE and screw XML in
the nose
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top