Forums Register Login
Custom URIResolver
I need a custom URIResolver for use with Saxon but know too little about Java to tackle it, and hope you might be able to help.
Specifically, I need a URIResolver that will respond to FileNotFoundException by copying the file 'null.xml' from a known location and then responding, that the file was found.
So, when an XSLT calls, with document(), a file that doesn't exist, its processing continues uninterupted because, at the very least it will find a copy of null.xml in that place, which the XSLT can then write to..
I'm wondering if the following is partly what I need. In my ignorance I'm expecting that this requires wrapping in a
"class MyResolver implements URIResolver {}"
but that's a guess.

(I know enough that I can compile java and use this custom URIResolver with Saxon)
Hello David,
May I ask you to add some spaces into your display name so that it complies with our naming policy?
Thanks, and welcome to the 'ranch!
All resolve has to do is to return a Source - looking at the Source JavaDocs we find that there are several classes that implement Source. The javax.xml.transform.stream.StreamSource for example can be created from a wide variety of inputs, perhaps you could use one of them.
Forgive me, my knowledge of Java is *very limited*
I need not simply to return the location of a source but copy a null file to the location requested. This file will then be written to as if it already existed.
It's not just input I'm needing, it's the creation of a copy of null.xml to requested.xml
Then I'm expecting
try {s = systemURIResolver.resolve(href, base);} will work.
The problem, ~obviously, is that XSLT (Saxon) can't work with files that don't exist.
Therefore it must find at least
<?xml version="1.0"?>
to be successful.
There is a way for XSLT to detect a file doesn't exist but that is of no use to me.
I'm not clear on what you are trying to do. Is a custom URIResolver all you need? If so, then it does not have to do this manipulation with the creation of a null.xml file, it just has to return a Source, like I said.

Or is there some other requirement for creation of null.xml
I expect I'm not being clear. (a little out of my depth
What I know is that I've been advised that a custom URIResolver is what I need. This from Micheal Kay author of the Saxon processor.
I grabbed the idea of the java code required from other talk of URIResolver as it looked like it made sense.
What I need is that the XSLT processor is able to write to a file suggested to it and it can't do that if the file doesn't exist.
Accepting what you suggest I've constructed the following which crashes with one error..
MyURIResolver.java:5: illegal start of expression
public Source resolve(href, base) {

If you can suggest a correction for that error then I'll see if it does work for me.

Actually this doesn't crash. It just doesn't compile.
The reason is that you haven't specified types for the method arguments. It should probably be something like "public Source resolve(String href, String base)"
Thanks Lasse, but I'm still getting the same compilation error.

What I need is that the XSLT processor is able to write to a file suggested to it and it can't do that if the file doesn't exist.

I don't understand this requirement - if you are writing to a file, it does not have to previously exist - all you need is a valid path and filename, the file will be created.
A URIResolver creates a source - that can be based on a file to be read or other sources of character streams and has nothing to do with defining a file to write to.
That is an interesting question, Bill, which prompted me to try simply writing to the file.

However, it doesn't work and I think it is due to XSLT being a compiled language. The error I get is 'Variable name must be a valid QName' which is directly due to the file doesn't exist and consequently the xsl can't be compiled.
This is consistent with Micheal Kay's suggesting that I would need to create a custom URIResolver.
Evidently this is more about the file not existing at compilation, than it is about the output.
Are you sure that

is what you want. That doesn't look right to me - of course I struggle with XSLT every time I have to use it.
Isn't that error saying that "document($filename)" is not a valid name for a xsl:variable???
According to the reference I am looking at "The name of a variable is governed by the QName rules of XML Namespaces" ... Names can't begin with digits or most special characters.... They also cannot contain most special characters"
So the error has nothing to do with the existance of the file.
Also, XSLT is an interpreted language in most cases.
Quite right, but then

simply gives 'Source file input does not exist'.
Regardless of my confusion, I think we have to acknowledge Micheal Kay's suggestion. I understand this is the same MK who is the name at the W3C
from http://www.w3.org/TR/#last-call
XSL Transformations (XSLT) Version 2.0
12 November 2003, Michael Kay
Any suggestions why the java code doesn't compile?

You might want to use a real IDE -- it would tell you about this kind of syntax errors quicker than you can say "Java"
[ April 13, 2004: Message edited by: Lasse Koskela ]
Wink, wink, nudge, nudge, say no more ... https://richsoil.com/cards

This thread has been viewed 5404 times.

All times above are in ranch (not your local) time.
The current ranch time is
Jan 18, 2018 15:10:29.