Solved

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

Posted on 2003-11-16
2
214 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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

821 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