camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From conflue...@apache.org
Subject [CONF] Apache Camel > XSLT
Date Thu, 10 Nov 2011 17:12:00 GMT
<html>
<head>
    <base href="https://cwiki.apache.org/confluence">
            <link rel="stylesheet" href="/confluence/s/2042/9/1/_/styles/combined.css?spaceKey=CAMEL&amp;forWysiwyg=true"
type="text/css">
    </head>
<body style="background: white;" bgcolor="white" class="email-body">
<div id="pageContent">
<div id="notificationFormat">
<div class="wiki-content">
<div class="email">
    <h2><a href="https://cwiki.apache.org/confluence/display/CAMEL/XSLT">XSLT</a></h2>
    <h4>Page <b>edited</b> by             <a href="https://cwiki.apache.org/confluence/display/~rnewcomb">Rich
Newcomb</a>
    </h4>
        <br/>
                         <h4>Changes (1)</h4>
                                 
    
<div id="page-diffs">
                    <table class="diff" cellpadding="0" cellspacing="0">
    
            <tr><td class="diff-snipped" >...<br></td></tr>
            <tr><td class="diff-unchanged" >| {{deleteOutputFile}} | {{false}}
| *Camel 2.6:* If you have {{output=file}} then this option dictates whether or not the output
file should be deleted when the [Exchange] is done processing. For example suppose the output
file is a temporary file, then it can be a good idea to delete it after use. | <br>|
{{output}} | {{string}} | *Camel 2.3:* Option to specify which output type to use. Possible
values are: {{string, bytes, DOM, file}}. The first three options are all in memory based,
where as {{file}} is streamed directly to a {{java.io.File}}. For {{file}} you *must* specify
the filename in the IN header with the key {{Exchange.XSLT_FILE_NAME}} which is also {{CamelXsltFileName}}.
Also any paths leading to the filename must be created beforehand, otherwise an exception
is thrown at runtime. | <br></td></tr>
            <tr><td class="diff-unchanged" >| {{contentCache}} | {{true}} | *Camel
2.6:* Cache for the resource content (the stylesheet file) when it is loaded. If set to {{false}}
Camel will reload the stylesheet file on each message processing. This is good for development.
 <br></td></tr>
            <tr><td class="diff-added-lines" style="background-color: #dfd;">Note:
from *Camel 2.9* a cached stylesheet can be forced to reload at runtime via JMX using the
{{clearCachedStylesheet}} operation. | <br></td></tr>
            <tr><td class="diff-unchanged" >| {{allowStAX}} | {{false}} | *Camel
2.8.3/2.9:* Whether to allow using StAX as the {{javax.xml.transform.Source}}. | <br>{div}
<br></td></tr>
            <tr><td class="diff-snipped" >...<br></td></tr>
    
            </table>
    </div>                            <h4>Full Content</h4>
                    <div class="notificationGreySide">
        <h2><a name="XSLT-XSLT"></a>XSLT</h2>

<p>The <b>xslt:</b> component allows you to process a message using an <a
href="http://www.w3.org/TR/xslt" class="external-link" rel="nofollow">XSLT</a> template.
This can be ideal when using <a href="/confluence/display/CAMEL/Templating" title="Templating">Templating</a>
to generate respopnses for requests.</p>

<h3><a name="XSLT-URIformat"></a>URI format</h3>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
xslt:templateName[?options]
</pre>
</div></div>

<p>Where <b>templateName</b> is the classpath-local URI of the template
to invoke; or the complete URL of the remote template. Refer to the <a href="http://static.springframework.org/spring/docs/2.5.x/api/org/springframework/core/io/DefaultResourceLoader.html"
class="external-link" rel="nofollow">Spring Documentation for more detail of the URI syntax</a></p>

<p>You can append query options to the URI in the following format, <tt>?option=value&amp;option=value&amp;...</tt></p>

