How to check XML has a node or it is an empty file

2019-08-28 20:45发布

Please help on this issue.

I want to check an XML contains a node or not [OR] it is an empty file using XSLT.

My XML is:

<mynode>
   <node1>testing</node1>
</mynode>

I want to write XSLT code something like this:

<xsl:choose>
  <xsl:when test="document('')/mynode">
    file is empty
  </xsl:when>
  <xsl:otherwise>
    file has nodes
  </xsl:otherwise>
</xsl:choose>

Please any one can help me on this.....

标签: xslt
1条回答
聊天终结者
2楼-- · 2019-08-28 21:20

If the file doesn't contain an element, then it is not an XML file, and XML parsing will therefore fail. Different XSLT 1.0 processors react differently to this (as permitted by the spec). In XSLT 2.0 you can use doc-available() to protect yourself, but this is not available in 1.0. In fact, there's nothing you can do about this in 1.0 without knowing details of your particular XSLT processor.

查看更多
登录 后发表回答