Solved

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

Posted on 2003-11-16
2
209 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
Comment Utility
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
Comment Utility
thanks :)
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

771 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

9 Experts available now in Live!

Get 1:1 Help Now