<p>Here are some example URIs</p>
<div class="confluenceTableSmall"><div class='table-wrap'>
<table class='confluenceTable'><tbody>
<tr>
<th class='confluenceTh'> URI </th>
<th class='confluenceTh'> Description </th>
</tr>
<tr>
<td class='confluenceTd'> <div class="code panel" style="border-width: 1px;"><div
class="codeContent panelContent">
<pre class="code-java">xslt:com/acme/mytransform.xsl</pre>
</div></div> </td>
<td class='confluenceTd'> refers to the file com/acme/mytransform.xsl on the classpath
</td>
</tr>
<tr>
<td class='confluenceTd'> <div class="code panel" style="border-width: 1px;"><div
class="codeContent panelContent">
<pre class="code-java">xslt:file:<span class="code-comment">///foo/bar.xsl</span></pre>
</div></div> </td>
<td class='confluenceTd'> refers to the file /foo/bar.xsl </td>
</tr>
<tr>
<td class='confluenceTd'> <div class="code panel" style="border-width: 1px;"><div
class="codeContent panelContent">
<pre class="code-java">xslt:http:<span class="code-comment">//acme.com/cheese/foo.xsl</span></pre>
</div></div> </td>
<td class='confluenceTd'> refers to the remote http resource </td>
</tr>
</tbody></table>
</div>
</div>

<p>Maven users will need to add the following dependency to their <tt>pom.xml</tt>
for this component when using <b>Camel 2.8</b> or older:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml">
<span class="code-tag">&lt;dependency&gt;</span>
    <span class="code-tag">&lt;groupId&gt;</span>org.apache.camel<span
class="code-tag">&lt;/groupId&gt;</span>
    <span class="code-tag">&lt;artifactId&gt;</span>camel-spring<span
class="code-tag">&lt;/artifactId&gt;</span>
    <span class="code-tag">&lt;version&gt;</span>x.x.x<span class="code-tag">&lt;/version&gt;</span>
    <span class="code-tag"><span class="code-comment">&lt;!-- use the same
version as your Camel core version --&gt;</span></span>
<span class="code-tag">&lt;/dependency&gt;</span>
</pre>
</div></div>
<p>From Camel 2.9 onwards the <a href="/confluence/display/CAMEL/XSLT" title="XSLT">XSLT</a>
component is provided directly in the camel-core.</p>

