XSLT 先删除一些子节点,如果没有子节点,则删除父节点
XSLT remove some child node first then remove parent node if there are no child node left
我只是花了几天时间寻找解决方案,没有找到任何解决方案。
所以我有这个 xml,我想在上面应用两个 <xsl:template match="">
,第二个取决于第一个:
<xml>
<parent>
<data1>data</data1>
<data2>data</data2>
<childNodes>
<childNode>
<child> Value 2 </child>
</childNode>
</childNodes>
</parent>
<parent>
<data1>data</data1>
<data2>data</data2>
<childNodes>
<childNode>
<child> Value 1 </child>
</childNode>
<childNode>
<child> Value 2 </child>
</childNode>
<childNode>
<child> Value 3 </child>
</childNode>
</childNodes>
</parent>
</xml>
我的 objective 是:
首先删除 when child!='Value 1' 使用这个:
<xsl:template match="childNode[child!='Value 1']/>
其次,如果内部没有更多父节点,则删除整个父节点,使用:
<xsl:template match="xml/parent[not(childNodes/childNode)]"/>
我找不到任何方法可以一个接一个地应用这两个 <xsl:template match="">
,通常有一种方法可以将它们组合成一个。我没有在这里管理这个
提前感谢您的意见。
本
不能按照你想要的顺序进行,因为在XSLT processing model中父节点先于子节点处理。
但是,您可以轻松删除没有子节点(或更确切地说是后代)的父节点,而不是将要删除的节点:
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="parent[not(childNodes/childNode/child='Value 1')]"/>
<xsl:template match="childNode[child!='Value 1']"/>
</xsl:stylesheet>
我只是花了几天时间寻找解决方案,没有找到任何解决方案。
所以我有这个 xml,我想在上面应用两个 <xsl:template match="">
,第二个取决于第一个:
<xml>
<parent>
<data1>data</data1>
<data2>data</data2>
<childNodes>
<childNode>
<child> Value 2 </child>
</childNode>
</childNodes>
</parent>
<parent>
<data1>data</data1>
<data2>data</data2>
<childNodes>
<childNode>
<child> Value 1 </child>
</childNode>
<childNode>
<child> Value 2 </child>
</childNode>
<childNode>
<child> Value 3 </child>
</childNode>
</childNodes>
</parent>
</xml>
我的 objective 是:
首先删除 when child!='Value 1' 使用这个:
<xsl:template match="childNode[child!='Value 1']/>
其次,如果内部没有更多父节点,则删除整个父节点,使用:
<xsl:template match="xml/parent[not(childNodes/childNode)]"/>
我找不到任何方法可以一个接一个地应用这两个 <xsl:template match="">
,通常有一种方法可以将它们组合成一个。我没有在这里管理这个
提前感谢您的意见。
本
不能按照你想要的顺序进行,因为在XSLT processing model中父节点先于子节点处理。
但是,您可以轻松删除没有子节点(或更确切地说是后代)的父节点,而不是将要删除的节点:
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="parent[not(childNodes/childNode/child='Value 1')]"/>
<xsl:template match="childNode[child!='Value 1']"/>
</xsl:stylesheet>