Re: [xsl] Calling a Java extension from XSLT using Saxon B

Subject: Re: [xsl] Calling a Java extension from XSLT using Saxon B
From: Ganesh Babu N <nbabuganesh@xxxxxxxxx>
Date: Wed, 24 Jun 2009 10:24:15 +0530
Another simple method. No need to write a java class.

1. You have to declare this namespace xmlns:file="java.io.File"
2. This is the input format: <file name="test.jpg"/>

<xsl:template match=file">
<xsl:variable name="file" select="resolve-uri(@name, base-uri(.))"
as="xs:string"/>
<xsl:if test="not(file:exists(file:new($file)))">
          <xsl:value-of select="@name"/><xsl:message>file missing /
incorrect name</xsl:message>
</xsl:if>
</xsl:template>

Regards,
Ganesh


On Wed, Jun 24, 2009 at 3:27 AM, Michael Kay<mike@xxxxxxxxxxxx> wrote:
>
> It's always useful to use the -TJ option when debugging this kind of
> problem.
>
> I think your problem is that you used the -jar option on the command line.
> This causes the classpath to be ignored. You need to use the
>
> java -cp ... net.sf.saxon.Transform
>
> way of invoking the transformation.
>
> I'm afraid this is an elephant trap that many people fall into. However
> prominently I put the warnings in the documentation, they still fall in.
>
> Incidentally, questions about specific XSLT products are best asked on a
> product specific list. For Saxon that's the saxon-help list which you can
> find via the saxon project on SourceForge.
>
> Regards,
>
> Michael Kay
> http://www.saxonica.com/
> http://twitter.com/michaelhkay
>
>> -----Original Message-----
>> From: Blaine McNutt (rbmcnutt) [mailto:rbmcnutt@xxxxxxxxx]
>> Sent: 23 June 2009 20:20
>> To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
>> Subject: [xsl] Calling a Java extension from XSLT using Saxon B
>>
>> I am not a Java programmer, but I would like to use an
>> extension to check for the existence of an image file
>> referenced by an XML file (as I believe that is the only way
>> to check for the existence of a non-XML file). This topic has
>> been discussed a lot on various aliases, and despite my best
>> efforts, I'm unable to unravel the mystery.
>>
>> I've included what I believe to be relevant information.
>>
>> Error Message:
>> --------------
>> Error at xsl:value-of on line 15 column 48 of
>> nxs-test-image-exists.xsl:
>>   XPST0017: XPath syntax error at char 20 on line 15 in
>> {nxs:fileExist(@osrc)}:
>>     Cannot find a matching 1-argument function named
>> {java:FileExist}fileExist() Failed to compile stylesheet. 1
>> error detected.
>>
>> Saxon-B 9.1.7 J stored in xmlsrc/xTools/saxon9.
>> -----------------------------------------------
>>
>> Java program (compiled and the FileExist.class file stored in
>> xmlsrc/xTools/saxon9/classes)
>>
>> /* Source:
>> http://p2p.wrox.com/xslt/71563-xslt-check-if-directory-exist.html */
>>
>> import java.io.File;
>>
>> public class FileExist {
>>
>>     private static boolean exist;
>>
>>     public static boolean fileExist(String myfile) {
>>
>>         exist = new File(myfile).exists();
>>
>>         return exist;
>>
>>     }
>> }
>>
>> XML Snippet for img element
>> ---------------------------
>>
>> ...
>> <img osrc="../../images/1982.jpg" />
>>
>> ...
>>
>> XSLT
>> ------
>>
>> <?xml version="1.0" encoding="UTF-8"?>
>>
>> <xsl:stylesheet version="2.0"
>>                 xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
>>                 xmlns:nxs="java:FileExist">
>>
>> <xsl:output method="xml"/>
>>
>> <xsl:template match="/">
>>                 <xsl:apply-templates /> </xsl:template>
>>
>> <xsl:template match="img">
>>                 <xsl:value-of select="nxs:fileExist(@osrc)"
>> /> </xsl:template>
>>
>> </xsl:stylesheet>
>>
>> Korn Shell script/command line call:
>> ------------------------------------
>>
>> # define saxon command line
>> saxon="java -classpath xTools/saxon9/classes; -jar -Xmx1G
>> xTools/saxon9/saxon9.jar -tree:tiny -dtd:off -ext:on "
>> # call transform
>> $saxon deliverables/help/helpFile.xml
>> xslt/nxs-test-image-exists.xsl > imageerror.xml
>>
>>
>> Thank you for any insights you might offer.
>>
>> Blaine

Current Thread