<h3><a name="XSLT-Options"></a>Options</h3>
<div class="confluenceTableSmall"><div class='table-wrap'>
<table class='confluenceTable'><tbody>
<tr>
<th class='confluenceTh'> Name </th>
<th class='confluenceTh'> Default Value </th>
<th class='confluenceTh'> Description </th>
</tr>
<tr>
<td class='confluenceTd'> <tt>converter</tt> </td>
<td class='confluenceTd'> <tt>null</tt> </td>
<td class='confluenceTd'> Option to override default <a href="http://camel.apache.org/maven/current/camel-core/apidocs/org/apache/camel/converter/jaxp/XmlConverter.html"
class="external-link" rel="nofollow">XmlConverter</a>. Will lookup for the converter
in the <a href="/confluence/display/CAMEL/Registry" title="Registry">Registry</a>.
The provided converted must be of type org.apache.camel.converter.jaxp.XmlConverter. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>transformerFactory</tt> </td>
<td class='confluenceTd'> <tt>null</tt> </td>
<td class='confluenceTd'> <b>Camel 1.6</b> Option to override default <a
href="http://java.sun.com/j2se/1.5.0/docs/api/javax/xml/transform/TransformerFactory.html"
class="external-link" rel="nofollow">TransformerFactory</a>. Will lookup for the
transformerFactory in the <a href="/confluence/display/CAMEL/Registry" title="Registry">Registry</a>.
The provided transformer factory must be of type javax.xml.transform.TransformerFactory. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>transformerFactoryClass</tt> </td>
<td class='confluenceTd'> <tt>null</tt> </td>
<td class='confluenceTd'> <b>Camel 1.6</b> Option to override default <a
href="http://java.sun.com/j2se/1.5.0/docs/api/javax/xml/transform/TransformerFactory.html"
class="external-link" rel="nofollow">TransformerFactory</a>. Will create a TransformerFactoryClass
instance and set it to the converter. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>uriResolver</tt> </td>
<td class='confluenceTd'> <tt>null</tt> </td>
<td class='confluenceTd'> <b>Camel 2.3</b>: Allows you to use a custom <tt>javax.xml.transformation.URIResolver</tt>.
Camel will by default use its own implementation <tt>org.apache.camel.builder.xml.XsltUriResolver</tt>
which is capable of loading from classpath. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>resultHandlerFactory</tt> </td>
<td class='confluenceTd'> <tt>null</tt> </td>
<td class='confluenceTd'> <b>Camel 2.3:</b> Allows you to use a custom <tt>org.apache.camel.builder.xml.ResultHandlerFactory</tt>
which is capable of using custom <tt>org.apache.camel.builder.xml.ResultHandler</tt>
types. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>failOnNullBody</tt> </td>
<td class='confluenceTd'> <tt>true</tt> </td>
<td class='confluenceTd'> <b>Camel 2.3:</b> Whether or not to throw an exception
if the input body is null. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>deleteOutputFile</tt> </td>
<td class='confluenceTd'> <tt>false</tt> </td>
<td class='confluenceTd'> <b>Camel 2.6:</b> If you have <tt>output=file</tt>
then this option dictates whether or not the output file should be deleted when the <a
href="/confluence/display/CAMEL/Exchange" title="Exchange">Exchange</a> is done processing.
For example suppose the output file is a temporary file, then it can be a good idea to delete
it after use. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>output</tt> </td>
<td class='confluenceTd'> <tt>string</tt> </td>
<td class='confluenceTd'> <b>Camel 2.3:</b> Option to specify which output
type to use. Possible values are: <tt>string, bytes, DOM, file</tt>. The first
three options are all in memory based, where as <tt>file</tt> is streamed directly
to a <tt>java.io.File</tt>. For <tt>file</tt> you <b>must</b>
specify the filename in the IN header with the key <tt>Exchange.XSLT_FILE_NAME</tt>
which is also <tt>CamelXsltFileName</tt>. Also any paths leading to the filename
must be created beforehand, otherwise an exception is thrown at runtime. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>contentCache</tt> </td>
<td class='confluenceTd'> <tt>true</tt> </td>
<td class='confluenceTd'> <b>Camel 2.6:</b> Cache for the resource content
(the stylesheet file) when it is loaded. If set to <tt>false</tt> Camel will reload
the stylesheet file on each message processing. This is good for development. <br/>
Note: from <b>Camel 2.9</b> a cached stylesheet can be forced to reload at runtime
via JMX using the <tt>clearCachedStylesheet</tt> operation. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>allowStAX</tt> </td>
<td class='confluenceTd'> <tt>false</tt> </td>
<td class='confluenceTd'> <b>Camel 2.8.3/2.9:</b> Whether to allow using
StAX as the <tt>javax.xml.transform.Source</tt>. </td>
</tr>
</tbody></table>
</div>
</div>

<h3><a name="XSLT-UsingXSLTendpoints"></a>Using XSLT endpoints</h3>

<p>For example you could use something like</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
from(<span class="code-quote">"activemq:My.Queue"</span>).
  to(<span class="code-quote">"xslt:com/acme/mytransform.xsl"</span>);
</pre>
</div></div>

<p>To use an XSLT template to formulate a response for a message for InOut message exchanges
(where there is a <tt>JMSReplyTo</tt> header).</p>

<p>If you want to use InOnly and consume the message and send it to another destination
you could use the following route:</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
from(<span class="code-quote">"activemq:My.Queue"</span>).
  to(<span class="code-quote">"xslt:com/acme/mytransform.xsl"</span>).
  to(<span class="code-quote">"activemq:Another.Queue"</span>);
