xml - xslt copy only certain descendants -
i trying copy descendant elements without attribute. cant figure out right way this.
here file:
<?xml version="1.0" encoding="utf-8" standalone="yes"?> <itemlist> <item> <subitem id="g0b86bn6"/> <subitem> <subitem/> <subitem id="8967698"/> </subitem> <subitem> <subitem/> <subitem id="9868966n7"/> <subitem> <subitem id="9896"/> <subitem> </subitem> </item> </itemlist>
the elements nested arbirtarily deep.
expected output:
<?xml version="1.0" encoding="utf-8" standalone="yes"?> <itemlist> <item> <subitem> <subitem/> </subitem> <subitem> <subitem/> </subitem> </item> </itemlist>
my xsl:
<xsl:template match="item"> <xsl:for-each select="child::*"> <xsl:if test=".[not(@id)]"> <xsl:copy> <xsl:apply-templates select=". | @*"/> </xsl:copy> </xsl:if> </xsl:for-each> </xsl:template>
the problem: copies children, not descendants. , copy-of copies descendants dont want copy.
how should this? , tips!
the preferred strategy in cases these use identity transform template copy as is, add exception templates suppress nodes not want passed output. example, following stylesheet:
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="subitem[@id]"/> </xsl:stylesheet>
when applied following well-formed input:
xml
<itemlist> <item> <subitem id="g0b86bn6"/> <subitem> <subitem/> <subitem id="8967698"/> </subitem> <subitem> <subitem/> <subitem id="9868966n7"/> <subitem> <subitem id="9896"/> </subitem> </subitem> </item> </itemlist>
will suppress subitem
element has id
attribute, resulting in:
output
<?xml version="1.0" encoding="utf-8"?> <itemlist> <item> <subitem> <subitem/> </subitem> <subitem> <subitem/> <subitem/> </subitem> </item> </itemlist>
Comments
Post a Comment