3 Replies Latest reply on Jun 18, 2021 2:43 AM by Prakash Jain

    How to dynamically assign attributes to an element in CAI

    Jon Nettum Active Member

      Hi,

       

      I have a question on how to dynamically assign an attribute to an element, also the attribute uses a namespace. I will give an example to explain.

       

      Let's say I have two possible input XML's:

      1)

      <root>

          <details>

              <anyName>

                  <accountNumber>123123123</accountNumber>

                  <productGroupType>1A</productGroupType>

              </anyName>

              <anyName>

                  <accountNumber>123123123</accountNumber>

                  <productGroupType>1A</productGroupType>

              </anyName>

          </details>

      </root>

       

      2)

      <root>

          <details>

              <otherName>

                  <accountNumber>123123123</accountNumber>

                  <productGroupType>1A</productGroupType>

              </otherName>

              <otherName>

                  <accountNumber>123123123</accountNumber>

                  <productGroupType>1A</productGroupType>

              </otherName>

          </details>

      </root>

       

      I would like to write a XQuery that dynamically rebuilds any of above XML's and adds the namespace and attribute such that the output is like below:

      1)

      <root xmlns:m="urn:informatica:ae:xquery:json2xml:meta-data">

          <details>

              <anyName m:isArray="true">

                  <accountNumber>123123123</accountNumber>

                  <productGroupType>1A</productGroupType>

              </anyName>

              <anyName m:isArray="true">

                  <accountNumber>123123123</accountNumber>

                  <productGroupType>1A</productGroupType>

              </anyName>

          </details>

      </root>

       

      2)

      <root xmlns:m="urn:informatica:ae:xquery:json2xml:meta-data">

          <details>

              <otherName m:isArray="true">

                  <accountNumber>123123123</accountNumber>

                  <productGroupType>1A</productGroupType>

              </otherName>

              <otherName m:isArray="true">

                  <accountNumber>123123123</accountNumber>

                  <productGroupType>1A</productGroupType>

              </otherName>

          </details>

      </root>

       

      I have below XQuery that recreates the XML, but without the attribute.

       

      let $XML1 :=

      <root>

          <details>

              <anyName>

                  <accountNumber>123123123</accountNumber>

                  <productGroupType>1A</productGroupType>

              </anyName>

              <anyName>

                  <accountNumber>123123123</accountNumber>

                  <productGroupType>1A</productGroupType>

              </anyName>

          </details>

      </root>

      let $type := "details"

       

      return

      <root xmlns:m="urn:informatica:ae:xquery:json2xml:meta-data">

          {

          element

          {$type}

          {

          for $e in $XML1/node()[name() = $type]/node()

               return element {fn:name($e)}{$e/node()}

          }

          }

      </root>

       

      Is it possible to extend above XQuery to get the desired output without hard-coding any element or attribute?

       

      Thank you.

       

       

      Br,

      Jon