Solved

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

Posted on 2003-11-16
2
215 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
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…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

710 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