【问题标题】:encode-for-uri and absolute Windows paths为 uri 编码和绝对 Windows 路径
【发布时间】:2018-04-26 09:03:02
【问题描述】:

我有一个 XML 文件,其中包含以文件名开头的部分:

<madcapfile filename="C:\1\Outputpath\The 7% solution.xlf">

每个部分都必须保存到单独的文件中。 这是我的 XSLT:

<xsl:template match="madcapfile">
  <xsl:variable name="file1" select="concat('file:///',@filename)"/>
  <xsl:variable name="file2" select="encode-for-uri($file1)"/>
  <xsl:variable name="file3" select="concat('file:///',replace(@filename,'%','%25'))"/>
  <xsl:result-document method="xml" href="{$file2}">  
    <xsl:apply-templates select="node()"/>
  </xsl:result-document>
</xsl:template>

变量 file1、file2、file3 是我迄今为止的尝试。 变量 file1 在所有文件的正确位置创建文件,但文件名中带有 % 的文件除外。
变量 file3 在所有文件的正确位置创建文件,因此这是一个可行的解决方案。

使用变量 file2 会出错:XSLT 处理器 (Saxon 9.7) 尝试将文件写入

C:\Path-to-XSLT\C:\1\Outputpath\The 7% solution.xlf  

即看起来 encode-for-uri 将其输入视为相对路径,即使它以“C:\”开头
我还尝试将“file:///”添加到路径的开头,这不会改变 encode-for-uri 的行为。

有没有办法强制 encode-for-uri 将其输入视为绝对路径?

【问题讨论】:

  • href 属性应该是一个文件 URI,这意味着它应该有正斜杠 / 而不是反斜杠,所以试试 concat('file:///', encode-for-uri(replace(@filename, '\\', '/'))) 是否给你一个可靠的结果。
  • 是的,这行得通。所以我假设 \ 会阻止处理器识别 URI?
  • 不,encode-for-uri 函数将转义任何反斜杠。

标签: xml xslt xslt-2.0 saxon


【解决方案1】:

有两个问题,href 属性需要一个 URI,并且在 URI 中,分隔符是 / 而不是 Windows 文件路径中使用的 \。此外,encode-for-uri 的使用会转义任何反斜杠。

所以要解决这个问题,你应该用正斜杠替换任何反斜杠,然后你可以使用encode-for-uri 来转义百分号:

concat('file:///', encode-for-uri(replace(@filename, '\\', '/')))

【讨论】:

    【解决方案2】:

    关于具体问题:

    有没有办法强制 encode-for-uri 将其输入视为 绝对路径?

    规范中提到了 fn:encode-for-uri:

    对字符串中的保留字符进行编码,该字符串旨在用于 URI 的路径段。

    所以答案是否定的:这不是函数的用途。它不是为处理完整的 URI 或理解它们的语法而设计的;它旨在处理用于构造 URI 路径段的字符串。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-17
      • 2010-12-17
      • 2021-10-02
      • 2016-01-07
      • 2019-11-14
      • 2010-09-15
      • 2021-11-10
      • 1970-01-01
      相关资源
      最近更新 更多