73.3. 例
from("queue:foo")
.filter().xquery("//foo")
.to("queue:bar")
クエリー内で関数を使用することもできますが、その場合は明示的な型変換が必要で、そうしないと org.w3c.dom.DOMException が発生します。hierarchy_request_err) が発生します。関数の期待される出力型を渡す必要があります。例えば、concat 関数は String を 返すが、これは次のように行われる。
from("direct:start")
.recipientList().xquery("concat('mock:foo.', /person/@city)", String.class);
そして XML DSL では:
<route>
<from uri="direct:start"/>
<recipientList>
<xquery type="java.lang.String">concat('mock:foo.', /person/@city</xquery>
</recipientList>
</route>
73.3.1. ネームスペースの利用 リンクのコピーリンクがクリップボードにコピーされました!
使用したい標準的な名前空間のセットがあり、それを多くの XQuery 式で共有したい場合は、図のように、Java DSL を使用する際に org.apache.camel.support.builder.Namespaces を使用できます。
Namespaces ns = new Namespaces("c", "http://acme.com/cheese");
from("direct:start")
.filter().xquery("/c:person[@name='James']", ns)
.to("mock:result");
2 番目のパラメーターとして渡される ns 変数で、名前空間が xquery に 提供されていることに注目してください。
各名前空間はキー=値のペアで、接頭辞がキーとなります。XQuery 式では、名前空間はその接頭辞で使用されます (例)。
/c:person[@name='James']
名前空間ビルダーは、図のように複数の名前空間の追加をサポートしています。
Namespaces ns = new Namespaces("c", "http://acme.com/cheese")
.add("w", "http://acme.com/wine")
.add("b", "http://acme.com/beer");
XML DSL で名前空間を使用する場合は、XML ルートタグ (または camelContext、routes、route タグの 1 つ) で名前空間を設定するため、それとは異なります。
以下の XML の例では、Spring XML を使用しています。名前空間はルートタグの bean で、xmlns:foo="http://example.com/person " の行で宣言されています。
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:foo="http://example.com/person"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd">
<camelContext id="camel" xmlns="http://activemq.apache.org/camel/schema/spring">
<route>
<from uri="activemq:MyQueue"/>
<filter>
<xquery>/foo:person[@name='James']</xquery>
<to uri="mqseries:SomeOtherQueue"/>
</filter>
</route>
</camelContext>
</beans>
この名前空間は接頭辞として foo を 使用するので、<xquery> 式ではこの名前空間を使用するために /foo: を使用します。