views:

108

answers:

5

Hello there

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  A: 

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?

Tomalak
+1  A: 

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.

Jukka Matilainen
A: 

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"/&gt;

   <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>
Dimitre Novatchev
A: 

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.

Martin Honnen
I'm a bit confused with your answer. What does "the output I get is the contents of that file" mean? did it work, as in, you could look up whatever one needs from the xml? I ask because right after that you said when the path couldnt be resolved you got an error. Not sure if you succeeded or not. The error I get(using roughly the same method: "foreach" instead of "copyof") is something along the lines of "The stated resource could not be found" (translated). I think that's the same one you got ('em silly germans with their error descriptions). Also, I'm using Altova XMLspy 2007. Thanks
Emaguel
With the stated version of AltovaXML tools I am able to access an XML doument in a zip document the way I showed.Whether Altova XML Spy 2007 supports that I don't know, check the documentation or ask in an Altova support forum.
Martin Honnen
A: 

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.

Emaguel
I just realized, it seems as if this is the only place I looked. I did surf the interwebs a lot before actually posting here. OK just wanted to clear that up.
Emaguel