XML查询可在嵌套路径中返回多个值

时间:2019-03-25 04:37:07

标签: sql-server xml tsql xquery

我有一个大型XML文件,其中包含人口统计详细信息和超过100,000人的资格。

我可以成功查询数据,但是对于具有多个资格的人,使用此语句只能返回1个资格

;WITH XMLNAMESPACES('http://xxx/pie/svc/frs/FindRegistration/2.0.0' AS rt, 
                    'http://xxx/pie/xsd/frs/FindRegistrationMessages/2.0.0' AS msg,
                    'http://xxx/pie/xsd/frs/PractitionerRegistrationElements/2.0.0' AS el)
    SELECT
        --@ExtractFileName as Extract_File_Name,
        ProfessionNumber = XC.value('(../@ProfessionNumber)', 'varchar(50)'),
        QualificationEditDate = XC.value('(el:Qualification/@QualificationEditDate)[1]', 'varchar(50)'),
        QualificationTitle = XC.value('(el:Qualification/el:QualificationTitle)[1]', 'varchar(200)'),
        AwardingInstitution = XC.value('(el:Qualification/el:AwardingInstitution)[1]', 'varchar(200)'),
        CountryQualificationObtained = XC.value('(el:Qualification/el:CountryQualificationObtained)[1]', 'varchar(50)'),
        YearOfQualification = XC.value('(el:Qualification/el:YearOfQualification)[1]', 'varchar(50)')

    FROM
        AHPRA_XML as t
        CROSS APPLY
            t.XMLData.nodes('/rt:FindRegistrationsResponse/msg:ProfessionNumberReplay/msg:Practitioner') XT(XC)

具有多个资格的人的xml结构类似于以下示例:

<ProfessionNumberReplay xmlns="http://xxx/pie/xsd/frs/FindRegistrationMessages/2.0.0" ProfessionNumber="MED0000xxx">
   <Practitioner>
      <PractitionerIdentifier xmlns="http://xxx/pie/xsd/frs/PractitionerRegistrationElements/2.0.0">6aec2xxxx8096b</PractitionerIdentifier>
      <PractitionerName xmlns="http://xxx/pie/xsd/frs/PractitionerRegistrationElements/2.0.0" NameEditDate="2010-07-03T14:14:35.377">
         <NameTitle>Dr</NameTitle>
         <FamilyName>xxxx</FamilyName>
         <GivenName>xxxx</GivenName>
         <MiddleName />
      </PractitionerName>
      <Demographics xmlns="http://xxx/pie/xsd/frs/PractitionerRegistrationElements/2.0.0">
         <Gender>M</Gender>
      </Demographics>
      <Qualification xmlns="http://xxx/pie/xsd/frs/PractitionerRegistrationElements/2.0.0" QualificationEditDate="2016-07-27T18:36:35.46">
         <QualificationTitle>Name of Qualification 1</QualificationTitle>
         <AwardingInstitution>Name of University</AwardingInstitution>
         <CountryQualificationObtained>United Kingdom</CountryQualificationObtained>
         <YearOfQualification>1966</YearOfQualification>
      </Qualification>
      <Qualification xmlns="http://xxx/pie/xsd/frs/PractitionerRegistrationElements/2.0.0" QualificationEditDate="2016-07-25T17:49:36.23">
         <QualificationTitle>Name of Qualification 2</QualificationTitle>
         <AwardingInstitution>Name of University</AwardingInstitution>
         <CountryQualificationObtained>Sri Lanka</CountryQualificationObtained>
         <YearOfQualification>1960</YearOfQualification>
      </Qualification>
      <Qualification xmlns="http://xxx/pie/xsd/frs/PractitionerRegistrationElements/2.0.0" QualificationEditDate="2016-08-10T19:37:23.517">
         <QualificationTitle>Name of Qualification 3</QualificationTitle>
         <AwardingInstitution>Name of University</AwardingInstitution>
         <CountryQualificationObtained>Sri Lanka</CountryQualificationObtained>
         <YearOfQualification>1959</YearOfQualification>
      </Qualification>
      <Qualification xmlns="http://xxx/pie/xsd/frs/PractitionerRegistrationElements/2.0.0" QualificationEditDate="2016-07-26T18:33:08.79">
         <QualificationTitle>Name of Qualification 4</QualificationTitle>
         <AwardingInstitution>Name of University</AwardingInstitution>
         <CountryQualificationObtained>United Kingdom</CountryQualificationObtained>
         <YearOfQualification>1959</YearOfQualification>
      </Qualification>
      <Address xmlns="http://xxx/pie/xsd/frs/PractitionerRegistrationElements/2.0.0" AddressEditDate="2017-01-10T16:55:49.34">
         <AustralianAddress>
            <AustralianLocality>Suburb Name</AustralianLocality>
            <AustralianPostcode>1000</AustralianPostcode>
            <AustralianState>QLD</AustralianState>
            <Country>Australia</Country>
         </AustralianAddress>
      </Address>
      <Profession xmlns="http://xxx/pie/xsd/frs/PractitionerRegistrationElements/2.0.0">
         <ProfessionNumber>MED0000xxx</ProfessionNumber>
         <Profession>Medical Practitioner</Profession>
         <ProfessionStartDate>1972-12-06T00:00:00</ProfessionStartDate>
         <Registration>
            <RecordNumber>001</RecordNumber>
            <RegistrationType>General</RegistrationType>
            <RegistrationStatus>Unregistered</RegistrationStatus>
            <RegistrationSubStatus>Not Renewed</RegistrationSubStatus>
            <RegistrationToDate>2017-09-30T00:00:00</RegistrationToDate>
            <InitialRegistrationDate>1972-12-06T00:00:00</InitialRegistrationDate>
         </Registration>
      </Profession>
   </Practitioner>
