[英]Very complicated XQuery transformation
我有一个非常复杂的XQuery要编写(至少按照我的标准)。
这是我的输入xml:
<testRequest>
<request1>
<Line>1</Line>
<action>addtoName</action>
</request1>
<request2>
<Line>2</Line>
<action>addtoSpace</action>
</request2>
<request3>
<Line>3<Line>
<action>addtospace</action>
</request3>
</testRequest>
在我的输出xml中,操作应作为属性附加到“ request1”元素。 因此,基于request1元素下的action元素,request1元素的属性应为以下之一:
if action = IgnoreCase(addtoName), the request1 element should be <request1 action=insertingname>
if action = IgnoreCase(addtoSpace), the request1 element should be <request1 action=updatingspace>
不仅如此,我还需要根据元素下面的操作值向该元素添加一个属性。 因此,我必须遍历元素下的每个元素,如果是,则查看是否有任何元素等于“ addtospace”,那么我需要获取元素的相应值并组成该元素的属性。 从上面的xml中,我的元素属性应为
<testRequest lineFiller="Line Like 2_* AND Line Like 3_*>, where 2 and 3 are the respective line numbers.
如果没有元素,则元素的属性应“更改”。
因此,总而言之,我转换后的xml应该如下所示:
<testRequest lineFiller="Line Like 2_* AND Line Like 3_*>
<request1 action=insertingname>
<Line>1</Line>
<action>addtoName</action>
</request1>
<request2 action=updatingspace>
<Line>2</Line>
<action>addtoSpace</action>
</request2>
<request3 action=updatingspace>
<Line>3<Line>
<action>addtospace</action>
</request3>
</testRequest>
任何帮助完成这项艰巨任务的工作将不胜感激。
谢谢!!!
您应该定义函数来生成需要添加到元素中的属性。
为了添加到“ request”元素,这应该起作用:
declare function local:getaction($x) {
if (lower-case($x/action) = "addtoname") then attribute action {"insertingspace"} else
if (lower-case($x/action) = "addtospace") then attribute action {"updatingspace"} else
()
};
可以类似地创建linefiller属性:
declare function local:getfiller($x) {
attribute lineFiller {
if ($x/*[lower-case(action) = "addtospace"]) then
string-join(
for $r in $x/*[lower-case(action) = "addtospace"]
return concat("Line Like ",$r/Line,"_*")
, " AND ")
else "change"
}
};
然后放在一起,在原始文档上进行简单的for循环,在需要的地方添加属性:
let $doc:=<<your original document>>
return
<testRequest>
{ local:getfiller($doc) }
{ for $r in $doc/* return
element { name($r) } {
local:getaction($r),
$r/*
}
}
</testRequest>
编辑:增强的getfiller函数,如果没有操作,则返回“更改”
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.