在XSLT 2.0中使用未解析的文本时,指定HTTP身份验证的凭据

时间:2014-03-11 15:18:51

标签: xslt xslt-2.0 saxon

我发现您可以使用XSLT 2.0中的unparsed-text函数通过HTTP获取文本文件。效果很好,但我需要获取的文件需要基本的HTTP身份验证。试过以下,但这似乎没有任何区别。

<variable name="text" select="unparsed-text('http://test:1234@localhost/file.txt')" />

有没有办法可以指定使用此unparsed-text函数时要使用的凭据?

2 个答案:

答案 0 :(得分:1)

经过一番研究后,我非常有信心只能使用扩展功能来适当地处理HTTP请求。换句话说,单独使用unparsed-text()将无法让你到达任何地方,正如你所注意到的那样。

在我看来,extension functions written by Florent Georges符合您的需求。下面的代码不是我自己的,我已经从网站上的一个例子中进行了调整。

这是删除请求的示例,其中包含基本身份验证。这适用于document()unparsed-text()

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:ex="java:org.fgeorges.xslt.Exslt2"
                exclude-result-prefixes="ex"
                version="2.0">

   <!-- Parameters: the user (default to admin), the password
        (required) and the document to delete. -->
   <xsl:param name="user" select="'test'"/>
   <xsl:param name="pwd" required="yes"/>
   <xsl:param name="doc" select="'/file.txt'"/>

   <!-- The URI to be resolved -->
   <xsl:variable name="uri" select="
       'http://localhost'"/>

   <!-- The HTTP request -->
   <xsl:variable name="request">
      <http-request
          method="delete"
          user="{ $user }"
          password="{ $pwd }"/>
   </xsl:variable>

   <!-- The main template -->
   <xsl:template match="/" name="initial">
      <xsl:variable name="res" select="
          ex:http-send($request, concat($uri, $doc))"/>
     <!--Further processing-->
   </xsl:template>

</xsl:stylesheet>

我确定这些扩展是否仍可用于最新版本的Saxon。也许您应该使用EXPath HTTP Client代替 - 这与上述扩展非常相似。

答案 1 :(得分:1)

作为使用扩展函数的替代方法,使用Saxon,您可以注册UnparsedTextURIResolver来管理URI解除引用。

相关问题