Question

I discovered you can get text files through HTTP using the unparsed-text function in XSLT 2.0. Works great, but a file I need to get requires basic HTTP authentication. Tried the following, but that didn't seem to make any difference.

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

Is there a way I can specify credentials to use when using this unparsed-text function?

Était-ce utile?

La solution

After a bit of research, I am quite confident it can only be done with an extension function to handle HTTP requests appropriately. In other words, using unparsed-text() alone will not get you anywhere, as you have noticed.

It seems to me that the extension functions written by Florent Georges would meet your needs. The code below is not my own, I have adapted it from an example on the website.

It is an example for a delete request, with basic authentication in place. This works both for document() and 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>

I am sure whether those extensions can still be used with recent versions of Saxon. Perhaps you should use the EXPath HTTP Client instead - which is very similar to the extension described above.

Autres conseils

As an alternative to using extension functions, with Saxon you can register an UnparsedTextURIResolver to look after the URI dereferencing.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top