0

Given the following XML ${body} payload object

<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>

I want to append the tag id to name. So, based on that example name would be = John, 4324234

I have a minimal experience on this. I cannot use java either. I thought convert it to string and do the replacement, but I don't know how to do it without coding.

Any help will be appreciated.

Esteban
  • 1
  • 2

1 Answers1

0

One way to do this kind of modification is to use the XQuery language, in your route in XML DSL simply add a transform tag with the following XQuery expression:

    <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>

It will basically replace the text content of the tag author/name with the "$name, $id".

This answer is only meant to show how it can be done in XML DSL, the XQuery could be suboptimal and comes from this other answer.

Nicolas Filotto
  • 43,537
  • 11
  • 94
  • 122