XSL noob here - I have XML similar to the following: <response> <a> <b> <c id="1">data</c> <c id="2">data</c> <c id="3">data</c> <c id="4">data</c> </b> </a> </response> I want to transform the XML so that the result is the same except one of the c nodes is always returned first in the response based on a specfic id element (see below). <response> <a> <b> <c id="3">data</c> <c id="1">data</c> <c id="2">data</c> <c id="4">data</c> </b> </a> </response> Seems like it should be simple to do, but I can't figure it out the right syntax. Can anyone help?
time2fly@gmail.com wrote: > I want to transform the XML so that the result is the same except one > of the c nodes is always returned first in the response based on a > specfic id element (see below). Pass in the id of the 'c' element as a parameter and then use the identity transformation template plus a template for 'b' elements that ensures that the 'c' children are processed in the order you want: <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:param name="id" select="3"/> <xsl:strip-space elements="*"/> <xsl:output method="xml" indent="yes"/> <xsl:template match="@* | node()"> <xsl:copy> <xsl:apply-templates select="@* | node()"/> </xsl:copy> </xsl:template> <xsl:template match="b"> <xsl:copy> <xsl:apply-templates select="@*"/> <xsl:apply-templates select="c[@id = $id]"/> <xsl:apply-templates select="c[not(@id = $id)]"/> </xsl:copy> </xsl:template> </xsl:stylesheet> -- Martin Honnen --- MVP XML http://JavaScript.FAQTs.com/
On May 1, 4:11 am, Martin Honnen wrote: > time2...@gmail.com wrote: > > I want to transform the XML so that the result is the same except one > > of the c nodes is always returned first in the response based on a > > specfic id element (see below). > > Pass in the id of the 'c' element as a parameter and then use the > identity transformation template plus a template for 'b' elements that > ensures that the 'c' children are processed in the order you want: > > <xsl:stylesheet > xmlns:xsl="http://www.w3.org/1999/XSL/Transform" > version="1.0"> > > <xsl:param name="id" select="3"/> > > <xsl:strip-space elements="*"/> > > <xsl:output method="xml" indent="yes"/> > > <xsl:template match="@* | node()"> > <xsl:copy> > <xsl:apply-templates select="@* | node()"/> > </xsl:copy> > </xsl:template> > > <xsl:template match="b"> > <xsl:copy> > <xsl:apply-templates select="@*"/> > <xsl:apply-templates select="c[@id = $id]"/> > <xsl:apply-templates select="c[not(@id = $id)]"/> > </xsl:copy> > </xsl:template> > > </xsl:stylesheet> > > -- > > Martin Honnen --- MVP XML > http://JavaScript.FAQTs.com/ This worked perfectly, thanks!