我想使用下面的代码插入一个节点,但是如果我将重新运行代码,我不希望我的节点在所有元素都具有相同值的情况下重复两次-
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")
每当我运行此查询时,都会在value4之后插入我的ELEMENT,但我希望如果我多次运行此元素,则它应检查所有的“ element”以及$ 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>
任何建议??
答案 0 :(得分:1)
在添加“ AUS”元素之前检查其是否存在,然后不再次添加它,或将其替换为xdmp:node-replace。
HTH!
答案 1 :(得分:1)
问题和答案与您先前的问题How to insert a constructed XML nodes in XQuery?
该节点是纯文本内容,元素还是混合内容都没有区别。
答案 2 :(得分:1)
@ shalini--请在下面的查询中进行测试,我认为这将满足您的所有要求。
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