Xs: string as element () in XQuery
Suppose I have XML files like:
<SampleF>
<FirstNode AAA="Something" BBB="Something"></FirstNode>
<SecondNode CCC="Random" DDD="Random"></SecondNode>
</SampleF>
And the second is like this:
<SampleF2>
<FirstNode>
<AAA>Something</AAA>
<BBB>Random</BBB>
</FirstNode>
</SampleF2>
And I would like to get from both (AAA = "Something" / Something) of them like element()
. How do I convert it? When in the first case I get xs:string
in the second document-node()
.
I did something like this for the first example, but I am 100% sure there is a better way to do it
declare function getElementFirstExample($message) as element() {
let $name := "AAA"
let $value := $message/*:SampleF1/*:FirstNode/@AAA
return element {$name} {"$value"}
};
Thanks in advance for your help and advice.
source to share
As I understand it, you need the value of an attribute <FirstNodes/>
AAA
or child, regardless of whether it is in an element or an attribute.
Use an alternate axis spacing to access the attribute and element and data(...)
to get the string value.
data(//FirstNode/(@AAA, AAA))
Putting this together for your function and explicit use case:
declare function getElementFirstExample($message) as element() { let $name := "AAA" let $value := data($message/*:SampleF1/*:FirstNode/(@AAA, *:AAA)) return element {$name} {"$value"} };
source to share