在 xml sql 列上使用 xquery 过滤结果集
我无法弄清楚如何过滤此 xml 片段。我想获得 svcProvNbr
其中 userServiceCde = OASV
<dataExtract xmlns="http://dtd-sirvahub.sirva.com/xml/SCHEMA/ATS" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://dtd-sirvahub.sirva.com/xml/SCHEMA/ATS http://dtd-sirvahub.sirva.com/xml/SCHEMA/ATS/dataExtract.xsd">
<shipment>
<location lctnSeqNbr="001">
<lctnServiceProvider>
<userServiceCde>EASV</userServiceCde>
<svcProvNbr>1470-000</svcProvNbr>
<svcProvTypeCde>A</svcProvTypeCde>
</lctnServiceProvider>
<lctnServiceProvider>
<userServiceCde>OASV</userServiceCde>
<svcProvNbr>1470-000</svcProvNbr>
<svcProvTypeCde>A</svcProvTypeCde>
</lctnServiceProvider>
</location>
</shipment>
</dataExtract>
我尝试了几种不同的方法,但没有
xmlalliedxml.query
('declare namespace r = "http://dtd-sirvahub.sirva.com/xml/SCHEMA/ATS";
data(/r:dataExtract/r:shipment/r:location[@lctnSeqNbr=sql:variable("@vcSegmentNo")]/r:lctnServiceProvider.userServiceCde[.="OASV"]/r:svcProvNbr)')
运气
xmlalliedxml.query
('declare namespace r = "http://dtd-sirvahub.sirva.com/xml/SCHEMA/ATS";
for $sp in /r:dataExtract/r:shipment/r:location[@lctnSeqNbr=sql:variable("@vcSegmentNo")]/r:lctnServiceProvider/r:svcProvNbr
where $sp/r:dataExtract/r:shipment/r:location[@lctnSeqNbr=sql:variable("@vcSegmentNo")]/r:lctnServiceProvider/r:userServiceCde[.="OASV"]
return
$sp')
I'm having trouble figuring out how to filter this snippet of xml. I would like to get the svcProvNbr
where the userServiceCde = OASV
<dataExtract xmlns="http://dtd-sirvahub.sirva.com/xml/SCHEMA/ATS" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://dtd-sirvahub.sirva.com/xml/SCHEMA/ATS http://dtd-sirvahub.sirva.com/xml/SCHEMA/ATS/dataExtract.xsd">
<shipment>
<location lctnSeqNbr="001">
<lctnServiceProvider>
<userServiceCde>EASV</userServiceCde>
<svcProvNbr>1470-000</svcProvNbr>
<svcProvTypeCde>A</svcProvTypeCde>
</lctnServiceProvider>
<lctnServiceProvider>
<userServiceCde>OASV</userServiceCde>
<svcProvNbr>1470-000</svcProvNbr>
<svcProvTypeCde>A</svcProvTypeCde>
</lctnServiceProvider>
</location>
</shipment>
</dataExtract>
I have tried a couple different ways but no luck
xmlalliedxml.query
('declare namespace r = "http://dtd-sirvahub.sirva.com/xml/SCHEMA/ATS";
data(/r:dataExtract/r:shipment/r:location[@lctnSeqNbr=sql:variable("@vcSegmentNo")]/r:lctnServiceProvider.userServiceCde[.="OASV"]/r:svcProvNbr)')
and
xmlalliedxml.query
('declare namespace r = "http://dtd-sirvahub.sirva.com/xml/SCHEMA/ATS";
for $sp in /r:dataExtract/r:shipment/r:location[@lctnSeqNbr=sql:variable("@vcSegmentNo")]/r:lctnServiceProvider/r:svcProvNbr
where $sp/r:dataExtract/r:shipment/r:location[@lctnSeqNbr=sql:variable("@vcSegmentNo")]/r:lctnServiceProvider/r:userServiceCde[.="OASV"]
return
$sp')
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
试试这个:
我修改了您的 XML,以便可以将两个条目分开,并且此
SELECT
语句确实返回1470-001
作为值。更新:要从表中的列执行此操作,请使用以下代码:
Try this:
I modified your XML so I can keep the two entries apart, and this
SELECT
statement does indeed return1470-001
as the value.Update: to do this from a column in a table, use this code:
除了 marc_s 提供的之外,您可能还需要考虑使用 CROSS APPLY
TableColumn.nodes('/') as t(c) 位于 FROM 和 WHERE 之间(
TableColumn
是包含 XML 的列的名称,然后是括号内的路径)。In addition to what marc_s gave, you might want to consider using
CROSS APPLY
between FROM and WHERE (withTableColumn.nodes('/') as t(c)
TableColumn
being the name of column containing the XML and then the path inside the parenthesis).