camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From conflue...@apache.org
Subject [CONF] Apache Camel > XQuery
Date Tue, 25 Oct 2011 20:11: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/XQuery">XQuery</a></h2>
    <h4>Page <b>edited</b> by             <a href="https://cwiki.apache.org/confluence/display/~davsclaus">Claus
Ibsen</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" >Camel supports [XQuery|http://www.w3.org/TR/xquery/]
to allow an [Expression] or [Predicate] to be used in the [DSL] or [Xml Configuration]. For
example you could use XQuery to create an [Predicate] in a [Message Filter] or as an [Expression]
for a [Recipient List]. <br> <br></td></tr>
            <tr><td class="diff-added-lines" style="background-color: #dfd;">h3.
Options <br>{div:class=confluenceTableSmall} <br>|| Name || Default Value || Description
|| <br>| {{allowStAX}} | {{false}} | *Camel 2.8.3/2.9:* Whether to allow using StAX
as the {{javax.xml.transform.Source}}. | <br>{div} <br> <br>h3. Examples
<br> <br></td></tr>
            <tr><td class="diff-unchanged" >{code} <br>from(&quot;queue:foo&quot;).filter().
<br></td></tr>
            <tr><td class="diff-snipped" >...<br></td></tr>
    
            </table>
    </div>                            <h4>Full Content</h4>
                    <div class="notificationGreySide">
        <h2><a name="XQuery-XQuery"></a>XQuery</h2>

<p>Camel supports <a href="http://www.w3.org/TR/xquery/" class="external-link" rel="nofollow">XQuery</a>
to allow an <a href="/confluence/display/CAMEL/Expression" title="Expression">Expression</a>
or <a href="/confluence/display/CAMEL/Predicate" title="Predicate">Predicate</a>
to be used in the <a href="/confluence/display/CAMEL/DSL" title="DSL">DSL</a>
or <a href="/confluence/display/CAMEL/Xml+Configuration" title="Xml Configuration">Xml
Configuration</a>. For example you could use XQuery to create an <a href="/confluence/display/CAMEL/Predicate"
title="Predicate">Predicate</a> in a <a href="/confluence/display/CAMEL/Message+Filter"
title="Message Filter">Message Filter</a> or as an <a href="/confluence/display/CAMEL/Expression"
title="Expression">Expression</a> for a <a href="/confluence/display/CAMEL/Recipient+List"
title="Recipient List">Recipient List</a>.</p>

<h3><a name="XQuery-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>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="XQuery-Examples"></a>Examples</h3>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
from(<span class="code-quote">"queue:foo"</span>).filter().
  xquery(<span class="code-quote">"<span class="code-comment">//foo"</span>).
</span>  to(<span class="code-quote">"queue:bar"</span>)
</pre>
</div></div>

<p>You can also use functions inside your query, in which case you need an explicit
type conversion (or you will get a org.w3c.dom.DOMException: HIERARCHY_REQUEST_ERR) by passing
the Class as a second argument to the <b>xquery()</b> method.</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
from(<span class="code-quote">"direct:start"</span>).
  recipientList().xquery(<span class="code-quote">"concat('mock:foo.', /person/@city)"</span>,
<span class="code-object">String</span>.class);
</pre>
</div></div>

<h3><a name="XQuery-Variables"></a>Variables</h3>

<p>The IN message body will be set as the <tt>contextItem</tt>. Besides
this these Variables is also added as parameters:</p>

