Solved

How to sort data, inside the XML file, by using XSLT

Posted on 2003-11-16
2
213 Views
Last Modified: 2010-04-16
I am trying to sort the xml file's <Name> with XSLT but no luck so far.
Using Windows Application, not Web one.  The form has two datagrids--one for Planet,
another one for PlanetData.  One button for XSLT.  Other buttons for
loading/saving xml file's data.

------

[Planets.xml]

<?xml version="1.0" encoding="UTF-8" ?>
<Planets>
      <Planet>
            <NameID>01</NameID>
            <Name>Mercury</Name>
      </Planet>
      <Planet>
            <NameID>02</NameID>
            <Name>Venus</Name>
      </Planet>
      <Planet>
            <NameID>03</NameID>
            <Name>Earth</Name>
      </Planet>

      <PlanetData>
            <DataID>001</DataID>
            <Mass>0.0553(Earth = 1)</Mass>
            <NameID>01</NameID>
      </PlanetData>
      <PlanetData>
            <DataID>002</DataID>
            <Mass>0.815(Earth = 1)</Mass>
            <NameID>02</NameID>
      </PlanetData>
      <PlanetData>
            <DataID>003</DataID>
            <Mass>1.0(Earth = 1)</Mass>
            <NameID>03</NameID>
      </PlanetData>
</Planets>

-------

[PlanetSort.xslt] (I dont know if these codes are incorrect...)

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
      <xsl:template match="Planets">
            <xsl:apply-templates>
            <xsl:sort select="Name"/>
            </xsl:apply-templates>
      </xsl:template>
</xsl:stylesheet>

-------

I have some difficulties trying to write codes for the XSLT button. I cannot
transform (sort) them properly.

private void button1_Click(object sender, System.EventArgs e)
{
      string fn1 = "Planets.xml";
      string fn2 = "Sorted_Planets.xml";
      string fnx = "PlanetSort.xslt";

      FileStream fin = new FileStream(@fn1, FileMode.Open);
      XmlTextReader xr = new XmlTextReader(fin);
      System.Xml.XPath.XPathDocument xpd = new System.Xml.XPath.XPathDocument(xr);
      System.Xml.Xsl.XslTransform xslt = new System.Xml.Xsl.XslTransform();
      xslt.Load(@fnx);

      //  problems here...
      FileStream fout = new FileStream(@fn2, FileMode.Create);
      XmlTextWriter xw = new XmlTextWriter(fout, System.Text.Encoding.GetEncoding("UTF-8"));
      xw.WriteProcessingInstruction("xml", "version=\"1.0\" encoding=\"UTF-8\" ");
      xw.Formatting = Formatting.Indented;
      xw.WriteStartElement("Planets");
      xw.WriteStartElement("Planet");
      xw.WriteStartElement("Name");
      xw.WriteEndElement();
      xw.WriteEndElement();
      xw.WriteEndElement();
      xslt.Transform(xpd, null, xw);
      xr.Close();
      xw.Close();
}

------

Don't know if XSLT is useful but if you have different better methods, please let me know.
Advices, examples will be appreciated.  Thank you :)
0
Comment
Question by:rizel
2 Comments
 
LVL 1

Accepted Solution

by:
alkonaut earned 250 total points
ID: 9762516
You have to specify a valid xpath expression pointing to the element you wish to sort on. e.g. if you want to output the 3 planet nodes sorted by name you'd write

<xsl:template match="Planets">
  <xsl:apply-templates select="Planet">
    <xsl:sort select="Name"/>
  </xsl:apply-templates>
</xsl:template>

beacuse now you can see the path /PlanetData/Planet/Name

otherwise you could also use <xsl:sort select="Planet/Name"/> under Planets, but the nodes <PlanetData> have no element <Name> so they cannot be sorted on name.
0
 

Author Comment

by:rizel
ID: 9888158
thanks :)
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now