首页 文章

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

提问于
浏览
0

如果空的话我可以测试这样的节点吗?

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

1 回答

  • 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中查看 .

相关问题