2

I want to use XSLT to read all the XML files within a subdirectory and output the results to a single XML file, but I'm not sure how to execute the transformation.

The syntax below works 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.

I don't think the spaces in the uri are an issue. The syntax below does work for me, provided that the uris are in quotation marks (and provided that the source and the output are both filenames or both directories).

I'm using the Saxon HE processor, but I am willing to use another tool if necessary.

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

1 Answer 1

2

Well the stylesheet needs to take the input into account, for instance by using the collection function http://www.saxonica.com/documentation/index.html#!sourcedocs/collections

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

and then you run Saxon with the -it:main command line option to start with the template main that pulls in all *.xml files in that directory.

You of course then need to make sure there are templates processing and transforming the nodes in the files as needed but as you have not provided any example of the XML structure there is not much we can suggest to help with that.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks, the XSLT is now executing. I'm now getting an error message that the requested template "main" does not exist, but I'll put that into a separate question.
Is there a way to pass the base dir to the saxon execution instead of having it hard coded in the template?
@dave, it is not a good idea to solve such new questions in a comment but you can of course declare a global parameter e.g. top level <xsl:param name="dir-uri" as="xs:string">file:///C:/Temp/dir</xsl:param> and then use e.g. collection($dir-uri || '?select=*.xml') (that is XSLT/XPath 3, if you still use XPath 2 use the concat function instead of the concat || operator) and then the Saxon command line, your IDE or your Java code allows you to set the parameter dir-uri on each run.
@MartinHonnen you're right, my bad. Thanks for your reply. In the end, the issue was that I put the param underneath the template, not at the stylesheet top level.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.