-
Notifications
You must be signed in to change notification settings - Fork 21
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Is it possible to call xml-maven-plugin by passing only the initial template to be executed? #62
Comments
I couldn't really tell. My assumption is, that such a "named template" is basically just a lookup into a map, where the template name is being mapped to a URI. So, if you know the URI, you can use that, and everything should work just fine. Apart from that, I am at a loss. (We are managing the xml-maven-plugin, not Saxon.) If there should be some kind of standard mechanism, that I have missed in my 20 years of XML related work, I am ready to learn, and to extend the plugin, as far as the standard goes. I am drawing the line at Saxon specific stuff, though. |
Thank you for the quick reply. Admittedly I am a novice when it comes to using XSLT. I realise that the simple solution for my issue of not requiring an input file was to create an empty xml file as my source when calling my template. I do see that XSLT 3.0 in section 2.3.4 does indicate that "a stylesheet may be evaluated by selecting a named template to be evaluated". My current approach to calling the
Perhaps you could provide an example of how I could include the URI approach above to specify a named template when using the xml-maven-plugin? |
The XSL Initial Template is a mechanism new to XSL-T 3.0. It is not a proprietary Saxon feature. From the XSL spec:
This is how Saxon implements it:
The right way to use it would be: <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="3.0">
<xsl:template name="xsl:initial-template">
<xsl:apply-templates />
</xsl:template>
<xsl:template match="whatever">
<xsl:apply-templates />
<xsl:template>
and so on...
</xsl:stylesheet> The "initial template" is the main entry point to the stylesheet and gets executed first. As we can see, it does not require an input-document, thus, and only in this case, the stylesheet can be called without an input XML file. The output gets created programmatically from the contents of the stylesheet. |
I found this issue because I had a similar requirement of transforming XML files loaded from a directory using the I wanted to share my approach for this for anyone else who might stumble upon this issue. The basic idea is to declare an (optional) dependency on Saxon <dependencies>
<dependency>
<groupId>net.sf.saxon</groupId>
<artifactId>Saxon-HE</artifactId>
<version>12.3</version>
<!-- Avoid a transitive dependency on Saxon. -->
<optional>true</optional>
</dependency>
</dependencies> and to use the Exec Maven Plugin to invoke the Saxon CLI shipped with the library (which supports the XSL Initial Template mechanism): <plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>3.1.0</version>
<executions>
<execution>
<id>transform-xml</id>
<phase>generate-resources</phase>
<goals>
<goal>java</goal>
</goals>
<configuration>
<mainClass>net.sf.saxon.Transform</mainClass>
<arguments>
<argument>-it</argument>
<argument>-xsl:${project.basedir}/stylesheet.xslt</argument>
<argument>-o:${project.build.outputDirectory}/output.xml</argument>
</arguments>
</configuration>
</execution>
</executions>
</plugin> |
When using the Saxon-HE command line (currently version 10.5), one can make a call as follows:
saxon -it -xsl:my-stylesheet.xsl -o:output.xml
For the command above, by default the template with name
xsl:initial-template
in the specified stylesheet is executed.No file names are needed as input.
Questions:
xml-maven-plugin
can be invoked similarly (using Saxon-HE as a dependency) by passing this-it
parameter (or equivalent) and with nodir
element in the relatedtransformationSet
?outputDir
in the relatedtransformationSet
.The text was updated successfully, but these errors were encountered: