Altova Mailing List Archives>Archive Index >comp.text.xml Archive Home >Recent entries >Thread Prev - Sort xml and output xml >Thread Next - Re: Sort xml and output xml Re: Sort xml and output xmlTo: NULL Date: 12/5/2006 12:26:00 AM Jim Andersen wrote: > I have this xml-file. I want to sort it, and create a > new xml-file. The result should be identical to the > input except that it's sorted. Since you failed to mention what you're using for processing, I assume it's some XSLT 1.0-compliant processor. > <?xml version="1.0" encoding="UTF-8"?> > <levelone> > <child ID="1" sort="5"><name>Paul</name></child> > <child ID="2" sort="1"><name>Adam</name></child> > <child ID="3" sort="2"><name>Will</name></child> > <root> That's not an XML file. It's not well-formed. > I've been searching for hours, but all the examples I > have seen creates an HTML file. That's mighty strange, especially since XML is the default output method. All the XSLT tutorials I remember covered sorting, identity transformation and controlling your output. Presuming you meant something like: <levelone> <child ID="1" sort="5"><name>Paul</name></child> <child ID="2" sort="1"><name>Adam</name></child> <child ID="3" sort="2"><name>Will</name></child> </levelone> The following XSLT should do the trick: <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="node()|@*"> <xsl:copy> <xsl:apply-templates select="node()|@*"/> </xsl:copy> </xsl:template> <xsl:template match="*[@sort][not(preceding-sibling::*[@sort])]"> <xsl:apply-templates select="../*[@sort]" mode="copy"> <xsl:sort select="@sort"/> </xsl:apply-templates> </xsl:template> <xsl:template match="*[@sort][preceding-sibling::*[@sort]]"/> <xsl:template match="node()|@*" mode="copy"> <xsl:copy> <xsl:apply-templates select="node()|@*"/> </xsl:copy> </xsl:template> </xsl:stylesheet> -- Pavel Lepin | ||||||
| Company | Legal | Press | Partners | Careers | Sitemap | Contact Us | Altova Blog | Mobile | Full Site | |||
|
