xsl-list
[Top] [All Lists]

Re: [xsl] Creating csv from multiple input files

2008-05-06 07:48:39
Related to your question...

Eventually you may find yourself needing to implement XMLCatalogs if
you're processing a number of XML files that have DTDs.  In which
case, you'll need to use the Apache Commons Resolver with saxon.

On windows, I use a batch file similar to the following for running
saxon.  It is pretty easy to pass parameters.
for example, if you call the batch file saxon.bat, use this command:
saxon -o:output.xml -s:input.xml -xsl:xml2csv.xslt param1="" param2=""

where param1 and param2 are the names of your xsl:param and the values
are in the "".
---------------------
@echo off
:: A wrapper for calling the saxon XSLT processor.
:: Uses Saxon 9.0j
:: Uses Xerces 2.9.0
:: Uses the Apache Commons Resolver v1.2 for the Catalog Manager
:: Uses %_Path2CatalogManager% which is the path to the folder where
the CatalogManager.properties file is stored.
:: (Not to the CatalogManager.properties file but its folder!)
IF NOT DEFINED JAVA_HOME (
        echo ERROR! Environment Variable JAVA_HOME is not set.
        echo Set JAVA_HOME variable appropriately.
        echo For example: set JAVA_HOME=c:\program files\java\jre1.6.0_01
        goto end
)
:: Next 2 lines remove potential double quotes from the JAVA_HOME
environment variable so that additional information can be added
:: JAVA_HOME for tests.
set JAVA_HOME=%JAVA_HOME:"=%
set JAVA_HOME=%JAVA_HOME:'=%
IF NOT EXIST "%JAVA_HOME%\bin\java.exe" (
    echo ERROR! Could not find "%JAVA_HOME%\bin\java.exe"
    goto end
  )

IF NOT EXIST "%_Path2CatalogManager%" (
    echo ERROR! Could not find CatalogManager.properties
    goto end
    )
:: Note: I set the saxon_prefix using multiple lines for readability
set saxon_prefix="%JAVA_HOME%\bin\java.exe"
:: The %path_to_saxon9% and similar variables are not intended to be
defined... but instead you should replace these with the actual paths.
set saxon_prefix=%saxon_prefix% -cp
%path_to_saxon9%\saxon9.jar;%path_to_xercesImpl%\xercesImpl.jar;%path_to_resolver%\resolver.jar;%_Path2CatalogManager%;
set saxon_prefix=%saxon_prefix%
-Djavax.xml.parsers.DocumentBuilderFactory=org.apache.xerces.jaxp.DocumentBuilderFactoryImpl
set saxon_prefix=%saxon_prefix%
-Djavax.xml.parsers.SAXParserFactory=org.apache.xerces.jaxp.SAXParserFactoryImpl
set saxon_prefix=%saxon_prefix%
-Djavax.xml.transform.TransformerFactory=com.icl.saxon.TransformerFactoryImpl
set saxon_prefix=%saxon_prefix% net.sf.saxon.Transform
set saxon_prefix=%saxon_prefix% -versionmsg:off
set saxon_prefix=%saxon_prefix%
-x:org.apache.xml.resolver.tools.ResolvingXMLReader
set saxon_prefix=%saxon_prefix%
-y:org.apache.xml.resolver.tools.ResolvingXMLReader
set saxon_prefix=%saxon_prefix% -r:org.apache.xml.resolver.tools.CatalogResolver
%saxon_prefix% %*
goto end

:end
------------------


On Tue, May 6, 2008 at 10:31 AM, Georg Hohmann 
<georg(_dot_)hohmann(_at_)gmail(_dot_)com> wrote:
 So you could use
 >   <xsl:apply-templates
 > select="collection('file:///C:/dir/subdir?select=*.xml')/*/*"/>
 >  to process all child elements of all root elements of those .xml files in
 > the directory C:\dir\subdir.

 Thanks for the answer.
 Following your advice i modified my stylesheet like this:


 <?xml version="1.0" encoding="UTF-8"?>
 <xsl:stylesheet version="2.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
  <xsl:output name="csv" method="text" encoding="UTF-8"/>
  <xsl:template match="/">
     <xsl:result-document format="csv" href="result.csv">
        <xsl:apply-templates
 select="collection('data/input?select=*.xml')/*/*"/>

        <xsl:text>&#x0d;&#x0a;</xsl:text>
     </xsl:result-document>
  </xsl:template>
 ...

 "data/input" is the path relative to my xslt where the input files
 resist. Now i wonder how i should run saxon9 from the command line.
 Saxon9 needs an input file, but now the input files are all defined
 inside the stylesheet. I was thinking of something like this:

 java -jar saxon9.jar xml2csv.xslt

 But this does not work due to the lack of an input file.
 Could you please help a bit further?

 Georg.

 P.S.: Meanwhile i will generate a simple shell script like Wendell suggested.
 That is the most obviously approach that i didn't thought of.
 Sometimes too much xslt makes me blind :-)



 --~------------------------------------------------------------------
 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
 To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
 or e-mail: 
<mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
 --~--



--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--