我不确定这是否正是您要查找的内容(请记住始终包含输入和所需的输出 XML),但是在这里您有一个通用模板,它在应用任何其他处理之前递归地查找空节点和属性(如果您不需要属性检查,只需删除“或”之后的部分):
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:i="whatever">
<xsl:output method="xml" indent="yes"/>
<!-- +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<!-- +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
<xsl:template match="/*">
<xsl:copy>
<xsl:variable name="nil">
<xsl:apply-templates select="." mode="nil"/>
</xsl:variable>
<xsl:if test="$nil='true'">
<xsl:attribute name="i:nil">true</xsl:attribute>
</xsl:if>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
<!-- +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
<xsl:template match="*" mode="nil">
<xsl:choose>
<xsl:when test="string-length(.)=0 or @*[string-length(.)=0]">
<xsl:value-of select="'true'"/>
</xsl:when>
<xsl:otherwise>
<xsl:apply-templates select="*" mode="nil"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
零:输入:
<TEST>
<Child test="aaa" secondtest="">asdf</Child>
</TEST>
输出:
<TEST xmlns:i="whatever" i:nil="true">
<Child test="aaa" secondtest="">asdf</Child>
</TEST>
没有零:输入+输出(什么都不做):
<TEST>
<Child test="aaa" secondtest="bbb">asdf</Child>
</TEST>