views:

185

answers:

3

I've found a lot of examples that reference Java and C for this, but how do I, or can I, check for the existence of an external file with XSL.

First, I realize that this is only a snippet, but it's part of a huge stylesheet, so I'm hoping it's enough to show my issue.

    <!-- Use this template for Received SMSs -->
<xsl:template name="ReceivedSMS">
    <!-- Set/Declare "SMSname" variable (local, evaluates per instance) -->
    <xsl:variable name="SMSname">
        <xsl:value-of select=" following-sibling::Name"/>
    </xsl:variable>
    <fo:table font-family="Arial Unicode MS" font-size="8pt" text-align="start">
        <fo:table-column column-width=".75in"/>
        <fo:table-column column-width="6.75in"/>
        <fo:table-body>
            <fo:table-row>
                <!-- Cell contains "speakers" icon -->
                <fo:table-cell display-align="after">
                    <fo:block text-align="start">
                        <fo:external-graphic src="../images/{$SMSname}.jpg" content-height="0.6in"/>

What I'd like to do, is put in an "if" statement, surronding the {$SMSname}.jpg line. That is:

                     <fo:block text-align="start">
                        <xsl:if test="exists( the external file {$SMSname}.jpg)">
                            <fo:external-graphic src="../images/{$SMSname}.jpg" content-height="0.6in"/>                            
                        </xsl:if>
                        <xsl:if test="not(exists( the external file {$SMSname}.jpg))">
                            <fo:external-graphic src="../images/unknown.jpg" content-height="0.6in"/>                            
                        </xsl:if>
                    </fo:block>                       

Because of "grouping", etc., I'm using XSLT 2.0. I hope that this is something that can be done. I hope even more that it's something simple.

As always, thanks in advance for any help. LO

A: 

EDIT: Use unparsed-text-available function. It is part of xslt 2.0, but not XQuery or standalone XPath.

I've left my previous answer here so you can follow the trail of uncertainty...

I don't believe there is a way of doing this in XSLT using the standard functions. You can do it using extension functions, as described here, for java.

There is the unparsed-text-available function, but I'm unsure if this is a standard function. There's an example of it's usage at Zvon. The unparsed-text-available is mentioned here as being part of xslt 2.0, and is supported in Saxon.

mdma
See my answer for an explanation why this cannot be done.
Dimitre Novatchev
+1  A: 

No, this cannot be done using XSLT 2.0/XPath 2.0.

The XSLT 2.0 function unparsed-text-available() is only suitable for locating text files and even if a text file with the specifies URI exists this function may return false(), because it also must read the contents of the file and check that it only contains allowed characters.

From the *spec*:

"The unparsed-text-available function determines whether a call on the unparsed-text function with identical arguments would return a string.

If the first argument is an empty sequence, the function returns false. If the second argument is an empty sequence, the function behaves as if the second argument were omitted.

In other cases, the function returns true if a call on unparsed-text with the same arguments would succeed, and false if a call on unparsed-text with the same arguments would fail with a non-recoverable dynamic error.

Note:

This requires that the unparsed-text-available function should actually attempt to read the resource identified by the URI, and check that it is correctly encoded and contains no characters that are invalid in XML "

End of quotation.

Dimitre Novatchev
A: 

A proposed File Module EXPath specification would support file-system functions such as this (file:exists() in the spec) as standard XPath extension functions. There isn't yet an XSLT implementation for this, but its worth watching.

pgfearo