</ProfessionNumberReplay>

我尝试添加“。”但是当我从嵌套XML中的其他路径返回数据时,我似乎无法正常工作。

2 个答案:

答案 0 :(得分:0)

将片段插入表后:

declare @t table (XMLData xml not null);

,下面的代码可以满足您的需求:

with xmlnamespaces(
  'http://xxx/pie/xsd/frs/FindRegistrationMessages/2.0.0' AS msg,
  'http://xxx/pie/xsd/frs/PractitionerRegistrationElements/2.0.0' AS el
)
SELECT
  ProfessionNumber = pnr.c.value('./@ProfessionNumber', 'varchar(50)'),
  QualificationEditDate = XC.value('./@QualificationEditDate', 'datetime2(0)'),
  QualificationTitle = XC.value('(./el:QualificationTitle/text())[1]', 'varchar(200)'),
  AwardingInstitution = XC.value('(./el:AwardingInstitution/text())[1]', 'varchar(200)'),
  CountryQualificationObtained = XC.value('(./el:CountryQualificationObtained/text())[1]', 'varchar(50)'),
  YearOfQualification = XC.value('(./el:YearOfQualification/text())[1]', 'varchar(50)')
FROM @t t
  cross apply t.XMLData.nodes('/msg:ProfessionNumberReplay') pnr(c)
  CROSS APPLY pnr.c.nodes('./msg:Practitioner/el:Qualification') XT(XC);

问题在于您没有进入/el:Qualification XML方法中的nodes()层次结构级别,而仅转到其父级。因为这里只有一个/msg:Practitioner节点,所以结果集只包含一行。我将apply移到必要的级别后,所有行都出现了,而不仅仅是第一行。

答案 1 :(得分:0)

感谢@RogerWolf的示例语句。当我运行它时,它没有返回任何数据,但是您的评论强调了我在层次结构中还不够深入的事实。为了解决这个问题,我只是将CROSS APPLY(最后一行)更改为“资格”级别,然后在我想返回的每一列上都增加了一个额外级别-参见下面的代码:

;WITH XMLNAMESPACES('http://xxx/pie/svc/frs/FindRegistration/2.0.0' AS rt, 
                    '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)'),
        QualificationEditDate = XC.value('(@QualificationEditDate)[1]', 'varchar(50)'),
        QualificationTitle = XC.value('(el:QualificationTitle)[1]', 'varchar(200)'),
        AwardingInstitution = XC.value('(el:AwardingInstitution)[1]', 'varchar(200)'),
        CountryQualificationObtained = XC.value('(el:CountryQualificationObtained)[1]', 'varchar(50)'),
        YearOfQualification = XC.value('(el:YearOfQualification)[1]', 'varchar(50)')
    FROM
        AHPRA_XML as t
              CROSS APPLY
                     t.XMLData.nodes('/rt:FindRegistrationsResponse/msg:ProfessionNumberReplay/msg:Practitioner/el:Qualification') XT(XC)

我不确定这是否比您提供的代码有效/有效,但这对我来说还是很有意义的。

再次感谢