我有一个类似于以下功能,返回大块xml(在生产环境中), 在应用程序的各个阶段,我们只需要从中检索一些元素或仅检索一些属性。
create function testrig.fxConfigurations(@context varchar(300)) returns xml as
begin
return (select
'<configurations realm="configuration">
<context name="apera">
<backends>
<backend name="Hades">
<os>HP Unix</os>
<ip>nnn.nnn.nnn</ip>
<db vender="Oracle" version="11g">
<netconnect>Data Source= (DESCRIPTION =(ADDRESS = (PROTOCOL = TCP)(HOST = hades)(PORT = 1521)(RECV_BUF_SIZE=1048576))(CONNECT_DATA =(SERVER = DEDICATED)(SERVICE_NAME = baan)));Password=********;User ID=ItsMe;" providerName="Oracle.DataAccess.Client"
</netconnect>
</db>
</backend>
<backend name="Athena">
<os>HP Unix</os>
<ip>nnn.nnn.nnn</ip>
<db vender="Oracle" version="11g">
<netconnect>Data Source= (DESCRIPTION =(ADDRESS = (PROTOCOL = TCP)(HOST = hades)(PORT = 1521)(RECV_BUF_SIZE=1048576))(CONNECT_DATA =(SERVER = DEDICATED)(SERVICE_NAME = baan)));Password=********;User ID=ItsMe;" providerName="Oracle.DataAccess.Client"
</netconnect>
</db>
</backend>
</backends>
</context>
</configurations>
')
end
go
如何仅检索属性名称,即“Hades&#39;”,“Athena&#39;像这样使用xQuery
select (testrig.fxConfigurations(null).query('configurations/context[@name="apera"]/backends').query('/descendant-or-self::*'))
答案 0 :(得分:1)
如果要连接backend/@name
属性的所有值(ala GROUP_CONCAT
):
select testrig.fxConfigurations(null)
.query('data(configurations/context[@name="apera"]/backends/backend/@name)')
如果您需要单独使用这些属性,则可能需要使用nodes
来提取它们:
WITH cteXml AS
(
SELECT testrig.fxConfigurations(null)
.query('configurations/context[@name="apera"]') as context
)
SELECT Nodes.node.value('@name', 'varchar(50)') AS backEndName
FROM cteXml
CROSS APPLY cteXml.context.nodes('//backends/backend') as Nodes(node);