0

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

Siva Charan
  • 17,940
  • 9
  • 60
  • 95
  • All possible duplicates: http://stackoverflow.com/questions/2244101/xsl-document-file-not-found and http://stackoverflow.com/questions/3420762/empty-xml-split-out-default-xml and http://stackoverflow.com/questions/3700285/test-if-document-is-well-formed-before-parsing –  Feb 16 '11 at 15:53
  • Sorry Alejandro, it is not duplicate question. I need to resolve this using XSLT version 1.0 and not on 2.0... Please help me out on this. – Siva Charan Feb 16 '11 at 16:01

1 Answers1

3

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.

Michael Kay
  • 156,231
  • 11
  • 92
  • 164
  • Can we do something like set param variable to '0' globally and then calling apply-template .. from there check something like this condition *[not(*)] if so then will set the global param value to '1' .. finally based on the global value, I can overcome my problem but I not able to implement this... If my assumption is correct, please implement it & show me... If not then correct me... – Siva Charan Feb 16 '11 at 16:16
  • Hi Mic, Do you got my assumption steps? I need to put some more points clearly.... – Siva Charan Feb 16 '11 at 16:29