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

2019-08-28 20:40发布

问题:

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.....

回答1:

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.



标签: xslt