title: Dynamic Path in XQUERY
body: <p>This is my xml-file:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;QQ:Envelope xmlns:QQ="http://schemas.xmlsoap.org/soap/envelope/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"&gt;
     &lt;SOAP-ENV:Header&gt;
        &lt;RR:ABCInfo xmlns:RR="http://abc.test.de/abc/SOAP-Header/1.0"&gt;
           &lt;RR:Version&gt;2.2.2.2&lt;/RR:Version&gt;
           &lt;RR:BuildRevision&gt;3333&lt;/RR:BuildRevision&gt;
           &lt;RR:BuildTimestamp&gt;2019-01-01T00:00:00.000+02:00&lt;/RR:BuildTimestamp&gt;
           &lt;RR:Start&gt;2019-01-01T10:10:10.101+02:00&lt;/RR:Start&gt;
           &lt;RR:End&gt;2019-01-01T11:11:11.111+02:00&lt;/RR:End&gt;
           &lt;RR:Something&gt;2.222 sek.&lt;/RR:Something&gt;
           &lt;RR:Anything/&gt;
        &lt;/RR:ABCInfo&gt;
        &lt;work:WorkContext xmlns:work="http://test.com/"&gt;1234567890abcdefghijklmnopqrstuvwxyz&lt;/work:WorkContext&gt;
     &lt;/SOAP-ENV:Header&gt;
     &lt;QQ:Body&gt;
        &lt;TT:testA xmlns:TT="http://abc.test.de/XYZ/2.0.1" xmlns:RR="http://abc.test.de/abc/abcdefgh/1.0"&gt;
           &lt;TT:testB&gt;
              &lt;TT:testC&gt;
                 &lt;TT:testD&gt;                  
                    &lt;TT:testE id="1234567" quellID="09876543"&gt;
                       &lt;TT:data&gt;urn:de:abc:test:whatever&lt;/TT:data&gt;
                       &lt;TT:changeDate&gt;2019-02-02T02:02:02.020+02:00&lt;/TT:changeDate&gt;
                       &lt;TT:part1 listURI="urn:de:abc:codeliste:555" listVersionID="V12"&gt;
                          &lt;code&gt;555_777&lt;/code&gt;
                          &lt;name&gt;Fischers Fritze&lt;/name&gt;
                       &lt;/TT:part1&gt;
                       &lt;TT:piece2&gt;Frische Fische fischen&lt;/TT:piece2&gt;
                       &lt;TT:begin&gt;
                          &lt;TT:date&gt;20191231&lt;/TT:date&gt;
                       &lt;/TT:begin&gt;
                    &lt;/TT:testE&gt;
                 &lt;/TT:testD&gt;
              &lt;/TT:testC&gt;
           &lt;/TT:testB&gt;
        &lt;/TT:testA&gt;
     &lt;/QQ:Body&gt;
  &lt;/QQ:Envelope&gt;
  </code></pre>
  
  <p>I have a XQuery, where I have to return XML. The first element in the returning XML is "result". The other elements in the returning XML should be dynamically created.</p>
  
  <p>I get 2 sequences from outside, though I have made 2 fix Sequences in the following example to test it.
  In Sequence No 1 I get the names for the other elements.
  In Sequence No 2 I get the related path to the element names in Sequence 1.</p>
  
  <p>I open the XML file an read a path (there might be several elements, though in my example is only one.
  Then I want to process this result in a loop and return the dynamic elements.
  If I access the path with a fix value (variable $c in the following code) I get the correct value, but then I must know the elements in Sequence 1 and the path in Sequence 2.
  If I concatenate the path then I get the value from all elements.</p>
  
  <p>This is my XQuery Code:</p>
  
  <pre><code>declare namespace TT="http://abc.test.de/XYZ/2.0.1";
  declare namespace QQ="http://schemas.xmlsoap.org/soap/envelope/";
  
  declare function local:getValue($path) as xs:string {
        if (fn:exists($path)) then
        (
            data($path)
        ) else (
            ""
        )
  };
  
  let $a := ('part1', 'piece2', 'beginDate')
  let $b := ('TT:part1/name','TT:piece2', 'TT:begin/TT:date')
  
  for $x in doc("Test.XML")/QQ:Envelope/QQ:Body/TT:testA/TT:testB/TT:testC/TT:testD/TT:testE
  
  return &lt;result&gt;
        {
        for $item at $ind in $a
            let $c := local:getValue($x/TT:part1/name)
            let $d := local:getValue($x || concat("/", $b[$ind]))
  
            return element { $item } {$c, "  ---  ", $d}
        }
     &lt;/result&gt;
  </code></pre>
  
  <p>Is there a possibility to access the path dynamically?</p>
  
  <p>Thank you in advance.</p>