如何使用 XSL 检查外部文件是否存在? [英] How do I check for the existence of an external file with XSL?

查看:23
本文介绍了如何使用 XSL 检查外部文件是否存在?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为此我找到了很多引用 Java 和 C 的示例,但是我如何或可以如何使用 XSL 检查外部文件的存在.

首先,我意识到这只是一个片段,但它是一个巨大样式表的一部分,所以我希望这足以说明我的问题.

 <xsl:template name="ReceivedSMS"><!-- 设置/声明SMSname"变量(本地,每个实例评估)--><xsl:variable name="SMSname"><xsl:value-of select="follow-sibling::Name"/></xsl:变量><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><!-- 单元格包含扬声器"图标--><fo:table-cell display-align="after"><fo:block text-align="start"><fo:external-graphic src="../images/{$SMSname}.jpg" content-height="0.6in"/>

我想做的是放在一个if"语句中,放在 {$SMSname}.jpg 行的周围.即:

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

由于分组"等原因,我使用的是 XSLT 2.0.我希望这是可以做到的.我更希望这是简单的事情.

与往常一样,提前感谢您的帮助.哦

解决方案

不,这不能使用 XSLT 2.0/XPath 2.0 完成.

XSLT 2.0 函数 unparsed-text-available() 仅适用于定位文本文件,即使存在指定 URI 的文本文件,此函数也可能返回 false(),因为它还必须读取文件内容并检查它是否仅包含允许的字符.

来自 规范:

unparsed-text-available 函数决定了对具有相同参数的 unparsed-text 函数的调用是否会返回一个字符串.

如果第一个参数是空序列,则函数返回 false.如果第二个参数是一个空序列,则该函数的行为就像省略了第二个参数一样.

在其他情况下,如果调用具有相同参数的未解析文本会成功,则该函数返回 true,如果调用具有相同参数的未解析文本失败并出现不可恢复的动态错误,则该函数返回 false.

注意:

这要求 unparsed-text-available 函数实际上应该尝试读取由 URI 标识的资源,并检查它是否正确编码并且不包含在 XML 中无效的字符"

引文结束.

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

解决方案

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.

这篇关于如何使用 XSL 检查外部文件是否存在?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