xml - How move Child Nodes along with values to grand parent node -
i tried few different ways transfer child nodes grand parent, pattern match happening 1 child node not recursively. appreciate here.
<grandparent1> <parent1> <child1>1</child1> <child2>2</child2> </parent2> </grandparent1>
should become
<grandparent1> <child1>1</child1> <child2>2</child2> </grandparent1>
size of child nodes varies. appreciate here
this easy using xslt's recursive processing model.
first, use identity transform template recursively copy as is. add exception parent nodes , make them continue recursion without copying themselves.
xslt 1.0
<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="*"/> <!-- identity transform --> <xsl:template match="@*|node()"> <xsl:copy> <xsl:apply-templates select="@*|node()"/> </xsl:copy> </xsl:template> <xsl:template match="parent1 | parent2"> <xsl:apply-templates/> </xsl:template> </xsl:stylesheet>
to make generic parent (i.e. 2nd level) node, without knowing name in advance, use:
<xsl:template match="/*/*"> <xsl:apply-templates/> </xsl:template>
as second template.
Comments
Post a Comment