2
源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節點。
你的表面規則存在一些矛盾。你的模板和標題建議(但你在敘述中沒有說清楚),如果一個元素沒有屬性並且沒有子元素,那麼它應該被刪除。但是PRule被淘汰的說法與此相矛盾,因爲PRule具有屬性。 – 2012-07-18 12:01:54
這將是一個簡單的樣式表,一旦您澄清了規則。但目前,規則尚不明確。 – 2012-07-18 12:02:35
@ SeanB.Durkin:要求說明很好。 – 2012-07-18 13:00:03