删除父节点,如果一个子节点是空
问题描述:
源XML:删除父节点,如果一个子节点是空
<MP>
<Name>pol</Name>
<PRules>
<PRule order="1" name="r1">
<Conditions>
<Condition eleName="eth" value="05">05</Condition>
<Condition eleName="dest" value="32">32</Condition>
</Conditions>
</PRule>
<PRule order="2" name="r2">
<Conditions>
<Condition eleName="eth" value="04">04</Condition>
</Conditions>
<Actions>
<Action name="xyz"/>
</Actions>
</PRule>
</PRules>
</MP>
如果有属性的条件节点eleName =“ETH”已被删除。如果条件为空,删除条件节点后,必须删除完整的PRule节点。
我已经申请了以下XSLT:
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" omit-xml-declaration="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template name="attributeTemplate" match="Condition[@elementName='eth']"/>
<xsl:template match="PRule[descendant::Conditions[not(@*)]]"/>
</xsl:stylesheet>
但结果来了是这样的:
<MP>
<Name>pol</Name>
</PRules>
</MP>
我有什么变化,使转换XML作为
<MP>
<Name>pol</Name>
<PRules>
<PRule name="r1" order="1">
<Conditions>
<Condition eleName="dest" value="32">32</Condition>
</Conditions>
</PRule>
</PRules>
</MP>
xsl文件出了什么问题,我不明白。基本上我想在条件为空的情况下删除父PRule节点。
答
这种转变:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="PRule[not(*/Condition[not(@eleName='eth')])]"/>
<xsl:template match="Condition[@eleName = 'eth']"/>
</xsl:stylesheet>
时所提供的XML文档应用:
<MP>
<Name>pol</Name>
<PRules>
<PRule order="1" name="r1">
<Conditions>
<Condition eleName="eth" value="05">05</Condition>
<Condition eleName="dest" value="32">32</Condition>
</Conditions>
</PRule>
<PRule order="2" name="r2">
<Conditions>
<Condition eleName="eth" value="04">04</Condition>
</Conditions>
<Actions>
<Action name="xyz"/>
</Actions>
</PRule>
</PRules>
</MP>
产生想要的,正确的结果:
<MP>
<Name>pol</Name>
<PRules>
<PRule order="1" name="r1">
<Conditions>
<Condition eleName="dest" value="32">32</Condition>
</Conditions>
</PRule>
</PRules>
</MP>
说明:
+0
谢谢..它正在工作.. – Bala 2012-07-18 13:37:39
你的表面规则存在一些矛盾。你的模板和标题建议(但你在叙述中没有说清楚),如果一个元素没有属性并且没有子元素,那么它应该被删除。但是PRule被淘汰的说法与此相矛盾,因为PRule具有属性。 – 2012-07-18 12:01:54
这将是一个简单的样式表,一旦您澄清了规则。但目前,规则尚不明确。 – 2012-07-18 12:02:35
@ SeanB.Durkin:要求说明很好。 – 2012-07-18 13:00:03