Altova Mailing List Archives>Archive Index >microsoft.public.xml Archive Home >Recent entries >Thread Prev - Sorting an XML fragment >Thread Next - Re: Sorting an XML fragment Re: Sorting an XML fragmentTo: NULL Date: 4/9/2008 6:27:00 PM MyCross wrote: > I have no idea how to approach this. It's just an in-memory string > with an XML fragment, not an XML document - can I use XSLT on that? Here is a stylesheet that performs the sort: <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/> <xsl:template match="Trend"> <xsl:copy> <xsl:apply-templates select="Score"> <xsl:sort select="@PullDate" data-type="text"/> </xsl:apply-templates> </xsl:copy> </xsl:template> <xsl:template match="@* | node()"> <xsl:copy> <xsl:apply-templates select="@* | node()"/> </xsl:copy> </xsl:template> </xsl:stylesheet> And here is some C#/.NET 2.0 sample code that uses XslCompiledTransform to take a string with XML input and to return a string with the transformation result: class MyTransformer { private XslCompiledTransform processor; public MyTransformer(string stylesheetUri) { processor = new XslCompiledTransform(); processor.Load(stylesheetUri); } public string Transform(string input) { StringWriter output = new StringWriter(); processor.Transform(XmlReader.Create(new StringReader(input)), null, output); return output.ToString(); } } Use like this: string exampleInput = @" <Trend> <Score PullDate=""2008-03-22T00:00:00"" Value=""768"" /> <Score PullDate=""2007-11-28T00:00:00"" Value=""748"" /> </Trend>"; MyTransformer transformer = new MyTransformer(@"..\..\XSLTFile1.xslt"); string result = transformer.Transform(exampleInput); You could use the same MyTransformer object for several Transform calls. -- Martin Honnen --- MVP XML http://JavaScript.FAQTs.com/ | ||||||
| Company | Legal | Press | Partners | Careers | Sitemap | Contact Us | Altova Blog | Mobile | Full Site | |||
|
