如何测试节点及其子节点是否为空?

时间:2018-12-10 06:22:35

标签: xml xslt-1.0

我是否可以测试像这样的空节点?

<Address>
  <Street></Street>
  <Building></Building>
  <Postcode></Postcode>
  <Town></Town>
  <State></State>
  <Country></Country>
</Address>

1 个答案:

答案 0 :(得分:0)

您可以通过以下方法测试节点及其子节点是否为空:

<xsl:if test="normalize-space(.)=''">All nodes empty: true&#xA;</xsl:if>

或者,您可以通过以下方法测试其是否为空节点:

<xsl:if test="normalize-space(*)=''">Contains an empty node: true&#xA;</xsl:if>

针对您的输入XML运行此样式表时:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="1.0">

    <xsl:output omit-xml-declaration="yes"/>

    <xsl:template match="Address">
        <xsl:if test="normalize-space(.)=''">All nodes empty: true&#xA;</xsl:if>
        <xsl:if test="normalize-space(*)=''">Contains an empty node: true&#xA;</xsl:if>
    </xsl:template>

</xsl:stylesheet>

它将产生:

All nodes empty: true
Contains an empty node: true

action中查看。

相关问题