...
Div | ||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| ||||||||||||||||||||||||||||||||||||||||||||||
|
Using XSLT endpoints
For example you could use something like
Code Block |
---|
from("activemq:My.Queue").
to("xslt:com/acme/mytransform.xsl");
|
To use an XSLT template to formulate a response for a message for InOut message exchanges (where there is a JMSReplyTo
header).
If you want to use InOnly and consume the message and send it to another destination you could use the following route:
Code Block |
---|
from("activemq:My.Queue").
to("xslt:com/acme/mytransform.xsl").
to("activemq:Another.Queue");
|
Getting Parameters into the XSLT to work with
By default, all headers are added as parameters which are available in the XSLT.
To do this you will need to declare the parameter so it is then useable.
...
<setHeader headerName="myParam"><constant>42</constant></setHeader>
<to uri="xslt:MyTransform.xsl"/>
And the XSLT just needs to declare it at the top level for it to be available:
...
<xsl: ...... >
<xsl:param name="myParam"/>
<xsl:template ...>
Spring XML versions
To use the above examples in Spring XML you would use something like
...
<camelContext xmlns="http://activemq.apache.org/camel/schema/spring">
<route>
<from uri="activemq:My.Queue"/>
<to uri="xslt:org/apache/camel/spring/processor/example.xsl"/>
<to uri="activemq:Another.Queue"/>
</route>
</camelContext>
|
Using XSLT endpoints
For example you could use something like
Code Block |
---|
from("activemq:My.Queue").
to("xslt:com/acme/mytransform.xsl");
|
To use an XSLT template to formulate a response for a message for InOut message exchanges (where there is a JMSReplyTo
header).
If you want to use InOnly and consume the message and send it to another destination you could use the following route:
Code Block |
---|
from("activemq:My.Queue").
to("xslt:com/acme/mytransform.xsl").
to("activemq:Another.Queue");
|
Getting Parameters into the XSLT to work with
By default, all headers are added as parameters which are available in the XSLT.
To do this you will need to declare the parameter so it is then useable.
Code Block | ||||
---|---|---|---|---|
| ||||
<setHeader headerName="myParam"><constant>42</constant></setHeader>
<to uri="xslt:MyTransform.xsl"/>
|
And the XSLT just needs to declare it at the top level for it to be available:
Code Block | ||||
---|---|---|---|---|
| ||||
<xsl: ...... >
<xsl:param name="myParam"/>
<xsl:template ...>
|
Spring XML versions
To use the above examples in Spring XML you would use something like
Code Block | ||||
---|---|---|---|---|
| ||||
<camelContext xmlns="http://activemq.apache.org/camel/schema/spring">
<route>
<from uri="activemq:My.Queue"/>
<to uri="xslt:org/apache/camel/spring/processor/example.xsl"/>
<to uri="activemq:Another.Queue"/>
</route>
</camelContext>
|
There is a test case along with its Spring XML if you want a concrete example.
Using xsl:include
Camel 2.2 or older
If you use xsl:include in your XSL files then in Camel 2.2 or older it uses the default javax.xml.transform.URIResolver
which means it can only lookup files from file system, and its does that relative from the JVM starting folder.
For example this include:
Code Block | ||||
---|---|---|---|---|
| ||||
<xsl:include href="staff_template.xsl"/>
|
Will lookup the staff_tempkalte.xsl
file from the starting folder where the application was started.
Camel 2.3 or newer
Now Camel provides its own implementation of URIResolver
which allows Camel to load included files from the classpath and more intelligent than before.
For example this include:
Code Block | ||||
---|---|---|---|---|
| ||||
<xsl:include href="staff_template.xsl"/>
|
Will now be located relative from the starting endpoint, which for example could be:
Code Block |
---|
.to("xslt:org/apache/camel/component/xslt/staff_include_relative.xsl")
|
Which means Camel will locate the file in the classpath as org/apache/camel/component/xslt/staff_template.xsl
.
This allows you to use xsl include and have xsl files located in the same folder such as we do in the example org/apache/camel/component/xslt
.
You can use the following two prefixes classpath:
or file:
to instruct Camel to look either in classpath or file system. If you omit the prefix then Camel uses the prefix from the endpoint configuration. If that neither has one, then classpath is assumed.
You can also refer back in the paths such as
Code Block |
---|
<xsl:include href="../staff_other_template.xsl"/>
|
Which then will resolve the xsl file under org/apache/camel/component
.
Using xsl:include and default prefix
When using xsl:include such as:
Code Block | ||||
---|---|---|---|---|
| ||||
<xsl:include href="staff_template.xsl"/>
|
Then in Camel 2.10.3 and older, then Camel will use "classpath:" as the default prefix, and load the resource from the classpath. This works for most cases, but if you configure the starting resource to load from file,
Code Block |
---|
.to("xslt:file:etc/xslt/staff_include_relative.xsl")
|
.. then you would have to prefix all your includes with "file:" as well.
There is a test case along with its Spring XML if you want a concrete example.
Using xsl:include
Camel 2.2 or older
If you use xsl:include in your XSL files then in Camel 2.2 or older it uses the default javax.xml.transform.URIResolver
which means it can only lookup files from file system, and its does that relative from the JVM starting folder.
For example this include:
Code Block | ||||
---|---|---|---|---|
| ||||
<xsl:include href="staff_template.xsl"/>
|
...
file:staff_ |
...
template.xsl |
...
"/>
|
From Camel 2.10.4 onwards we have made this easier as Camel will use the prefix from the endpoint configuration as the default prefix. So from Camel 2.10.4 onwards you can do
Camel 2.3 or newer
Now Camel provides its own implementation of URIResolver
which allows Camel to load included files from the classpath and more intelligent than before.
For example this include:
Code Block | ||||
---|---|---|---|---|
| ||||
<xsl:include href="staff_template.xsl"/>
|
Will now be located relative from the starting endpoint, which for example could be:
Code Block |
---|
.to("xslt:org/apache/camel/component/xslt/staff_include_relative.xsl")
|
Which means Camel will locate the file in the classpath as org/apache/camel/component/xslt/staff_template.xsl
.
This allows you to use xsl include and have xsl files located in the same folder such as we do in the example org/apache/camel/component/xslt
.
You can use the following two prefixes classpath:
or file:
to instruct Camel to look either in classpath or file system. If you omit the prefix then Camel uses the prefix from the endpoint configuration. If that neither has one, then classpath is assumed.
You can also refer back in the paths such as
Code Block |
---|
<xsl:include href="../staff_other_template.xsl"/>
|
Which then will resolve the xsl file under org/apache/camel/component
.
Using xsl:include and default prefix
When using xsl:include such as:
...
<xsl:include href="staff_template.xsl"/>
Then in Camel 2.10.3 and older, then Camel will use "classpath:" as the default prefix, and load the resource from the classpath. This works for most cases, but if you configure the starting resource to load from file,
Code Block |
---|
.to("xslt:file:etc/xslt/staff_include_relative.xsl")
|
.. then you would have to prefix all your includes with "file:" as well.
...
<xsl:include href="file:staff_template.xsl"/>
="staff_template.xsl"/>
|
Which will load the staff_template.xsl resource from the file system, as the endpoint was configured with "file:" as prefix.
You can still though explicit configure a prefix, and then mix and match. And have both file and classpath loading. But that would be unusual, as most people either use file or classpath based resources.
Using Saxon extension functions
Since Saxon 9.2, writing extension functions has been supplemented by a new mechanism, referred to as integrated extension functions you can now easily use camel:
- Java example:
Code Block | ||
---|---|---|
| ||
SimpleRegistry registry = new SimpleRegistry();
registry.put("function1", new MyExtensionFunction1());
registry.put("function2", new MyExtensionFunction2());
CamelContext context = new DefaultCamelContext(registry);
context.addRoutes(new RouteBuilder() {
@Override
public void configure() throws Exception {
from("direct:start")
.to("xslt:org/apache/camel/component/xslt/extensions/extensions.xslt?saxonExtensionFunctions=#function1,#function2");
}
}); |
Spring example:
Code Block | ||
---|---|---|
| ||
<camelContext xmlns="http://camel.apache.org/schema/spring">
<route>
<from uri="direct:extensions"/>
<to uri="xslt:org/apache/camel/component/xslt/extensions/extensions.xslt?saxonExtensionFunctions=#function1,#function2"/>
</route>
</camelContext>
<bean id="function1" class="org.apache.camel.component.xslt.extensions.MyExtensionFunction1"/>
<bean id="function2" class="org.apache.camel.component.xslt.extensions.MyExtensionFunction2"/> |
From Camel 2.10.4 onwards we have made this easier as Camel will use the prefix from the endpoint configuration as the default prefix. So from Camel 2.10.4 onwards you can do:
...
<xsl:include href="staff_template.xsl"/>
Which will load the staff_template.xsl resource from the file system, as the endpoint was configured with "file:" as prefix.
You can still though explicit configure a prefix, and then mix and match. And have both file and classpath loading. But that would be unusual, as most people either use file or classpath based resources.
Dynamic stylesheets
To provide a dynamic stylesheet at runtime you can define a dynamic URI. See How to use a dynamic URI in to() for more information.
...