努力从 XML 数据中提取值 (T-SQL)
Posted
技术标签:
【中文标题】努力从 XML 数据中提取值 (T-SQL)【英文标题】:Struggling with extracting values from XML data (T-SQL) 【发布时间】:2019-12-31 18:36:30 【问题描述】:这是来自文件底部的我的 XML 数据的 sn-p,显示了结束标签(这是一个巨大的文件):
<EncounterValidationResponse xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.dhcs.ca.gov/EDS/DHCSResponse" ResponseVersion="1.2">
<EncounterFileName>EncounterFileName</EncounterFileName>
<EncounterSubmitterName>EncounterSubmitter</EncounterSubmitterName>
<EncounterSubmissionDate>2019-12-13T08:47:00.0000000-08:00</EncounterSubmissionDate>
<ValidationStatus>Accepted</ValidationStatus>
<Transactions>
<Transaction>
<IdentifierName>BatchNumber</IdentifierName>
<TransactionNumber>1277104</TransactionNumber>
<Identifiers>
<Envelope IdentifierName="OriginatorTransactionId" IdentifierValue="1277104-00" />
</Identifiers>
<Encounters>
<Encounter Status="Accepted">
<IdentifierType>TransactionIdentifier</IdentifierType>
<EncounterReferenceNumber>8129339999264</EncounterReferenceNumber>
<EncounterId>1934049991539</EncounterId>
<EncounterResponses>
<Response Severity="Warning">
<Id>0x0006D</Id>
<Description>MEDS indicates multiple records exist for this beneficiary</Description>
</Response>
<Response Severity="Warning">
<Id>0x00221</Id>
<Description>Prescriber Taxonomy (296) is either not provided or is invalid</Description>
</Response>
<Response Severity="Warning">
<Id>0x00249</Id>
<Description>Reason for Service Code (439-E4) is requested per the payer sheet but no value is provided/specified.</Description>
</Response>
<Response Severity="Warning">
<Id>0x00249</Id>
<Description>Professional Service Code (440-E5) is requested per the payer sheet but no value is provided/specified.</Description>
</Response>
<Response Severity="Warning">
<Id>0x00249</Id>
<Description>Result of Service Code (441-E6) is requested per the payer sheet but no value is provided/specified.</Description>
</Response>
</EncounterResponses>
</Encounter>
</Encounters>
</Transaction>
</Transactions>
</EncounterValidationResponse>
我正在尝试提取所有 Encounter Status 值、EncounterReferenceNumbers、Response Severity 值、Id 和 Description 值。我希望查询返回五列,即遭遇状态、遭遇参考编号、严重性、ID 和描述。
我已经在互联网上搜索了几天并尝试了几件事,但似乎无法得到它。
感谢任何帮助!
【问题讨论】:
这不是一个有效的 XML。更正它以包含缺少的开始标签。 现在好点了吗?这个文件很大,所以我不能发布整个文件。 是的,现在很清楚了。命名空间是 XML 数据的重要组成部分,因此您无法在不知道此信息的情况下创建正确的查询。请参阅答案。 【参考方案1】:类似这样的演示:
declare @x XML='<EncounterValidationResponse xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.dhcs.ca.gov/EDS/DHCSResponse" ResponseVersion="1.2">
<EncounterFileName>EncounterFileName</EncounterFileName>
<EncounterSubmitterName>EncounterSubmitter</EncounterSubmitterName>
<EncounterSubmissionDate>2019-12-13T08:47:00.0000000-08:00</EncounterSubmissionDate>
<ValidationStatus>Accepted</ValidationStatus>
<Transactions>
<Transaction>
<IdentifierName>BatchNumber</IdentifierName>
<TransactionNumber>1277104</TransactionNumber>
<Identifiers>
<Envelope IdentifierName="OriginatorTransactionId" IdentifierValue="1277104-00" />
</Identifiers>
<Encounters>
<Encounter Status="Accepted">
<IdentifierType>TransactionIdentifier</IdentifierType>
<EncounterReferenceNumber>8129339999264</EncounterReferenceNumber>
<EncounterId>1934049991539</EncounterId>
<EncounterResponses>
<Response Severity="Warning">
<Id>0x0006D</Id>
<Description>MEDS indicates multiple records exist for this beneficiary</Description>
</Response>
<Response Severity="Warning">
<Id>0x00221</Id>
<Description>Prescriber Taxonomy (296) is either not provided or is invalid</Description>
</Response>
<Response Severity="Warning">
<Id>0x00249</Id>
<Description>Reason for Service Code (439-E4) is requested per the payer sheet but no value is provided/specified.</Description>
</Response>
<Response Severity="Warning">
<Id>0x00249</Id>
<Description>Professional Service Code (440-E5) is requested per the payer sheet but no value is provided/specified.</Description>
</Response>
<Response Severity="Warning">
<Id>0x00249</Id>
<Description>Result of Service Code (441-E6) is requested per the payer sheet but no value is provided/specified.</Description>
</Response>
</EncounterResponses>
</Encounter>
</Encounters>
</Transaction>
</Transactions>
</EncounterValidationResponse>';
with xmlnamespaces (DEFAULT 'http://www.dhcs.ca.gov/EDS/DHCSResponse')
select t.n.value('@Status','nvarchar(100)') status,
t.n.value('EncounterReferenceNumber[1]','nvarchar(100)') EncounterReferenceNumber,
r.n.value('@Severity','nvarchar(100)') Severity,
r.n.value('Id[1]','nvarchar(100)') Id,
r.n.value('Description[1]','nvarchar(100)') Description
from @x.nodes('/EncounterValidationResponse/Transactions/Transaction/Encounters/Encounter') t(n)
cross apply t.n.nodes('EncounterResponses/Response') r(n);
【讨论】:
哇!!完美的!!这正是我想要的。我希望我早点寻求帮助;但是,一路走来,我确实学到了很多东西。 Serg,我非常感谢您快速准确的回复。谢谢,谢谢,谢谢。以上是关于努力从 XML 数据中提取值 (T-SQL)的主要内容,如果未能解决你的问题,请参考以下文章
努力从 BigQuery SQL 中的时间戳字段中提取特定月份的 DATE