</pre>
</div></div>

<h3><a name="XSLT-GettingParametersintotheXSLTtoworkwith"></a>Getting Parameters
into the XSLT to work with</h3>

<p>By default, all headers are added as parameters which are available in the XSLT.<br/>
To do this you will need to declare the parameter so it is then <em>useable</em>.</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml">
<span class="code-tag">&lt;setHeader headerName=<span class="code-quote">"myParam"</span>&gt;</span><span
class="code-tag">&lt;constant&gt;</span>42<span class="code-tag">&lt;/constant&gt;</span><span
class="code-tag">&lt;/setHeader&gt;</span>
<span class="code-tag">&lt;to uri=<span class="code-quote">"xslt:MyTransform.xsl"</span>/&gt;</span>
</pre>
</div></div>

<p>And the XSLT just needs to declare it at the top level for it to be available:</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml">
<span class="code-tag">&lt;xsl: ...... &gt;</span>

   <span class="code-tag">&lt;<span class="code-keyword">xsl:param</span>
name=<span class="code-quote">"myParam"</span>/&gt;</span>
  
    <span class="code-tag">&lt;<span class="code-keyword">xsl:template</span>
...&gt;</span>
</pre>
</div></div>

<h3><a name="XSLT-SpringXMLversions"></a>Spring XML versions</h3>

<p>To use the above examples in Spring XML you would use something like</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml">
  <span class="code-tag">&lt;camelContext xmlns=<span class="code-quote">"http://activemq.apache.org/camel/schema/spring"</span>&gt;</span>
    <span class="code-tag">&lt;route&gt;</span>
      <span class="code-tag">&lt;from uri=<span class="code-quote">"activemq:My.Queue"</span>/&gt;</span>
      <span class="code-tag">&lt;to uri=<span class="code-quote">"xslt:org/apache/camel/spring/processor/example.xsl"</span>/&gt;</span>
      <span class="code-tag">&lt;to uri=<span class="code-quote">"activemq:Another.Queue"</span>/&gt;</span>
    <span class="code-tag">&lt;/route&gt;</span>
  <span class="code-tag">&lt;/camelContext&gt;</span>
</pre>
</div></div>

<p>There is a <a href="http://svn.apache.org/repos/asf/camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/processor/XsltTest.java"
class="external-link" rel="nofollow">test case</a> along with <a href="http://svn.apache.org/repos/asf/camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/XsltTest-context.xml"
class="external-link" rel="nofollow">its Spring XML</a> if you want a concrete example.</p>

<h3><a name="XSLT-Usingxsl%3Ainclude"></a>Using xsl:include</h3>
<p><b>Camel 1.6.2/2.2 or older</b><br/>
If you use xsl:include in your XSL files then in Camel 2.2 or older it uses the default <tt>javax.xml.transform.URIResolver</tt>
which means it can only lookup files from file system, and its does that relative from the
JVM starting folder.</p>

<p>For example this include:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml">
<span class="code-tag">&lt;<span class="code-keyword">xsl:include</span>
href=<span class="code-quote">"staff_template.xsl"</span>/&gt;</span>
</pre>
</div></div>

<p>Will lookup the <tt>staff_tempkalte.xsl</tt> file from the starting folder
where the application was started.</p>

<p><b>Camel 1.6.3/2.3 or newer</b> <br/>
Now Camel provides its own implementation of <tt>URIResolver</tt> which allows
Camel to load included files from the classpath and more intelligent than before.</p>

<p>For example this include:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml">
<span class="code-tag">&lt;<span class="code-keyword">xsl:include</span>
href=<span class="code-quote">"staff_template.xsl"</span>/&gt;</span>
</pre>
</div></div>

