Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2003-11-16
2
Medium Priority
?
218 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 1000 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Integration Management Part 2
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased riskā€¦

773 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