输入
<person>
<address>
<city>NY</city>
<state></state>
<country>US</country>
</address>
<other>
<gender></gender>
<age>22</age>
<weight/>
</other>
</person>
Run Code Online (Sandbox Code Playgroud)
我只想从“其他”节点中删除空元素,而且“其他”下的标签也不固定。
输出
<person>
<address>
<city>NY</city>
<state></state>
<country>US</country>
</address>
<other>
<age>22</age>
</other>
</person>
Run Code Online (Sandbox Code Playgroud)
我是 xslt 的新手,所以请帮助..
这个转变:
<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="other/*[not(node())]"/>
</xsl:stylesheet>
Run Code Online (Sandbox Code Playgroud)
当应用于提供的 XML 文档时:
<person>
<address>
<city>NY</city>
<state></state>
<country>US</country>
</address>
<other>
<gender></gender>
<age>22</age>
<weight/>
</other>
</person>
Run Code Online (Sandbox Code Playgroud)
产生想要的正确结果:
<person>
<address>
<city>NY</city>
<state/>
<country>US</country>
</address>
<other>
<age>22</age>
</other>
</person>
Run Code Online (Sandbox Code Playgroud)
解释:
身份规则“按原样”复制每个匹配的节点,并为其选择执行。
覆盖身份模板的唯一模板与作为其子节点other
且没有子节点的任何元素匹配(为空)。由于该模板没有主体,因此这会有效地“删除”匹配的元素。