Home. 
.

transparent

transparent

transparent

Altova Mailing List Archives


Re: Sorting an XML fragment

From: Martin Honnen <mahotrash@-----.-->
To: 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/


transparent
Print
Mail
Like It
Disclaimer
.

These Archives are provided for informational purposes only and have been generated directly from the Altova mailing list archive system and are comprised of the lists set forth on www.altova.com/list/index.html. Therefore, Altova does not warrant or guarantee the accuracy, reliability, completeness, usefulness, non-infringement of intellectual property rights, or quality of any content on the Altova Mailing List Archive(s), regardless of who originates that content. You expressly understand and agree that you bear all risks associated with using or relying on that content. Altova will not be liable or responsible in any way for any content posted including, but not limited to, any errors or omissions in content, or for any losses or damage of any kind incurred as a result of the use of or reliance on any content. This disclaimer and limitation on liability is in addition to the disclaimers and limitations contained in the Website Terms of Use and elsewhere on the site.

.
.

transparent

transparent