Best way to re-create existing root element with all attributes

Sunday, 13 September 2020 · 4 messages · last reply 15 Sep 2020
CG
Christian Grünin reply to Yitzhak Khabinsky, 13 Sep 2020, 23:4613 Sep 2020 22:13 #

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

Quoted text · Yitzhak Khabinsky · 124 lines
On 9/13/20, Yitzhak Khabinsky <ykhabins at 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 > > > > > >
MH
Martin Honnenin reply to Yitzhak Khabinsky, 13 Sep 2020, 23:4613 Sep 2020 22:14 #
Quoted text · Yitzhak Khabinsky · 87 lines
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 }

YK
Yitzhak Khabinsky13 Sep 2020 23:46 #

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

YK
Yitzhak Khabinsky15 Sep 2020 23:26 #

Hi Martin,

Thanks for the answer.

That's exactly what I was looking for.

Regards, Yitzhak Khabinsky


⚡Generated with XQuery