tags:

views:

1321

answers:

7

Within an XSLT document, is it possible to loop over a set of files in the current directory?

I have a situation where I have a directory full of xml files that need some analysis done to generate a report. I have my stylesheet operating on a single document fine, but I'd like to extend that without going to another tool to merge the xml documents.

I was thinking along these lines:

<xsl:for-each select="{IO Selector Here}">
    <xsl:variable select="document(@url)" name="contents" />
    <!--More stuff here-->
</xsl:for-each>
A: 

I don't think XSL is set up to work that way: it's designed to be used by something else on one or more documents, and the something else would be responsible for finding files to which the XSLT should be applied.

If you had one main document and a fixed set of supporting documents, you could possibly use the document() function to return specific nodes and/or values, but I suspect your case is different.

Dave DuPlantis
A: 

I found this website that gives a solution to this problem, but it is using an external tool which I realize you don't want.

http://www.ibm.com/developerworks/xml/library/x-tipbatc.html

Aaron Smith
A: 

From within XSLT I think this will not be possible.

You could pass in all the XML file names to an <xsl:param name="files" /> as a comma separated list and loop over it using recursion and substring-before() and substring-after().

Tomalak
A: 

I have a command-line tool that could be used for this - it uses the XSLT processor built into Ant (the java build tool) to process input + transform into output. Would be easy to wrap with a batch file for loop.

svn://donie.homeip.net/public/tools

SteveDonie
+3  A: 

In XSLT 2.0, and with Saxon, you can do this with the collection() function:

<xsl:for-each select="file:///path/to/directory">
  <!-- process the documents -->
</xsl:for-each>

See http://www.saxonica.com/documentation/sourcedocs/collections.html for more details.

In XSLT 1.0, you have to create an index that lists the documents you want to process with a separate tool. Your environment may provide such a tool; for example, Cocoon has a Directory Generator that creates such an index. But without knowing what your environment is, it's hard to know what to recommend.

JeniT
+1  A: 

As others said, you cannot do it in a platform-independent way. In .NET world, you could create a custom XmlResolver so that document('dir://c:/foo/') would return the list of files in the 'c:\foo' directory in an arbitrary format you wish. See the following links for more information on custom XmlResolver's:

Customizing the XmlUrlResolver Class
The power of XmlResolver

Also you may resort to using scripts (like the msxsl:script element) or extensions in your XSLT stylesheet.

All these approaches will make your XSLT code unportable to other platforms.

Antosha
A: 

If you are using .Net you can use XsltExtension to make calls from your XSLT document to methods in your .net class. The method could then return nodesets back to your XSLT. So your method could handle the file IO part.

Si Keep