我需要做一个检查条件,如果条件不满足需要硬编码值。下面是我的xml。
我需要检查子根内部的情况 - 如果ItemType = Table1和ItemCondition = Chair1那么我必须给出一个硬编码值'Proceed'(这个硬编码值我将映射到目标端)
<Root>
<SubRoot>
<ItemType>Table1</ItemType>
<ItemCondition>Chair1</ItemCondition>
<ItemValue>
.......
</ItemValue>
</SubRoot>
<SubRoot>
<ItemType>Table2</ItemType>
<ItemCondition>chair2</ItemCondition>
<ItemValue>
.......
</ItemValue>
</SubRoot>
</Root>
我试过这样但似乎没有工作
/Root/SubRoot[ItemType='Table1' and ItemCondition='Chair1']='Proceed'
你能不能请任何人帮忙。提前谢谢。
编辑:
if (SubRoot[ItemType ='Table1' and ItemCondition ='Chair1']) then 'Proceed' else 'Decline'
答案 0 :(得分:2)
由于您没有指定是否完全转换文档,我将回答假设您在代码中单独处理XML元素。
我经常使用XPath中的“if”条件:
substring(<default_text>, 1 div <condition>)
如果右侧最终为<default_text>
,那么它基本上会为您提供1 div <non-zero value>
。所以在你的情况下,也许你可以这样做:
substring("Proceed", 1 div boolean(//Root/SubRoot[ItemType="Table1" and ItemCondition="Chair1"]))
这应该只为符合您指定条件的元素提供“继续”。
但是,我在编辑中注意到您还需要“其他”值。一旦你理解了我刚给你看的方法,那么你可以做一些聪明的连续工作来准确地给你你想要的东西:
concat(
substring("Proceed", 1 div boolean(//Root/SubRoot[ItemType="Table1" and ItemCondition="Chair1"])),
substring("Decline", 1 div not(//Root/SubRoot[ItemType="Table1" and ItemCondition="Chair1"]))
)
因此,如果元素满足这些条件,则基本上应该concat("Proceed", "")
导致“继续”,而concat("", "Decline")
导致不满足这些条件的元素“拒绝”。 / p>
答案 1 :(得分:0)
XPath用于选择节点或节点集。我认为你应该检查XQuery并生成新的XML文件。
就像
for $x in /Root/SubRoot
return if ($x/ItemType='Table1' and $x/ItemCondition='Chair1')
then <SubRoot>'Proceed'</SubRoot>
else $x