Xslt 如果子节点为空,请删除父节点
源XML:Xslt 如果子节点为空,请删除父节点,xslt,Xslt,源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</Condit
<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>
波尔
05
32
04
如果必须删除属性为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>
波尔
32
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>
波尔
05
32
04
和这一转变:
<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>
波尔
05
32
04
和在你的明显规则中有一些矛盾。您的模板和标题建议(但您在叙述中没有明确说明),如果一个元素没有属性,也没有子元素,那么应该将其删除。但是你关于PRule被淘汰的说法与此相矛盾,因为PRule有属性。一旦你澄清了规则,这将是一个简单的样式表。但是目前,规则还不明确。@SeanB.Durkin:要求陈述得很好。你的明显规则中存在一些矛盾。您的模板和标题建议(但您在叙述中没有明确说明),如果一个元素没有属性,也没有子元素,那么应该将其删除。但是你关于PRule被淘汰的说法与此相矛盾,因为PRule有属性。一旦你澄清了规则,这将是一个简单的样式表。但目前,规则还不明确。@SeanB.Durkin:要求陈述得很好。