如何将带有 inline/attachment 的 url 转换为带有 xsl 文件扩展名的 url?

How to transform url with inline/attachment to url with file extension in xsl?

在 xsl 脚本的帮助下,我将 url 提取到 XML 的文件中。这个url的结尾是:api/v1/objects/uuid/b79de4e5-8d1f-4840-b85f-e052db92a52f/file/id/1001974122/file_version/name/small/disposition/inline

当我在网络浏览器中输入此 url 时,它将被转换为 URL,文件扩展名为 eas/partitions-inline/48/1001/1001974000/1001974122/9a4191c7ce7414650d36ac9bc1c2b012261013ad/image/png/8223@33a8cae1-a9fa-4655-8c3d-b71241bbc99b_1001974122_small.png

有没有办法在没有浏览器的情况下使用 xsl 进行这种转换?

我需要 url 在我的输出 xml 中带有文件扩展名,以便 运行 收割机对其进行处理。

关于 URL 转换(以及使用的 XML 工具)的问题非常非正式,但我们假设 3xx 对原始 URL 的响应以及输出结果的意图 URL。例如:

$ curl --silent --head http://whosebug.com | grep Location
Location: https://whosebug.com/

要在转换时做同样的事情 XML,XSLT 处理器需要有一个 HTTP 客户端。 EXPath 中有 HTTP Client module,XPath 扩展规范及其实现的集合。

要快速安装 EXPath,download page. It comes with Saxon XSLT processor. At the time of writing it refers to expath-repo-installer-0.13.1.jar 上提供了安装程序。 运行 喜欢:

java -jar expath-repo-installer-0.13.1.jar

安装后,下载 Saxon 的 HTTP 客户端模块,expath-http-client-saxon-0.12.0.zip 并从中提取 expath-http-client-saxon-0.12.0.xar。然后将其安装到 EXPath 存储库:

mkdir repo
bin/xrepo --repo repo install /path/to/expath-http-client-saxon-0.12.0.xar

那你可以用bin/saxon.

data.xml

<?xml version="1.0" encoding="utf-8"?>
<data>
  <datum><url>http://python.org</url></datum>
  <datum><url>http://whosebug.com</url></datum>
</data>

text.xslt

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet 
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:http="http://expath.org/ns/http-client"
  exclude-result-prefixes="#all"
  version="2.0">

  <xsl:import href="http://expath.org/ns/http-client.xsl"/>
  <xsl:output method="xml" encoding="utf-8" indent="yes"/>

  <xsl:template match="/">
    <result>
      <xsl:for-each select="data/datum">
        <!-- the request element -->
        <xsl:variable name="request" as="element(http:request)">
          <http:request method="head" follow-redirect="false">
            <xsl:attribute name="href">
              <xsl:value-of select="url"/>
            </xsl:attribute> 
          </http:request>
        </xsl:variable>
        <!-- sending the request -->
        <xsl:variable name="response" select="http:send-request($request)"/>
        <!-- output -->
        <url>
          <orig><xsl:value-of select="url"/></orig>
          <location>
            <xsl:value-of 
              select="$response[1]/header[@name='location']/@value"/>
          </location>
        </url>
      </xsl:for-each>  
    </result>
  </xsl:template>
</xsl:stylesheet>

有关如何控制 HTTP 客户端的更多详细信息,请参阅 the module's spec

然后 bin/saxon --repo repo data.xml test.xslt 产生:

<?xml version="1.0" encoding="utf-8"?>
<result>
   <url>
      <orig>http://python.org</orig>
      <location>https://python.org/</location>
   </url>
   <url>
      <orig>http://whosebug.com</orig>
      <location>https://whosebug.com/</location>
   </url>
</result>