T-SQL无法查询具有多个嵌套名称空间且没有唯一ID的XML

时间:2019-02-18 01:50:31

标签: sql-server xml tsql xml-namespaces

我需要从一个包含4个名称空间的xml文件中创建一个查询,但是它们都没有唯一的ID。例如。 xmlns:fb

我在下面列出了它们:

xmlns="http://xxx/pie/svc/frs/FindRegistration/2.0.0"
xmlns="http://xxx/pie/xsd/frs/FindRegistrationMessages/2.0.0"
xmlns="http://xxx/pie/xsd/common/CommonCoreElements/2.0.0"
xmlns="http://xxx/pie/xsd/frs/PractitionerRegistrationElements/2.0.0"

如果我从文档中剥离了​​所有名称空间(通过Visual Studio),则可以很好地检索数据,但是我需要保持名称空间完整。

<FindRegistrationsResponse xmlns="http://xxx/pie/svc/frs/FindRegistration/2.0.0">
    <AuditDetails xmlns="http://xxx/pie/xsd/frs/FindRegistrationMessages/2.0.0">
        <QueryIdentifier xmlns="http://xxx/pie/xsd/common/CommonCoreElements/2.0.0">8901823</QueryIdentifier>
        <PractitionerCount xmlns="http://xxx/pie/xsd/common/CommonCoreElements/2.0.0">134140</PractitionerCount>
        <ServiceMessagesCount xmlns="http://xxx/pie/xsd/common/CommonCoreElements/2.0.0">2</ServiceMessagesCount>
        <TotalRecordsSearched xmlns="http://xxx/pie/xsd/common/CommonCoreElements/2.0.0">134142</TotalRecordsSearched>
        <TotalNumberOfRegistration xmlns="http://xxx/pie/xsd/common/CommonCoreElements/2.0.0">257034</TotalNumberOfRegistration>
        <SnapshotDate xmlns="http://xxx/pie/xsd/common/CommonCoreElements/2.0.0">2018-06-29</SnapshotDate>
    </AuditDetails>
    <ProfessionNumberReplay ProfessionNumber="MED0000xxxxx" xmlns="http://xxx/pie/xsd/frs/FindRegistrationMessages/2.0.0">
        <Practitioner>
            <PractitionerIdentifier xmlns="http://xxx/pie/xsd/frs/PractitionerRegistrationElements/2.0.0">xxxxxxxx</PractitionerIdentifier>
            <PractitionerName NameEditDate="2010-07-03T14:14:35.377" xmlns="http://xxx/pie/xsd/frs/PractitionerRegistrationElements/2.0.0">
                <NameTitle>Dr</NameTitle>
                <FamilyName>Smith</FamilyName>
                <GivenName>John</GivenName>
                <MiddleName />
            </PractitionerName>
            <Demographics xmlns="http://xxx/pie/xsd/frs/PractitionerRegistrationElements/2.0.0">
                <Gender>GenderName</Gender>
            </Demographics>
            <Qualification QualificationEditDate="2016-08-10T19:35:54.067" xmlns="http://xxx/pie/xsd/frs/PractitionerRegistrationElements/2.0.0">
                <QualificationTitle>Bachelor of Medicine / Bachelor of Surgery </QualificationTitle>
                <AwardingInstitution>University of xxx</AwardingInstitution>
                <CountryQualificationObtained>Country Name</CountryQualificationObtained>
                <YearOfQualification>1999</YearOfQualification>
            </Qualification>
            <Address AddressEditDate="2010-07-03T14:15:48.607" xmlns="http://xxx/pie/xsd/frs/PractitionerRegistrationElements/2.0.0">
                <AustralianAddress>
                    <AustralianLocality>SUBURB NAME</AustralianLocality>
                    <AustralianPostcode>2000</AustralianPostcode>
                    <AustralianState>NSW</AustralianState>
                    <Country>Australia</Country>
                </AustralianAddress>
            </Address>
            <Profession xmlns="http://xxx/pie/xsd/frs/PractitionerRegistrationElements/2.0.0">
                <ProfessionNumber>MED0000xxxxx</ProfessionNumber>
                <Profession>Medical Practitioner</Profession>
                <ProfessionStartDate>1999-05-10T00:00:00</ProfessionStartDate>
                <Registration>
                    <RecordNumber>001</RecordNumber>
                    <RegistrationType>Limited (Public Interest - Occasional Practice)</RegistrationType>
                    <RegistrationStatus>Unregistered</RegistrationStatus>
                    <RegistrationSubStatus>Withdrawn</RegistrationSubStatus>
                    <RegistrationToDate>2013-09-30T00:00:00</RegistrationToDate>
                    <InitialRegistrationDate>1965-05-10T00:00:00</InitialRegistrationDate>
                </Registration>
                <Condition ConditionEditDate="2012-10-22T14:00:10.5">
                    <ConditionType>Registration</ConditionType>
                    <ConditionDetail>text here</ConditionDetail>
                </Condition>
            </Profession>
        </Practitioner>
    </ProfessionNumberReplay>
