问题
I want to insert a node using below code but if i will rerun the code i don't want my node to be repeated twice if all the elements are having same value-
let $doc := fn:doc("abc.xml")
(: abc.xml looks like--
<root>
<value1>somevalue</value1>
<value2>somevalue</value2>
<value3>somevalue</value3>
<value4>somevalue</value4>
<Country>Australia</Country>
<value6>somevalue</value6>
<value7>somevalue</value7>
<value8>somevalue</value8>
<value9>somevalue</value9>
<value10>somevalue</value10>
</root> :)
let $wrapper := if($doc//Country eq "Australia") then "AUS"
else if($doc//Country eq "India") then "IND"
else ()
let $element := element {$wrapper}{
element A{"A"},
element B{"B"},
element C{"C"},
element D{"D"},
element E{"E"}
}
let $doc := xdmp:node-insert-after(doc("abc.xml")//value4, $element)
return doc("abc.xml")
Whenever i am running this query my ELEMENT is getting inserted after value4 but i want that if i am running this multiple times then it should check all the "element" as well as $wrapper. If it already exists then it should do nothing but if any value is changed then either it should replace the wrapper or add a new wrapper
Example- If i am running this 2 times-
Actual Output when my country is AUSTRALIA and elements are "ABCDE"
<root>
<value1>somevalue</value1>
<value2>somevalue</value2>
<value3>somevalue</value3>
<value4>somevalue</value4>
<AUS>
<A>A</A>
<B>B</B>
<C>C</C>
<D>D</D>
<E>E</E>
</AUS>
<AUS>
<A>A</A>
<B>B</B>
<C>C</C>
<D>D</D>
<E>E</E>
</AUS>
<Country>Australia</Country>
<value8>somevalue</value8>
<value9>somevalue</value9>
<value10>somevalue</value10>
</root>
Expected Output- After running 2 or more times
Case-1-If the elements are "ABCDE" and country is AUSTRALIA then it should give-
<root>
<value1>somevalue</value1>
<value2>somevalue</value2>
<value3>somevalue</value3>
<value4>somevalue</value4>
<AUS>
<A>A</A>
<B>B</B>
<C>C</C>
<D>D</D>
<E>E</E>
</AUS>
<Country>Australia</Country>
<value8>somevalue</value8>
<value9>somevalue</value9>
<value10>somevalue</value10>
</root>
Expected Output- Case-2-If the elements are "ABCFJ" and country is AUSTRALIA then it should give-
<root>
<value1>somevalue</value1>
<value2>somevalue</value2>
<value3>somevalue</value3>
<value4>somevalue</value4>
<AUS>
<A>A</A>
<B>B</B>
<C>C</C>
<D>F</D>
<E>J</E>
</AUS>
<Country>Australia</Country>
<value8>somevalue</value8>
<value9>somevalue</value9>
<value10>somevalue</value10>
</root>
Expected Output- Case-3-If the elements are "ABCDE" and country changed to INDIA in 2nd run then it should give-
<root>
<value1>somevalue</value1>
<value2>somevalue</value2>
<value3>somevalue</value3>
<value4>somevalue</value4>
<AUS>
<A>A</A>
<B>B</B>
<C>C</C>
<D>F</D>
<E>E</E>
</AUS>
<IND>
<A>A</A>
<B>B</B>
<C>C</C>
<D>F</D>
<E>E</E>
</IND>
<Country>Australia</Country>
<value8>somevalue</value8>
<value9>somevalue</value9>
<value10>somevalue</value10>
</root>
Any Suggestions ??
回答1:
Check for presence of the 'AUS' element before adding it, and then either don't add it a second time, or replace it with xdmp:node-replace.
HTH!
回答2:
The question and answer are the same as to your previous question How to insert a constructed XML nodes in XQuery?
It makes no difference if the node is text-only content, element, or mixed-content.
回答3:
@shalini-- Please test below query, I think this will fulfil your all requirements.
xquery version "1.0-ml";
let $input :=doc("abc.xml")
let $wrapper := if($input//Country eq "Australia") then "AUS"
else if($input//Country eq "India") then "IND"
else ()
let $element := element {$wrapper}{
element A{"A"},
element B{"E"},
element C{"D"},
element D{"D"},
element E{"E"}
}
let $result:=(
if(fn:exists($input//AUS) and ($wrapper eq "AUS") and ($element eq
$input//AUS)) then ("Element already added no need to update")
else if(fn:exists($input//AUS) and ($wrapper eq "AUS") and ($element ne
$input//AUS))
then(
let $insert-node :=(xdmp:node-insert-after($input//value4,
$element),
<root>
<Meassge>==Below Element added==</Meassge>
<element>{$element}</element>
</root>)
return $insert-node
)
else if(fn:exists($input//IND) and ($wrapper eq "IND") and ($element eq
$input//IND)) then ("Element already added no need to update")
else if(fn:exists($input//IND) and ($wrapper eq "IND") and ($element ne
$input//IND))
then(
let $insert-node :=(xdmp:node-insert-after($input//value4, $element),
<root>
<Meassge>==Below Element added==</Meassge>
<element>{$element}</element>
</root>)
return $insert-node
)
else(
let $insert-node :=(xdmp:node-insert-after($input//value4, $element),
<root>
<Meassge>==Below Element added==</Meassge>
<element>{$element}</element>
</root>)
return $insert-node
)
)
return $result
来源:https://stackoverflow.com/questions/52476912/how-to-insert-a-new-element-after-a-node-in-xquery