这种转变:
<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=
"attributes[not(node())]
|
attribute[not(attributeName/text())]
"/>
</xsl:stylesheet>
当这个XML文档施加(注意最后的空<attributes>
和attribute/attributeName
):
<attributes>
<attribute>
<attributeName>agenda-group</attributeName>
<value>generic</value>
</attribute>
<attribute>
<attributeName>auto-focus</attributeName>
<value>true</value>
</attribute>
<attribute>
<attributeName>no-loop</attributeName>
<value>true</value>
</attribute>
<attribute>
<attributeName>salience</attributeName>
<value>73</value>
</attribute>
<attribute>
<attributeName></attributeName>
<value></value>
</attribute>
<attributes/>
</attributes>
产生想要的结果(空元素忽略 - 不复制):
<attributes>
<attribute>
<attributeName>agenda-group</attributeName>
<value>generic</value>
</attribute>
<attribute>
<attributeName>auto-focus</attributeName>
<value>true</value>
</attribute>
<attribute>
<attributeName>no-loop</attributeName>
<value>true</value>
</attribute>
<attribute>
<attributeName>salience</attributeName>
<value>73</value>
</attribute>
</attributes>
说明:身份规则(即副本每个节点“原样”)是由一个单一的模板重写该匹配想要的“空”元素并且没有主体,所以他们被简单地忽略/忽略。
我不知道由OP所需的输入文件是为你承担。请参阅我的解答。 –
@empo:是的,这是我对这个问题的解释。如果值为空/缺少,名称 - 值对仍然有意义。 –