Re: [xsl] Checking if a file exists

Subject: Re: [xsl] Checking if a file exists
From: "Michael Kay michaelkay90@xxxxxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Fri, 19 Apr 2024 00:06:04 -0000
Your main problem here is that you are thinking about things very
procedurally: you are thinking about creating files and checking for the
existence of files in terms of a world that is changing as your code executes,
while XSLT is designed to create the illusion of a stateless world that
doesn't change. The clue is that you use a lot of temporal language in your
description: "while creating", "and then", "look back", "earlier", "did
exist". Using temporal language like this says you haven't grasped the
processing model.

To prevent you writing stateful code like this, XSLT has rules like: you can't
write two documents with the same URI; you can't read a document and write a
document with the same URI, etc. One of the rules is that if doc-available()
returns false, that means the file doesn't exist, and that's a property of the
unchanging world, which means the system will ensure that the file continues
to not exist, which means xsl:result-document is not allowed to write it.

You can bypass all these rules using the EXPath file module; the file module
allows you to write code that is stateful. The consequence is that the results
can be unpredictable; if you combine things like file:exists with
xsl:result-document then you can get very surprising results (Saxon-EE
executes xsl:result-document asynchronously in a separate thread, which means
that you might think you are calling file:exists after calling
xsl:result-document, but actually it executes first). You can get round that
by disabling multi-threading. But you're cutting against the grain when you do
that; you're not using the language the way it was designed.

Now, instead of trying to understand why your code doesn't work, please
explain the problem you are trying to solve, and we'll see if we can help you
solve it in a declarative way.

Michael Kay
Saxonica



> On 18 Apr 2024, at 23:27, dvint@xxxxxxxxx
<xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx> wrote:
>
> I need to take a list of XML files and rename them based upon values
provided by the writer. I know that my list has duplicate name tokens for
different files in the list. I do get duplicate names when generated.
>
> I'm trying to trap this situation and handle this while creating the files,
I have a template with this:
>
> ...
>
> '<xsl:value-of select="$newFile"/>' found '<xsl:value-of
select="doc-available($newFile)"/>'
>
> <xsl:choose>
> 	<xsl:when test="doc-available($newFile)">
> 		<xsl:message>[ERROR] Renaming <xsl:value-of select="$base-filename"/> to
Duplicate id= <xsl:value-of select="$newName"/></xsl:message>
> 	</xsl:when>
> 	<xsl:otherwise>
> 		<xsl:result-document
> 			href="{$rename-outPath}/{$newName}.dita"
> 			method="xml">
> 			<doc>
> 				<p>new file</p>
> 			</doc>
> 		</xsl:result-document>
> 	</xsl:otherwise>
> </xsl:choose>
>
> ...
>
> I get this output
>
'file:/Users/danvint/pubsrc/_src-data-files/asciidoc-processing/Ixia-Source/d
ita-files/test4/dita/rename/fs-concept-nest5.dita' found 'false'
>
> and then the xslt fails because the file actually exists. I can look back at
these messages and I see many files earlier I already created a
fs-concept-nest5.dita file. So it did exist in this new folder.
>
> What am I missing?
>
> ..dan

Current Thread