Best way to re-create existing root element with all attributes
Hello, This is input XML: <employee emp="1" dept="55"> <name>Peter</name> <job>Director</job> <employee emp="12" dept="26"> <name>John</name> <job>Animator</job> <employee emp="123" dept="27"> <name>James</name> <job>Assistant</job> </employee> </employee> </employee> Desired output XML: <employee emp="1" dept="55"> <name>Peter</name> <job>Director</job> </employee> Overall, it is a so called 'modified identity transformation' in XSLT parlance. Output XML shall be the same, except the child <employee> element and its children. Question: What is the best way to re-create existing root element with all its attributes? My implementation: xquery version "3.1"; declare context item := document { <employee emp="1" dept="55"> <name>Peter</name> <job>Director</job> <employee emp="12" dept="26"> <name>John</name> <job>Animator</job> <employee emp="123" dept="27"> <name>James</name> <job>Assistant</job> </employee> </employee> </employee> }; (: working version :) element {fn:node-name(/*)} { let $y := ./*/@* return $y, (: copy all attributes :) for $x in ./*/(* except employee) return $x } (: NOT working version, trying to make it more concise :) element {fn:node-name(/*)} {/*/@*} { return for $x in ./*/(* except employee) return $x } Regards, Yitzhak Khabinsky
Hi Yitzhak, Try this: declare context item := document { ... }; * update { delete node employee } See [1] for more information on updating expressions. Best, Christian [1] https://docs.basex.org/wiki/XQuery_Update On 9/13/20, Yitzhak Khabinsky <ykhabins@bellsouth.net> wrote:
Hello,
This is input XML:
<employee emp="1" dept="55"> <name>Peter</name> <job>Director</job> <employee emp="12" dept="26"> <name>John</name> <job>Animator</job> <employee emp="123" dept="27"> <name>James</name> <job>Assistant</job> </employee> </employee> </employee>
Desired output XML:
<employee emp="1" dept="55"> <name>Peter</name> <job>Director</job> </employee>
Overall, it is a so called 'modified identity transformation' in XSLT parlance.
Output XML shall be the same, except the child <employee> element and its children.
Question:
What is the best way to re-create existing root element with all its attributes?
My implementation:
xquery version "3.1";
declare context item := document {
<employee emp="1" dept="55">
<name>Peter</name>
<job>Director</job>
<employee emp="12" dept="26">
<name>John</name>
<job>Animator</job>
<employee emp="123" dept="27">
<name>James</name>
<job>Assistant</job>
</employee>
</employee>
</employee>
};
(: working version :)
element {fn:node-name(/*)}
{
let $y := ./*/@*
return $y, (: copy all attributes :)
for $x in ./*/(* except employee)
return $x
}
(: NOT working version, trying to make it more concise :)
element {fn:node-name(/*)} {/*/@*}
{
return for $x in ./*/(* except employee)
return $x
}
Regards, Yitzhak Khabinsky
On 13.09.2020 21:46, Yitzhak Khabinsky wrote:
Hello,
This is input XML:
<employee emp="1"dept="55"> <name>Peter</name> <job>Director</job> <employee emp="12"dept="26"> <name>John</name> <job>Animator</job> <employee emp="123"dept="27"> <name>James</name> <job>Assistant</job> </employee> </employee> </employee>
Desired output XML:
<employee emp="1"dept="55"> <name>Peter</name> <job>Director</job> </employee>
Overall, it is a so called ‘modified identity transformation’ in XSLT parlance.
Output XML shall be the same, except the child <employee> element and its children.
**
Question:
*What is the best way to re-create existing root element with all its attributes?*
My implementation:
xquery version "3.1";
declare context item := document {
<employee emp="1" dept="55">
<name>Peter</name>
<job>Director</job>
<employee emp="12" dept="26">
<name>John</name>
<job>Animator</job>
<employee emp="123" dept="27">
<name>James</name>
<job>Assistant</job>
</employee>
</employee>
</employee>
};
(: working version :)
element {fn:node-name(/*)}
{
let $y := ./*/@*
return $y, (: copy all attributes :)
for $x in ./*/(* except employee)
return $x
}
(: NOT working version, trying to make it more concise :)
I would write /*/element { node-name() } { @*, * except employee }
participants (3)
-
Christian Grün -
Martin Honnen -
Yitzhak Khabinsky