Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > XML > XSLT
Password Reminder
| FAQ | Members List | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old September 15th, 2003, 10:10 AM
Registered User
Join Date: Sep 2003
Location: London, , United Kingdom.
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default document() - check if a file exists


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.



Reply With Quote
  #2 (permalink)  
Old September 15th, 2003, 11:20 AM
Friend of Wrox
Join Date: Jun 2003
Location: Nor Hachin, Kotayk, Armenia.
Posts: 147
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to armmarti

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:
            <xsl:when test="document('abcd.xsl')">
                The file exists!
                Can't find the file...
Reply With Quote
  #3 (permalink)  
Old September 19th, 2007, 05:50 AM
Registered User
Join Date: Sep 2007
Location: , , .
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts


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:copy-of select="document($test-url)//file"/>

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

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

Reply With Quote

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

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

All times are GMT -4. The time now is 08:51 AM.

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