Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old September 15th, 2003, 10:10 AM
Registered User
 
Join Date: Sep 2003
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default document() - check if a file exists

Hi,

I have a piece of XSLT which opens a set of documents
in a for-each block. However I don't know at the time
the script runs whether or not all the files exist.

Currently this isn't a problem because Saxon has recoverable
errors, where the processor continues onto the next iteration.
However MS XML Pack doesn't handle this and craps out.

Is there a way to check whether or not the document() has actually
found the file before continuing? I've tried:

<xs:if test="document()">
....

But this doesn't fail if the file is not present.

Cheers,

Nick

 
Old September 15th, 2003, 11:20 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 147
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to armmarti
Default

Any XSLT processor which conforms to the XSLT spec must deal with the document() function in this way if there is a problem with retrieving the XML document:
it may signal an error; if it doesn't, it must recover the error by returning an empty nodeset.
So, if, as you're saying, "this doesn't fail if the file is not present", then the function must return an empty nodeset, and you can simply test it by this:
Code:
        <xsl:choose>
            <xsl:when test="document('abcd.xsl')">
                The file exists!
            </xsl:when>
            <xsl:otherwise>
                Can't find the file...
            </xsl:otherwise>
        </xsl:choose>
Regards,
Armen
 
Old September 19th, 2007, 05:50 AM
Registered User
 
Join Date: Sep 2007
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi

Unfortunately, MSXML uses XSLT 1.0 and it doesn't seem to be upgraded.

So my solution looks like this:
- new template "xml-file-exists" which opens VALID SERVICE_URL with file parameter. That URL returns VALID XML with root element and text node, saying "true" or false.
- use that template to generate variable and the pass variable to xsl:choose
- inside xsl:when at "true" use document()

The code
=======
  <xsl:template name="xml-file-exists">
    <xsl:param name="url"/>
    <xsl:variable name="test-url">http://SERVICE_URL?url=<xsl:value-of select="$url"/>
    </xsl:variable>
    <xsl:copy-of select="document($test-url)//file"/>
  </xsl:template>


  <xsl:variable name="file-exists">
    <xsl:call-template name="xml-file-exists">
      <xsl:with-param name="url" select="$options.xml"/>
    </xsl:call-template>
  </xsl:variable>

  <xsl:choose>
     <xsl:when test="$file-exists='true'">
     ...xsl code using document()...
     </xsl:when>
     <xsl:otherwise>
     ...xsl code without document()
     </xsl:otherwise>
  </xsl:choose>






Similar Threads
Thread Thread Starter Forum Replies Last Post
Check if a file exists on network darrenb Access VBA 2 March 27th, 2007 07:15 PM
hei,anyone know how to check a remote file exists lsj113 C# 2005 3 November 28th, 2006 02:55 AM
Check if file starting with a pattern exists or no desperado1306 Excel VBA 1 May 30th, 2006 08:28 AM
check first to see if the file exists crmpicco Classic ASP Professional 2 December 1st, 2005 12:34 PM
How to check if connection already exists Ciarano VB How-To 3 March 9th, 2004 10:24 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.