Read a remote zipped xml with just XSL

匿名 (未验证) 提交于 2019-12-03 03:04:01

问题:

I want to know if it's possible for an XSLT file to read data from an XML located within folders of a remote zip(from the server at work), without any external processors (saxon and so forth) and without downloading it.

Failing that, I'll resort to just reading the information from the zip... which brings me to my other (newb)issue.

I currently have an XSLT that accesses and gets the data from the downloaded and extracted XML file, but I can't do this without extracting it. I've read that with Altova and xslt 2.0 it is possible to read from within a zip file using the document() function, though, as of yet I have not been able achieve this.

this is how I'm trying to do it: document('name.zip|zip/folder/folder2/iwantthis.xml')

It just doesn't seem to find the file. I'd be almost eternally grateful if you show me the error of my ways and guide me into XSLThood.

Thank you kindly

回答1:

I don't think there is a general way of doing that.

However, in many Java-based XSLT processors (at least Saxon and Xalan), you can use 'jar:' URLs to refer to resources inside zip files. Prefix the URL pointing to the zip file with jar: and suffix it with !/ and the path to the file inside it. Like this: jar:file:///the/path/to/foo.zip!/foo.xml.

To achieve the same in other processors, you would probably need figure out if they allow registering a custom "URI handler" or an equivalent to deal with the special URIs you want to support.



回答2:

Unless your file system provides a native, transparent way to access a zip file as if it were a folder, this won't be possible.

In other words: You must be able to open the path you tried to feed to document() in any other program on your system. If that does not work anywhere else, what would make you assume it would work in XSLT?



回答3:

If you are using Saxon, then you could use the EXPath Zip Facility.

Here is an example:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"                  xmlns:xs="http://www.w3.org/2001/XMLSchema"                  xmlns:zip="http://www.expath.org/mod/zip"                 version="2.0">     <xsl:import href="http://www.expath.org/mod/zip.xsl"/>     <xsl:output indent="yes"/>     <!--        $file is the ZIP file to use.  If $entry is set, extract that        entry from $file, as an XML document.  If not, list the content        of $file.    -->    <xsl:param name="file"  as="xs:string"/>    <xsl:param name="entry" as="xs:string?"/>     <xsl:template name="main" match="/">       <xsl:choose>          <xsl:when test="$entry">             <!-- an XML entry in the ZIP file -->             <xsl:sequence select="zip:xml-entry($file, $entry)"/>          </xsl:when>          <xsl:otherwise>             <!-- the structure of the ZIP file -->             <xsl:sequence select="zip:entries($file)"/>          </xsl:otherwise>       </xsl:choose>    </xsl:template>  </xsl:stylesheet> 


回答4:

I just tried the AltovaXML tools command line XSLT 2.0 processor with the following XSLT 2.0 stylesheet:

<xsl:stylesheet   xmlns:xsl="http://www.w3.org/1999/XSL/Transform"   version="2.0">    <xsl:template name="main">     <xsl:copy-of select="document('ziptest1.zip|zip/ziptest1/file.xml')"/>   </xsl:template>  </xsl:stylesheet> 

where "ziptest1.zip" is a .zip file with a folder named "ziptest1" containing a file named "file.xml" and the output I get is the contents of that file. If the path can't be resolved then I get an error saying "Error retrieving resource".

I tested that with "AltovaXML Version 2010 rel. 3" which I think is the latest version.



回答5:

From what I got by reading all of your (very quick) responses and since using XSLT alone elicited "probably-not-possible" answers and Saxon-talk, I guess Saxon's the way to go. I'll try it first at home, to see how much of a hassle it is to install/use given that usually whenever I mention installing something at work, they turn me down (I'm an intern). I'll just wait till Martin Honnen answers my comment, though.

Thank you very much for your time and also for answering so quicky.

Edit: Thanks Martin, will check the documentation for that feature.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!