xslt - How to move single element of XML into Another Element using XSL -
i want move element id
here
input xml
<collection> <abc> <id>1234</id> </abc> <addedparts name="addedparts" type="unknown" status="0"> <part> </part> <here> </here> </addedparts> </collection>
expected output
<collection> <abc> </abc> <addedparts name="addedparts" type="unknown" status="0"> <part> </part> <here> <id>1234</id> </here> </addedparts> </collection>
the xsl write
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/xsl/transform"> <xsl:output method= "xml" version= "1.0" encoding= "utf-8" indent= "yes" /> <xsl:strip-space elements= "*" /> <xsl:output omit-xml-declaration="yes"/> <xsl:template match="node()|@*"> <xsl:copy> <xsl:apply-templates select="node()|@*"/> </xsl:copy> </xsl:template> <xsl:template match= "here" > <xsl:copy> <xsl:apply-templates select= "node()|@*" /> <!-- move nodes here --> <xsl:apply-templates select= "../id " mode= "move" /> </xsl:copy> </xsl:template > < xsl:template match= "id" />
i unable achieve expected output
you've got 2 things wrong current xslt
- you trying select
../id
,id
not child of current parent, child ofabc
element under current grand-parent. should../../abc/id
. - you applying template mode of "move" have no matching template mode, meaning xslt's built-in templates used, output text content of
id
, not element itself.
try xslt
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/xsl/transform"> <xsl:output method= "xml" version= "1.0" encoding= "utf-8" indent= "yes" /> <xsl:strip-space elements= "*" /> <xsl:output omit-xml-declaration="yes"/> <xsl:template match="node()|@*" name="identity"> <xsl:copy> <xsl:apply-templates select="node()|@*"/> </xsl:copy> </xsl:template> <xsl:template match="here" > <xsl:copy> <xsl:apply-templates select= "node()|@*" /> <!-- move nodes here --> <xsl:apply-templates select= "../../abc/id" mode="move" /> </xsl:copy> </xsl:template> <xsl:template match= "id" /> <xsl:template match="id" mode="move"> <xsl:call-template name="identity" /> </xsl:template> </xsl:stylesheet>
if don't intend try transform id
in way, simplify this...
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/xsl/transform"> <xsl:output method= "xml" version= "1.0" encoding= "utf-8" indent= "yes" /> <xsl:strip-space elements= "*" /> <xsl:output omit-xml-declaration="yes"/> <xsl:template match="node()|@*" name="identity"> <xsl:copy> <xsl:apply-templates select="node()|@*"/> </xsl:copy> </xsl:template> <xsl:template match="here" > <xsl:copy> <xsl:apply-templates select= "node()|@*" /> <!-- move nodes here --> <xsl:copy-of select= "../../abc/id" /> </xsl:copy> </xsl:template> <xsl:template match= "id" /> </xsl:stylesheet>
Comments
Post a Comment