Tutorial :Empty XML split out default XML


I have a XML passing to the XSL which gives the standard output. There is chance that i will get the XML as empty some times based on some cases. Is there any way if we have empty XML and get the standard or default out put in XSL ?


Is there any way if we have empty XML and get the standard or default out put in XSL ?

You can never have "empty XML".

By definition, a well-formed XML document must have a top element. If an XSLT 1.0 transformation is being applied on any non-well-formed XML document, any compliant XSLT 1.0 processor must produce an error and no processing is done.


From http://www.w3.org/TR/xpath-functions/#func-doc-available

If fn:doc($uri) returns a document node, this function returns true. If $uri is not a valid xs:anyURI, an error is raised [err:FODC0005]. Otherwise, this function returns false.

And from http://www.w3.org/TR/xpath-functions/#func-doc

One possible processing model for this function is as follows. The resource identified by the URI Reference is retrieved. If the resource cannot be retrieved, an error is raised [err:FODC0002]. The data resulting from the retrieval action is then parsed as an XML document and a tree is constructed in accordance with the [XQuery 1.0 and XPath 2.0 Data Model]. If the top-level media type is known and is "text", the content is parsed in the same way as if the media type were text/xml; otherwise, it is parsed in the same way as if the media type were application/xml. If the contents cannot be parsed successfully, an error is raised [err:FODC0002]. Otherwise, the result of the function is the document node at the root of the resulting tree. This tree is then optionally validated against a schema.

So, it looks like you could do this (I'm being cautious because of the words "One possible processing model"): This stylesheet:

<xsl:stylesheet version="2.0"   xmlns:xsl="http://www.w3.org/1999/XSL/Transform">      <xsl:param name="pURI" select="'B.xml'"/>      <xsl:template match="/">          <DefaultResult>              <xsl:if test="doc-available($pURI)">                  <xsl:apply-templates select="doc($pURI)/*"/>              </xsl:if>          </DefaultResult>      </xsl:template>      <xsl:template match="@*|node()">          <xsl:copy>              <xsl:apply-templates select="@*|node()"/>          </xsl:copy>      </xsl:template>  </xsl:stylesheet>  

With any input and B.xml been an empty document, Saxon output:


And this alternate information:

Error on line 1 column 1 of B.xml:    SXXP0003: Error reported by XML parser: Premature end of file.  

Altova Output:

<DefaultResult>      <Entry type="Folder">          <Name/>          <Date/>          <Info>              <URI>B.xml</URI>              <Comment/>              <Files>0</Files>              <CompressedSize>0</CompressedSize>              <UncompressedSize>0</UncompressedSize>              <Ratio>0</Ratio>              <ContainsEncryptedFiles>false</ContainsEncryptedFiles>          </Info>      </Entry>  </DefaultResult>  


One way is to check whether the root node exists in XSL and if not print something out.

But when i tried tsimiliar thin in Perl the script hung.So i think you have to check size and then apply the XSL


If your root tag is an empty node set something should work like this...

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">    <xsl:template match="/">      <xsl:choose>         <xsl:when test='*[not(*)]'>           Empty        </xsl:when>         <xsl:otherwise>           Full         </xsl:otherwise>       </xsl:choose>     </xsl:template>   </xsl:stylesheet>  

... If the file is empty (or at least doesn't contain a root element) your XSLT processor will most likely throw and exception.

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »