Solved

How to execute transformation of multiple files within a subdirectory

Posted on 2013-05-25
5
836 Views
Last Modified: 2013-05-31
I want to transform all the XML files within a subdirectory and output the results to a single XML file. I'm not sure if the XSL below works because I haven't figured out how to execute it.

I tried executing the code below from the command line but it didn't work:

java -jar c:\saxon\saxon9he.jar -s:"C:\Temp\2011 Valid XML" -o:"C:\Temp\LookupTables.xml" -xsl:"C:\Temp\LookupTables.xsl"


Here is the XSL:
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    
    <xsl:template match="node()|@*" mode="inFile">
                 <xsl:copy>
                      <xsl:apply-templates mode="inFile" select="node()|@*"/>
                 </xsl:copy>
            </xsl:template>
    
    <xsl:template match="/">
        <xsl:apply-templates mode="inFile" select="collection('file:///C:/Temp/2011 Valid XML?select=*.xml;recurse=yes')"/>
        <LookupTables>
            <Facility_Identifiers>
                <xsl:for-each select="/Report/RegistrationData/Facility/Identifiers/IdentifierList">
                    <Facility_Identifier Org="{/Report/RegistrationData/Organisation/Details/BusinessLegalName}" Facility="{/Report/RegistrationData/Facility/Details/FacilityName}" NPRI_ID="{Identifier[IdentifierType = 'NationalPollutantReleaseInventoryId']/IdentifierValue}" GHGIS_ID="{Identifier[IdentifierType = 'GHGRPId']/IdentifierValue}"/>
                </xsl:for-each>
            </Facility_Identifiers>
            <NAICS_Details>
                <xsl:for-each select="/Report/RegistrationData/Facility/Identifiers/NAICSCodeList/NAICSCode">
                    <NAICS_Detail Org="{/Report/RegistrationData/Organisation/Details/BusinessLegalName}" Facility="{/Report/RegistrationData/Facility/Details/FacilityName}" NAICS="{Code}" Classification="{NAICSClassification}" Percentage="{ActivityPercentage}"/>
                </xsl:for-each>
            </NAICS_Details>
        </LookupTables>
    </xsl:template>
    
</xsl:stylesheet>

Open in new window

0
Comment
Question by:mariita
  • 3
  • 2
5 Comments
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 39197065
this is not a valid file uri

"file:///C:/Temp/2011 Valid XML"

I don't think the spaces wil work

try


"file:///C:/Temp/2011%20Valid%20XML"
0
 

Author Comment

by:mariita
ID: 39197390
I'm still getting a source file does not exist error.

The above syntax actually works (including spaces in the uri) if both the source uri and the output uri are directories, but it doesn't work if the source uri is a directory while the output uri is a filename. If both the source uri and the output uri are directories, then the XSL is applied to each file in the the source directory, and the output will be a different file, which is written to each uri in the target directory.
0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 39198545
strange, the source should be a directory, the output a single file on the command line

You can't write to multiple files unless you have an xsl:result-document in there

Can you say what you are doing exactly?
0
 

Accepted Solution

by:
mariita earned 0 total points
ID: 39198665
I got the answer to this in another forum.

Saxon 9 does allow writing to multiple files using the syntax:

java -jar SomePath/saxon9he.jar -s:inputDirectory -o:outputDirectory -xsl:sheet.xsl

If the input is a directory and the output is a single file, then the -s: parameter is omitted and replaced with the  -it:mainTemplate command line option, where mainTemplate is the main template in the XSL. Then the mainTemplate pulls in the XML files using the collection function.

java -jar SomePath/saxon9he.jar -it:mainTemplate -o:outputFile -xsl:sheet.xsl

<xsl:template name="mainTemplate">
  <root>
    <xsl:apply-templates select="collection('file:///C:Temp/dir?select=*.xml')/*"/>
  </root>
</xsl:template>

See:
http://www.oxygenxml.com/archives/xsl-list/200911/msg00044.html
and
http://stackoverflow.com/questions/16757759/how-to-execute-xslt-with-multiple-input-files-and-single-output-file
0
 

Author Closing Comment

by:mariita
ID: 39210169
I got the answer to this in another forum.
0

Featured Post

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Create XML 5 52
XML & .net 5 49
Wordpress Cron - Send XML Data to external server FTP 3 23
Append data to an XML file 3 16
The Problem How to write an Xquery that works like a SQL outer join, providing placeholders for absent data on the outer side?  I give a bit more background at the end. The situation expressed as relational data Let’s work through this.  I’ve …
Browsing the questions asked to the Experts of this forum, you will be amazed to see how many times people are headaching about monster regular expressions (regex) to select that specific part of some HTML or XML file they want to extract. The examp…

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question