<p>Will now be located relative from the starting endpoint, which for example could
be:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
.to(<span class="code-quote">"xslt:org/apache/camel/component/xslt/staff_include_relative.xsl"</span>)
</pre>
</div></div>

<p>Which means Camel will locate the file in the <b>classpath</b> as <tt>org/apache/camel/component/xslt/staff_template.xsl</tt>.<br/>
This allows you to use xsl include and have xsl files located in the same folder such as we
do in the example <tt>org/apache/camel/component/xslt</tt>.</p>

<p>You can use the following two prefixes <tt>classpath:</tt> or <tt>file:</tt>
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.</p>

<p>You can also refer back in the paths such as</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
    &lt;xsl:include href=<span class="code-quote">"../staff_other_template.xsl"</span>/&gt;
</pre>
</div></div>

<p>Which then will resolve the xsl file under <tt>org/apache/camel/component</tt>.</p>

<h3><a name="XSLT-NotesonusingXSTLandJavaVersions"></a>Notes on using XSTL
and Java Versions</h3>
<p>Here are some observations from Sameer, a Camel user, which he kindly shared with
us:</p>

<blockquote>
<p>In case anybody faces issues with the XSLT endpoint please review these points.</p>

<p>I was trying to use an xslt endpoint for a simple transformation from one xml to
another using a simple xsl. The output xml kept appearing (after the xslt processor in the
route) with outermost xml tag with no content within.</p>

<p>No explanations show up in the DEBUG logs. On the TRACE logs however I did find some
error/warning indicating that the XMLConverter bean could no be initialized.</p>

<p>After a few hours of cranking my mind, I had to do the following to get it to work
(thanks to some posts on the users forum that gave some clue):</p>

<p>1. Use the transformerFactory option in the route <tt>("xslt:my-transformer.xsl?transformerFactory=tFactory")</tt>
with the <tt>tFactory</tt> bean having bean defined in the spring context for
<tt>class="org.apache.xalan.xsltc.trax.TransformerFactoryImpl"</tt>.<br/>
2. Added the Xalan jar into my maven pom.</p>

<p>My guess is that the default xml parsing mechanism supplied within the JDK (I am
using 1.6.0_03) does not work right in this context and does not throw up any error either.
When I switched to Xalan this way it works. This is not a Camel issue, but might need a mention
on the xslt component page.</p>

<p>Another note, jdk 1.6.0_03 ships with JAXB 2.0 while Camel needs 2.1. One workaround
is to add the 2.1 jar to the <tt>jre/lib/endorsed</tt> directory for the jvm or
as specified by the container.</p>

<p>Hope this post saves newbie Camel riders some time.</p></blockquote>

<h3><a name="XSLT-SeeAlso"></a>See Also</h3>
<ul>
	<li><a href="/confluence/display/CAMEL/Configuring+Camel" title="Configuring Camel">Configuring
Camel</a></li>
	<li><a href="/confluence/display/CAMEL/Component" title="Component">Component</a></li>
	<li><a href="/confluence/display/CAMEL/Endpoint" title="Endpoint">Endpoint</a></li>
	<li><a href="/confluence/display/CAMEL/Getting+Started" title="Getting Started">Getting
Started</a></li>
</ul>

    </div>
        <div id="commentsSection" class="wiki-content pageSection">
        <div style="float: right;">
            <a href="https://cwiki.apache.org/confluence/users/viewnotifications.action"
class="grey">Change Notification Preferences</a>
        </div>
        <a href="https://cwiki.apache.org/confluence/display/CAMEL/XSLT">View Online</a>
        |
        <a href="https://cwiki.apache.org/confluence/pages/diffpagesbyversion.action?pageId=69850&revisedVersion=34&originalVersion=33">View
Changes</a>
                |
        <a href="https://cwiki.apache.org/confluence/display/CAMEL/XSLT?showComments=true&amp;showCommentArea=true#addcomment">Add
Comment</a>
            </div>
</div>
</div>
</div>
</div>
</body>
</html>

Mime
View raw message