如何使用Xquery合并整个文档中相同和重复元素的所有上下文?
示例文档:
<webMessage xmlns="http://www.website.gov.uk/CM/envelope">
<EnvelopeVersion>2.0</EnvelopeVersion>
<Header>
<MessageDetails>
<Class>Web-CT600</Class>
<Qualifier/>
<Function/>
</MessageDetails>
<SenderDetails>
<IDAuthentication>
<SenderID/>
<Authentication>
<Method/>
<Role/>
<Value/>
</Authentication>
</IDAuthentication>
</SenderDetails>
</Header>
<webTalkDetails>
<Keys>
<Key Type="UTR">2274792909</Key>
</Keys>
<ChannelRouting>
<Channel>
<URI/>
<Product/>
<Version/>
</Channel>
</ChannelRouting>
</webTalkDetails>
<Body>
<IRenvelope xmlns="http://www.website.gov.uk/taxation/CT/3">
<IRheader>
<Keys>
<Key Type="UTR">2274792909</Key>
</Keys>
<PeriodEnd/>
<DefaultCurrency/>
<IRmark Type="generic">n1uS2MiavBsb6YwL82MK</IRmark>
<Sender/>
</IRheader>
<CompanyReturn ReturnType="new">
<CompanyInformation>
<CompanyName/>
<RegistrationNumber/>
<Reference/>
<PeriodCovered>
<From>2013-01-07</From>
<To>2014-01-07</To>
</PeriodCovered>
</CompanyInformation>
<Turnover>
<Total>45893</Total>
</Turnover>
<CompanyCalculation>
<Income>
<TradingAndProfessional>
<Profits>95517</Profits>
<NetProfits>51276</NetProfits>
</TradingAndProfessional>
</Income>
</CompanyCalculation>
<AttachedFiles>
<Xsubmission>
<Accounts>
<Instance>
<EncodedInlineSubmission> TEXT I WANT TO JOIN</EncodedInlineSubmission>
</Instance>
</Accounts>
<Computations>
<Instance>
<EncodedInlineSubmission> MORE TEXT I WANT TO JOIN</EncodedInlineSubmission>
</Instance>
</Computations>
</Xsubmission>
</AttachedFiles>
</CompanyTaxReturn>
</IRenvelope>
</Body>
所以在这里的XML中,我希望将所有实例中的所有文本组合在一起,并将它们放入一个单元素中,这样它就会读取:
<EncodedInlineSubmission> TEXT I WANT TO JOIN MORE TEXT I WANT TO JOIN</EncodedInlineSubmission>
答案 0 :(得分:0)
您可以使用所有元素的连接值和元素的先前名称来构造新元素:
for $x in //*:Xsubmission
let $encoded := $x//*:EncodedInlineSubmission
return element {$encoded[1]/local-name()} {string-join($encoded)}
答案 1 :(得分:0)
更新:在返回的字符串周围添加了一个元素构造函数。
您可以使用fn:string-join()
来加入带有连接字符串的字符串序列。您需要评估选择要加入的所有节点的XPath表达式,然后检索它们的字符串值。
以下是一个例子:
declare namespace env = "http://www.website.gov.uk/CM/envelope";
let $nodes := $doc/env:webMessage/env:Body//env:EncodedInlineSubmission
return element EncodedInlineSubmission { fn:string-join($nodes/fn:string(), " ") }
注意:
$doc
绑定到示例文档的文档节点