第一个 第二秒
//client-agent
/root/other/client-agent
或
/root/other/other/client-agent
不幸的是,没有简单的方法可以在SQL Server中获得此功能。 您可以使用一些Xquery通过XML中的所有节点下降,检查上下文节点,然后返回节点名称。对于大量节点,这可能非常降低。
/root/other/other/client-agent
函数,甚至是
/root/other/other/client-agent/client-agent
SET
begin
declare @Xml xml
set @Xml = convert(xml,'<root><other><client-agent type="a" /><other><client-agent type="b" /><client-agent type="c" ><some-info /></client-agent></other></other></root>')
select @Xml
select t.n.value('@type','varchar(max)') as [client-agent@type]
, t.n.query('.') OuterXml
, 'Is there a "t.n.SomeXmlFunction(WithTheRequiredParameters)" expression to get the node path' as [the-path-i-want-to-get]
from @Xml.nodes('//client-agent') as t(n)
end
到上下文节点。
从XML顶部通过所有后代节点的环。
如果该节点具有任何后代节点(包括本身),以...
.query()
然后返回
.value('','')
加上该节点的名称。
我们需要使用path-to-node-with-pos
ancestor::
这不会返回每个节点的位置。为此,您需要一个更复杂的Xquery,它也无法使用$current
$current
循环变量,上升到父母,然后获取所有后代节点.../
.query
.value
添加
select
t.n.value('@type','varchar(max)') as [client-agent@type],
t.n.query('.') OuterXml,
t.n.query('
let $current := .
for $i in //* [ descendant-or-self::node() [. is $current ] ]
return concat("/", local-name($i))
').value('text()[1]', 'nvarchar(max)') as [the-path-i-want-to-get]
from @Xml.nodes('//client-agent') as t(n);
.
position(.)
DB<>小提琴