</FindRegistrationsResponse>

这是我尝试过的T-SQL代码:

DECLARE @XML AS XML, @hDoc AS INT, @SQL NVARCHAR (MAX)

SELECT @XML = XMLData FROM AHPRA_XML

EXEC sp_xml_preparedocument @hDoc OUTPUT, @XML


;WITH XMLNAMESPACES('http://xxx/pie/svc/frs/FindRegistration/2.0.0',
                    'http://xxx/pie/xsd/frs/FindRegistrationMessages/2.0.0',
                    'http://xxx/pie/xsd/common/CommonCoreElements/2.0.0',
                    'http://xxx/pie/xsd/frs/PractitionerRegistrationElements/2.0.0')

SELECT DISTINCT  ProfessionNumber, NameTitle, GivenName, MiddleName, FamilyName, Gender

FROM OPENXML(@hDoc, 'FindRegistrationsResponse/ProfessionNumberReplay/Practitioner')
WITH 
(
ProfessionNumber [varchar](50) '../@ProfessionNumber',
NameTitle [varchar](20) 'PractitionerName/NameTitle',
GivenName  [varchar](100) 'PractitionerName/GivenName',
MiddleName  [varchar](100) 'PractitionerName/MiddleName',
FamilyName [varchar](100) 'PractitionerName/FamilyName',
Gender [varchar](10) 'Demographics/Gender'
)

EXEC sp_xml_removedocument @hDoc
GO

1 个答案:

答案 0 :(得分:2)

您已经定义了名称空间-但您从未在XPath中使用!另外,我强烈建议在内置的OPENXML方法之上使用内置的XQuery功能。

尝试以下选择语句:

;WITH XMLNAMESPACES('http://xxx/pie/svc/frs/FindRegistration/2.0.0' AS root, 
                    'http://xxx/pie/xsd/frs/FindRegistrationMessages/2.0.0' AS msg,
                    'http://xxx/pie/xsd/frs/PractitionerRegistrationElements/2.0.0' AS el)
    SELECT
        ProfessionNumber = XC.value('(../@ProfessionNumber)', 'varchar(50)'),
        NameTitle = XC.value('(el:PractitionerName/el:NameTitle)[1]', 'varchar(50)'),
        GivenName = XC.value('(el:PractitionerName/el:GivenName)[1]', 'varchar(50)'),
        MiddleName = XC.value('(el:PractitionerName/el:MiddleName)[1]', 'varchar(50)'),
        FamilyName = XC.value('(el:PractitionerName/el:FamilyName)[1]', 'varchar(50)'),
        Gender = XC.value('(el:Demographics/el:Gender)[1]', 'varchar(50)')
    FROM
        @XML.nodes('/root:FindRegistrationsResponse/msg:ProfessionNumberReplay/msg:Practitioner') AS XT(XC)

这应该从XML字符串中获取所需的数据。

更新

当您在问题中提供的XML以及此处的代码时,您确实会得到以下输出:

enter image description here

因此,您可能不是真的在使用提供的示例XML,或者您正在使用其他代码……