document()函数和包含%的文件名

时间:2018-04-18 07:14:52

标签: xslt saxon

我正在努力处理一个处理文件列表的XSL。对于大多数文件,该函数可以正常工作,但是当文件名包含%符号时会崩溃。

这是我的XSLT:

   <xsl:template match="file">
     <xsl:variable name="sourcedoc" select="concat('file:///', text())"/>
     <madcapfile>
        <xsl:attribute name="filename"><xsl:value-of select="text()"/></xsl:attribute>
        <xsl:apply-templates select="document($sourcedoc)"/>
     </madcapfile>
   </xsl:template>

和我的XML以及文件列表:

<file>the 7% solution.xml</file>

在Saxon处理此问题时,它抱怨它无法找到文件 &#34; 7olution.xml&#34;

XLST将此指定为输出:

<xsl:output method="xml"  escape-uri-attributes="no" />

感谢escape-uri-attributes =&#34; no&#34;,URI在转换后的XML中正确显示。我还没有能够找到适用于document()的等效函数。 我怎样才能确保document()将%视为文字而不是转换它?

2 个答案:

答案 0 :(得分:1)

尝试使用<xsl:variable name="sourcedoc" select="concat('file:///', encode-for-uri(.))"/>docdocument等函数可以使用URI,百分号需要转义,所以如果您的文件名带有百分号,建议的函数就会将其转义根据需要,以便docdocument功能起作用。

答案 1 :(得分:1)

我再次看到这个,并认为如果你的输入包含文件名而不是URI,特别是如果文件名有任意风格,处理它们的最佳方法可能是使用EXPath文件模块。那就是:

parse-xml(file:read-text($filename))

或者

doc(file:path-to-uri($filename))

在一般情况下将文件名转换为URI的逻辑非常复杂,当然比在前面粘贴file:///更复杂,而EXPath文件模块试图解决这个问题,标准函数库没有。

相关问题