Multiple file inputs?

后端 未结 6 914
天命终不由人
天命终不由人 2020-12-06 13:49

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

相关标签:
6条回答
  • 2020-12-06 13:51

    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.

    0 讨论(0)
  • 2020-12-06 13:51

    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

    0 讨论(0)
  • 2020-12-06 13:53

    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.

    0 讨论(0)
  • 2020-12-06 13:57

    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.

    0 讨论(0)
  • 2020-12-06 14:10

    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.

    0 讨论(0)
  • 2020-12-06 14:15

    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().

    0 讨论(0)
提交回复
热议问题