在Apache Camel XML DSL中更改${body}的内容

icomxhvb  于 2023-06-05  发布在  Apache
关注(0)|答案(1)|浏览(87)

给定以下XML ${body}有效负载对象

<list>
   <library>
      <name>xxx</name>
      <address>
         <line1>aaa</line1>
         <line2>bbb</line2>
         <line3>ccc</line3>
      </address>
   </library>
   <book>
      <author>
         <name>John</name>
         <id>4324234</id>
      </author>
      <title>New Book</title>
      <isbn>dsdaassda</isbn>
   </book>
   <book>...</book>
   <book>...</book>
</list>

我想将标签ID附加到name。因此,基于该示例,名称将是= John,4324234
我在这方面经验很少。我也不会用java。我想把它转换成字符串并做替换,但我不知道如何做到没有编码。
任何帮助将不胜感激。

qij5mzcb

qij5mzcb1#

进行这种修改的一种方法是使用XQuery language,在XML DSL中的路由中,只需使用以下XQuery表达式添加一个transform标记:

<transform>
        <xquery>
            declare namespace local = "http://mytest.org";
            declare function local:copy-replace($element as element()) {
              if ($element/self::name and $element/parent::author)
              then <name>{data($element/text())}, {data($element/../id/text())}</name>
              else element {node-name($element)}
                {$element/@*,
                for $child in $element/node()
                  return if ($child instance of element())
                    then local:copy-replace($child)
                    else $child
                }
            };
            local:copy-replace(/*)
        </xquery>
    </transform>

它基本上会将标签author/name的文本内容替换为“$name, $id”。
这个答案只是为了说明如何在XML DSL中实现,XQuery可能是次优的,它来自this other answer

相关问题