使用XQuery的Concat XML节点 [英] Concat XML nodes using XQuery

查看:144
本文介绍了使用XQuery的Concat XML节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要以下输出

<name>Thomas Mapother</name>
<name>Tom Cruise</name>

通过XQuery FLOWR表达式使用以下XML.

using the below XML using XQuery FLOWR Expressions.

INSERT INTO XMLO1 VALUES ('<Contact>
<Names>
    <Name Type = "Legal">
        <First>Thomas</First>
        <Middle>T</Middle>
        <Last>Mapother</Last>
    </Name>
    <Name Type = "Stage">
        <First>Tom</First>
        <Middle>C</Middle>
        <Last>Cruise</Last>
    </Name>
</Names>
</Contact>')

我尝试了以下查询.但它会返回不同的输出.

I tried the below query. But it returns a different output.

SELECT xDoc.query('let $names := Contact/Names/Name
return <name>{
    for $x in $names
    return ($x/First,$x/Last)}
</name>')
FROM XMLO1

推荐答案

类似以下内容:

select xDoc.query('
for $x in Contact/Names/Name
return element Name {concat($x/First[1]," ", $x/Last[1])}
')
from XMLO1

或切碎并重组(会更快):

Or shred and recombine (will be faster):

select T.X.value('(First/text())[1]', 'nvarchar(100)')+' '+
         T.X.value('(Last/text())[1]', 'nvarchar(100)')
from XMLO1
  cross apply xDoc.nodes('Contact/Names/Name') as T(X)
for xml path('Name');

这篇关于使用XQuery的Concat XML节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