<div class='table-wrap'>
<table class='confluenceTable'><tbody>
<tr>
<th class='confluenceTh'> Variable </th>
<th class='confluenceTh'> Type </th>
<th class='confluenceTh'> Description </th>
<th class='confluenceTh'> Support version </th>
</tr>
<tr>
<td class='confluenceTd'> exchange </td>
<td class='confluenceTd'> Exchange </td>
<td class='confluenceTd'> The current Exchange </td>
<td class='confluenceTd'>&nbsp;</td>
</tr>
<tr>
<td class='confluenceTd'> in.body </td>
<td class='confluenceTd'> Object </td>
<td class='confluenceTd'> The In message's body  </td>
<td class='confluenceTd'>  &gt;= 1.6.1 </td>
</tr>
<tr>
<td class='confluenceTd'> out.body </td>
<td class='confluenceTd'> Object </td>
<td class='confluenceTd'> The OUT message's body (if any)</td>
<td class='confluenceTd'> &gt;= 1.6.1 </td>
</tr>
<tr>
<td class='confluenceTd'> in.headers.*</td>
<td class='confluenceTd'> Object </td>
<td class='confluenceTd'> You can access the value of exchange.in.headers with key <b>foo</b>
by using the variable which name is in.headers.foo</td>
<td class='confluenceTd'> &gt;=1.6.1 </td>
</tr>
<tr>
<td class='confluenceTd'> out.headers.* </td>
<td class='confluenceTd'> Object </td>
<td class='confluenceTd'> You can access the value of exchange.out.headers with key
<b>foo</b> by using the variable which name is out.headers.foo variable</td>
<td class='confluenceTd'> &gt;=1.6.1 </td>
</tr>
<tr>
<td class='confluenceTd'> <b>key name</b> </td>
<td class='confluenceTd'> Object </td>
<td class='confluenceTd'> Any exchange.properties and exchange.in.headers (exchange.in.headers
support was removed since camel 1.6.1) and any additional parameters set using <tt>setParameters(Map)</tt>.
These parameters is added with they own key name, for instance if there is an IN header with
the key name <b>foo</b> then its added as <b>foo</b>. </td>
<td class='confluenceTd'>&nbsp;</td>
</tr>
</tbody></table>
</div>


<h3><a name="XQuery-UsingXMLconfiguration"></a>Using XML configuration</h3>

<p>If you prefer to configure your routes in your <a href="/confluence/display/CAMEL/Spring"
title="Spring">Spring</a> XML file then you can use XPath expressions as follows</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml">
&lt;beans xmlns=<span class="code-quote">"http://www.springframework.org/schema/beans"</span>
       <span class="code-keyword">xmlns:xsi</span>=<span class="code-quote">"http://www.w3.org/2001/XMLSchema-instance"</span>
       <span class="code-keyword">xmlns:foo</span>=<span class="code-quote">"http://example.com/person"</span>
       xsi:schemaLocation="
       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
       http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd"&gt;

  <span class="code-tag">&lt;camelContext id=<span class="code-quote">"camel"</span>
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:MyQueue"</span>/&gt;</span>
      <span class="code-tag">&lt;filter&gt;</span>
        <span class="code-tag">&lt;xquery&gt;</span>/foo:person[@name='James']<span
class="code-tag">&lt;/xquery&gt;</span>
        <span class="code-tag">&lt;to uri=<span class="code-quote">"mqseries:SomeOtherQueue"</span>/&gt;</span>
      <span class="code-tag">&lt;/filter&gt;</span>
    <span class="code-tag">&lt;/route&gt;</span>
  <span class="code-tag">&lt;/camelContext&gt;</span>
<span class="code-tag">&lt;/beans&gt;</span>
</pre>
</div></div>

<p>Notice how we can reuse the namespace prefixes, <b>foo</b> in this case,
in the XPath expression for easier namespace based XQuery expressions!</p>

<p>When you use functions in your XQuery expression you need an explicit type conversion
which is done in the xml configuration via the <b>@type</b> attribute: </p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml">
    <span class="code-tag">&lt;xquery type=<span class="code-quote">"java.lang.String"</span>&gt;</span>concat('mock:foo.',
/person/@city)<span class="code-tag">&lt;/xquery&gt;</span>
</pre>
</div></div>

<h3><a name="XQuery-UsingXQueryasanendpoint"></a>Using XQuery as an endpoint</h3>

<p>Sometimes an XQuery expression can be quite large; it can essentally be used for
<a href="/confluence/display/CAMEL/Templating" title="Templating">Templating</a>.
So you may want to use an <a href="/confluence/display/CAMEL/XQuery+Endpoint" title="XQuery
Endpoint">XQuery Endpoint</a> so you can route using XQuery templates.</p>

<p>The following example shows how to take a message of an ActiveMQ queue (MyQueue)
and transform it using XQuery and send it to MQSeries.</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
  &lt;camelContext id=<span class="code-quote">"camel"</span> xmlns=<span
