Sql server 获取SQL XML值的XPath
这是我的问题:从列中的以下XML中,我想知道在给定步骤Id和组件Id的情况下,名为“Enabled”的变量的值是否等于“Yes”Sql server 获取SQL XML值的XPath,sql-server,tsql,xpath,Sql Server,Tsql,Xpath,这是我的问题:从列中的以下XML中,我想知道在给定步骤Id和组件Id的情况下,名为“Enabled”的变量的值是否等于“Yes” '<xml> <box stepId="1"> <components> <component id="2"> <variables> <variable id="3" nom="Server" valeur="DEV1" />
'<xml>
<box stepId="1">
<components>
<component id="2">
<variables>
<variable id="3" nom="Server" valeur="DEV1" />
<variable id="4" nom="Enabled" valeur="Yes" />
</variables>
</component>
<component id="3">
<variables>
<variable id="3" nom="Server" valeur="DEV1" />
<variable id="4" nom="Enabled" valeur="No" />
</variables>
</component>
</components>
</box>
<box stepId="2">
<components>
<component id="2">
<variables>
<variable id="3" nom="Server" valeur="DEV2" />
<variable id="4" nom="Enabled" valeur="Yes" />
</variables>
</component>
<component id="3">
<variables>
<variable id="3" nom="Server" valeur="DEV2" />
<variable id="4" nom="Enabled" valeur="No" />
</variables>
</component>
</components>
</box>
</xml>'
'
'
- 有什么事吗
select x.value(N'../../../../@stepId', N'int') as StepID
, x.value(N'../../@id', N'int') as ComponentID
, x.value(N'@nom',N'nvarchar(100)') as Nom
, x.value(N'@valeur', N'nvarchar(100)') as Valeur
from @x.nodes(N'/xml/box/components/component/variables/variable') t(x)
select x.value(N'@valeur', N'nvarchar(100)') as Valeur
from @x.nodes(N'/xml/box[@stepId=sql:variable("@stepID")]/
components/component[@id = sql:variable("@componentID")]/
variables/variable[@nom="Enabled"]') t(x)
但是,如果必须使用准确检索感兴趣值的XPath:
select x.value(N'../../../../@stepId', N'int') as StepID
, x.value(N'../../@id', N'int') as ComponentID
, x.value(N'@nom',N'nvarchar(100)') as Nom
, x.value(N'@valeur', N'nvarchar(100)') as Valeur
from @x.nodes(N'/xml/box/components/component/variables/variable') t(x)
select x.value(N'@valeur', N'nvarchar(100)') as Valeur
from @x.nodes(N'/xml/box[@stepId=sql:variable("@stepID")]/
components/component[@id = sql:variable("@componentID")]/
variables/variable[@nom="Enabled"]') t(x)
如果stepID和component ID是列,而不是变量,那么在XPath过滤器中应该使用sql:column()而不是sql:variable。看
最后,如果您只需要检查是否存在,您可以使用XML方法:
select @x.exist(
N'/xml/box[@stepId=sql:variable("@stepID")]/
components/component[@id = sql:variable("@componentID")]/
variables/variable[@nom="Enabled" and @valeur="Yes"]')
我总是回到本文来了解如何在SQLServer2005中使用XML特性
对于基本的XPath诀窍,我推荐。我认为您想要的XPath查询是这样的:
/xml/box[@stepId="$stepId"]/components/component[@id="$componentId"]/variables/variable[@nom="Enabled" and @valeur="Yes"]
对于指定的$stepId和$componentId,这将为您获取名为“Enabled”且值为“Yes”的变量。这是假设xml以您显示的标记开头,而不是
如果SQLServer2005XPath非常简单(我从未使用过),那么上面的查询应该可以工作。否则,可能需要其他人来帮助您。谢谢,这很有帮助。如果找到匹配项或未找到匹配项,如何返回true或false?或者返回0或1。我不确定。您可以尝试以下操作:如果count()>=1选择1,否则选择0您可以使用EXIST方法。Field.EXIST('xpath')什么是
@x
?(将注释填充到15个字符)我怀疑他有DECLARE@xxml=(问题中的XML)
在SQL Server查询窗口中的语句上方,以便使用该XML进行测试,但将其从其答案中删除,因为这是多余的。总之,我怀疑这是一个包含问题中XML的变量。这两个链接都已失效。我建议查看语言参考:Links is dead.@user482745:第一个链接工作正常-更新了第二个链接,也工作正常