class="code-quote">"http:<span class="code-comment">//camel.apache.org/schema/spring"</span>&gt;
</span>    &lt;route&gt;
      &lt;from uri=<span class="code-quote">"activemq:MyQueue"</span>/&gt;
      &lt;to uri=<span class="code-quote">"xquery:com/acme/someTransform.xquery"</span>/&gt;
      &lt;to uri=<span class="code-quote">"mqseries:SomeOtherQueue"</span>/&gt;
    &lt;/route&gt;
  &lt;/camelContext&gt;
</pre>
</div></div>

<h3><a name="XQuery-Examples"></a>Examples</h3>

<p>Here is a simple <a href="http://svn.apache.org/repos/asf/camel/trunk/components/camel-saxon/src/test/java/org/apache/camel/builder/saxon/XQueryFilterTest.java"
class="external-link" rel="nofollow">example</a> using an XQuery expression as a
predicate in a <a href="/confluence/display/CAMEL/Message+Filter" title="Message Filter">Message
Filter</a></p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">from(<span class="code-quote">"direct:start"</span>).filter().xquery(<span
class="code-quote">"/person[@name='James']"</span>).to(<span class="code-quote">"mock:result"</span>);
</pre>
</div></div>

<p>This <a href="http://svn.apache.org/repos/asf/camel/trunk/components/camel-saxon/src/test/java/org/apache/camel/builder/saxon/XQueryWithNamespacesFilterTest.java"
class="external-link" rel="nofollow">example</a> uses XQuery with namespaces as a
predicate in a <a href="/confluence/display/CAMEL/Message+Filter" title="Message Filter">Message
Filter</a></p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">Namespaces ns = <span class="code-keyword">new</span>
Namespaces(<span class="code-quote">"c"</span>, <span class="code-quote">"http:<span
class="code-comment">//acme.com/cheese"</span>);
</span>
from(<span class="code-quote">"direct:start"</span>).
        filter().xquery(<span class="code-quote">"/c:person[@name='James']"</span>,
ns).
        to(<span class="code-quote">"mock:result"</span>);
</pre>
</div></div>

<h3><a name="XQuery-LearningXQuery"></a>Learning XQuery</h3>

<p>XQuery is a very powerful language for querying, searching, sorting and returning
XML. For help learning XQuery try these tutorials</p>

<ul>
	<li>Mike Kay's <a href="http://www.stylusstudio.com/xquery_primer.html" class="external-link"
rel="nofollow">XQuery Primer</a></li>
	<li>the W3Schools <a href="http://www.w3schools.com/xquery/default.asp" class="external-link"
rel="nofollow">XQuery Tutorial</a></li>
</ul>


<p>You might also find the <a href="http://www.w3.org/TR/xpath-functions/" class="external-link"
rel="nofollow">XQuery function reference</a> useful</p>

<h3><a name="XQuery-Dependencies"></a>Dependencies</h3>

<p>To use XQuery in your camel routes you need to add the a dependency on <b>camel-saxon</b>
which implements the XQuery language. </p>

<p>If you use maven you could just add the following to your pom.xml, substituting the
version number for the latest &amp; greatest release (see <a href="/confluence/display/CAMEL/Download"
title="Download">the download page for the latest versions</a>).</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
&lt;dependency&gt;
  &lt;groupId&gt;org.apache.camel&lt;/groupId&gt;
  &lt;artifactId&gt;camel-saxon&lt;/artifactId&gt;
  &lt;version&gt;1.4.0&lt;/version&gt;
&lt;/dependency&gt;
</pre>
</div></div>
    </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/XQuery">View Online</a>
        |
        <a href="https://cwiki.apache.org/confluence/pages/diffpagesbyversion.action?pageId=54162&revisedVersion=19&originalVersion=18">View
Changes</a>
                |
        <a href="https://cwiki.apache.org/confluence/display/CAMEL/XQuery?showComments=true&amp;showCommentArea=true#addcomment">Add
Comment</a>
            </div>
</div>
</div>
</div>
</div>
</body>
</html>

Mime
View raw message