SQL Server 2008 R2 SP2函数用于提取xml数据

时间:2014-06-20 00:55:36

标签: sql xml xpath sql-server-2008-r2

我目前有一个功能可以从CCD中提取患者编号,但CCD包含MRN, EMPI, SSN和患者编号。我需要它来提取患者编号而不是EMPI。然后我将使用它来利用RETURN创建一个列。

请帮助!

这是当前代码:

CREATE FUNCTION [dbo].[rs_ExtractPatientIdentifier] 
    (@xmlData xml, @messageType nvarchar(50))
RETURNS 
    nvarchar(128)
WITH SCHEMABINDING
BEGIN
    DECLARE @patientIdentifier nvarchar(128)

    ;WITH XMLNAMESPACES(DEFAULT 'http://domain.com/product/Schemas', 'urn:hl7-org:v3' as hl7ns)
    SELECT 
       @patientIdentifier = CASE @messageType
                               WHEN 'CCD' THEN @xmlData.value('(hl7ns:ClinicalDocument/hl7ns:recordTarget/hl7ns:patientRole/hl7ns:id/@extension)[1]', 'varchar(128)')
                               WHEN 'PatientAdverseReactionRequest' THEN @xmlData.value('(PatientAdverseReactionRequest/Patient/Identifiers/Identifier/Id)[1]', 'varchar(128)')
                               WHEN 'UnsolicitedDocumentType' THEN @xmlData.value('(DocumentManagementRequest/Patient/Identifiers/Identifier/Id)[1]', 'varchar(128)')
                               WHEN 'UnsolicitedLabResult' THEN @xmlData.value('(UnsolicitedLabResult/Patient/Identifiers/Identifier/Id)[1]', 'varchar(128)')
                               ELSE ''
                             END

    RETURN @patientIdentifier
END

以下是具有患者ID的XML,我需要将其作为本地定义的患者ID:

<ProcessCcdRequest xmlns="http://domain.com/product/Schemas">
<Ccd>&lt;ClinicalDocument xmlns="urn:hl7-org:v3" xmlns:sdtc="urn:hl7-org:sdtc" xmlns:voc="urn:hl7-org:v3/voc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:hl7-org:v3 CDA.xsd"&gt;&lt;realmCode code="US"/&gt;&lt;typeId extension="POCD_HD000040" root="2.16.840.1.113883.1.3"/&gt;&lt;templateId root="2.16.840.1.113883.10.20.22.1.1"/&gt;&lt;templateId root="2.16.840.1.113883.10.20.22.1.2"/&gt;&lt;id root="2.25.126274596634328418155136651226877799755"/&gt;&lt;code code="34133-9" codeSystem="2.16.840.1.113883.6.1" codeSystemName="LOINC" displayName="Summarization of episode note"/&gt;&lt;title&gt;Clinical Document Architecture&lt;/title&gt;&lt;effectiveTime value="20140619191531-0400"/&gt;&lt;confidentialityCode code="N" codeSystem="2.16.840.1.113883.5.25"/&gt;&lt;languageCode code="en-US"/&gt;&lt;recordTarget&gt;&lt;patientRole&gt;&lt;id assigningAuthorityName="Enterprise MPI Number" extension="000000001" root="1.3.6.1.4.1.26923.99.1.1.1.2.1"/&gt;&lt;id assigningAuthorityName="Enterprise MPI Number" extension="000000001" root="1.3.6.1.4.1.26923.99.1.1.1.2.1"/&gt;&lt;id assigningAuthorityName="SSN" extension="999999999" root="2.16.840.1.113883.4.1"/&gt;&lt;id assigningAuthorityName="Hospital: Locally defined Patient Account Number" extension="000222222222" root="1.3.6.1.4.1.26923.99.1.1.44273.7.3"/&gt;&lt;addr use="HP"&gt;&lt;streetAddressLine&gt;111111 Sw 53Rd St&lt;/streetAddressLine&gt;&lt;city&gt;AnyTown&lt;/city&gt;&lt;state&gt;FL&lt;/state&gt;&lt;postalCode&gt;33175&lt;/postalCode&gt;&lt;country&gt;United States of America&lt;/country&gt;&lt;/addr&gt;&lt;telecom use="HP" value="tel:111-222-5555"/&gt;&lt;patient&gt;&lt;name use="L"&gt;&lt;given&gt;Names&lt;/given&gt;&lt;family&gt;NamesNames&lt;/family&gt;&lt;/name&gt;&lt;administrativeGenderCode code="M" codeSystem="2.16.840.1.113883.5.1" codeSystemName="HL7 AdministrativeGenderCodes" displayName="Male"/&gt;&lt;birthTime value="19500326"/&gt;&lt;maritalStatusCode code="M" codeSystem="2.16.840.1.113883.5.2" codeSystemName="MaritalStatusCode" displayName="Married"/&gt;&lt;raceCode code="2106-3" codeSystem="2.16.840.1.113883.6.238" codeSystemName="Race &amp;amp; Ethnicity - CDC" displayName="White Hispanic"/&gt;&lt;sdtc:raceCode codeSystem="2.16.840.1.113883.6.238" codeSystemName="Race &amp;amp; Ethnicity - CDC" nullFlavor="NI"/&gt;&lt;sdtc:raceCode codeSystem="2.16.840.1.113883.6.238" codeSystemName="Race &amp;amp; Ethnicity - CDC" nullFlavor="NI"/&gt;&lt;ethnicGroupCode code="2135-2" codeSystem="2.16.840.1.113883.6.238" codeSystemName="Race &amp;amp; Ethnicity - CDC" displayName="Hispanic"/&gt;&lt;languageCommunication&gt;&lt;languageCode code="spa"/&gt;&lt;preferenceInd value="true"/&gt;&lt;/languageCommunication&gt;&lt;/patient&gt;&lt;providerOrganization&gt;&lt;id assigningAuthorityName="Hospital" extension="Entity: EDA" root="2.16.840.1.114060.6.3.1.4.1"/&gt;&lt;name&gt;Hospital&lt;/name&gt;&lt;telecom use="WP" value="tel:111-111-2222"/&gt;&lt;addr use="WP"&gt;&lt;streetAddressLine&gt;11111 N 1st Avenue&lt;/streetAddressLine&gt;&lt;city&gt;AnyTown&lt;/city&gt;&lt;state&gt;FL&lt;/state&gt;&lt;postalCode&gt;12345&lt;/postalCode&gt;&lt;country&gt;United States of America&lt;/country&gt;&lt;/addr&gt;&lt;/providerOrganization&gt;&lt;/patientRole&gt;&lt;/recordTarget&gt;&lt;author&gt;&lt;time value="20140619191531-0400"/&gt;&lt;assignedAuthor&gt;&lt;id assigningAuthorityName="NPI" extension="1234567891" root="2.16.840.1.113883.4.6"/&gt;&lt;addr&gt;&lt;streetAddressLine nullFlavor="NI"/&gt;&lt;city nullFlavor="NI"/&gt;&lt;state nullFlavor="NI"/&gt;&lt;postalCode nullFlavor="NI"/&gt;&lt;country nullFlavor="NI"/&gt;&lt;/addr&gt;&lt;telecom value="tel:111-222-4444 ext:"/&gt;&lt;assignedPerson&gt;&lt;name&gt;&lt;given&gt;OthLastNamei&lt;/given&gt;&lt;family&gt;LastName-Man&lt;/family&gt;&lt;/name&gt;&lt;/assignedPerson&gt;&lt;representedOrganization&gt;&lt;id extension="W0CL" root="2.16.840.1.114060.6.3.1.4.1"/&gt;&lt;name&gt; Hospital &lt;/name&gt;&lt;telecom value="tel:111-111-2222"/&gt;&lt;addr&gt;&lt;streetAddressLine&gt;11111 N 1st Avenue&lt;/streetAddressLine&gt;&lt;city&gt;AnyTown&lt;/city&gt;&lt;state&gt;FL&lt;/state&gt;&lt;postalCode&gt;12345&lt;/postalCode&gt;&lt;country&gt;United States of America&lt;/country&gt;&lt;/addr&gt;&lt;/representedOrganization&gt;&lt;/assignedAuthor&gt;&lt;/author&gt;&lt;custodian&gt;&lt;assignedCustodian&gt;&lt;representedCustodianOrganization&gt;&lt;!-- OID is OID for Custodian hospital name --&gt;&lt;id extension="W0CL" root="2.16.840.1.114060.6.3.1.4.1"/&gt;&lt;name&gt;Hospital&lt;/name&gt;&lt;telecom value="tel:111-111-2222"/&gt;&lt;addr&gt;&lt;streetAddressLine&gt;11111 N 1st Avenue&lt;/streetAddressLine&gt;&lt;city&gt;AnyTown&lt;/city&gt;&lt;state&gt;FL&lt;/state&gt;&lt;postalCode&gt;12345&lt;/postalCode&gt;&lt;country&gt;United States of America&lt;/country&gt;&lt;/addr&gt;&lt;/representedCustodianOrganization&gt;&lt;/assignedCustodian&gt;&lt;/custodian&gt;&lt;documentationOf&gt;&lt;serviceEvent classCode="PCPR"&gt;&lt;effectiveTime&gt;&lt;low value="20140614"/&gt;&lt;high value="20140619"/&gt;&lt;/effectiveTime&gt;&lt;/serviceEvent&gt;&lt;/documentationOf&gt;&lt;componentOf&gt;&lt;encompassingEncounter&gt;&lt;id extension="000000123456" root="2.16.840.1.113883.19"/&gt;&lt;effectiveTime&gt;&lt;low value="201406141743"/&gt;&lt;high value="201406191843"/&gt;&lt;/effectiveTime&gt;&lt;encounterParticipant typeCode="CON"&gt;&lt;assignedEntity&gt;&lt;id assigningAuthorityName="NPI" extension="1111111111" root="2.16.840.1.113883.4.6"/&gt;&lt;addr use="WP"&gt;&lt;streetAddressLine nullFlavor="NI"/&gt;&lt;city nullFlavor="NI"/&gt;&lt;state nullFlavor="NI"/&gt;&lt;postalCode nullFlavor="NI"/&gt;&lt;country nullFlavor="NI"/&gt;&lt;/addr&gt;&lt;telecom use="WP" value="tel:123-123-1234 ext:"&gt;  &lt;/telecom&gt;&lt;assignedPerson&gt;&lt;name&gt;&lt;given&gt;John J&lt;/given&gt;&lt;family&gt;Snow&lt;/family&gt;&lt;/name&gt;&lt;/assignedPerson&gt;&lt;/assignedEntity&gt;&lt;/encounterParticipant&gt;&lt;encounterParticipant typeCode="ADM"&gt;&lt;assignedEntity&gt;&lt;id assigningAuthorityName="NPI" extension="1234567891" root="2.16.840.1.113883.4.6"/&gt;&lt;addr use="WP"&gt;&lt;streetAddressLine nullFlavor="NI"/&gt;&lt;city nullFlavor="NI"/&gt;&lt;state nullFlavor="NI"/&gt;&lt;postalCode nullFlavor="NI"/&gt;&lt;country nullFlavor="NI"/&gt;&lt;/addr&gt;&lt;telecom use="WP" value="tel:111-222-4444 ext:"&gt;  &lt;/telecom&gt;&lt;assignedPerson&gt;&lt;name&gt;&lt;given&gt;OthLastNamei&lt;/given&gt;&lt;family&gt;LastName-Man&lt;/family&gt;&lt;/name&gt;&lt;/assignedPerson&gt;&lt;/assignedEntity&gt;&lt;/encounterParticipant&gt;&lt;encounterParticipant typeCode="ATND"&gt;&lt;assignedEntity&gt;&lt;id assigningAuthorityName="NPI" extension="1234567891" root="2.16.840.1.113883.4.6"/&gt;&lt;addr use="WP"&gt;&lt;streetAddressLine nullFlavor="NI"/&gt;&lt;city nullFlavor="NI"/&gt;&lt;state nullFlavor="NI"/&gt;&lt;postalCode nullFlavor="NI"/&gt;&lt;country nullFlavor="NI"/&gt;&lt;/addr&gt;&lt;telecom use="WP" value="tel:111-222-4444 ext:"&gt;  &lt;/telecom&gt;&lt;assignedPerson&gt;&lt;name&gt;&lt;given&gt;OthLastNamei&lt;/given&gt;&lt;family&gt;LastName-Man&lt;/family&gt;&lt;/name&gt;&lt;/assignedPerson&gt;&lt;/assignedEntity&gt;&lt;/encounterParticipant&gt;&lt;location&gt;&lt;healthCareFacility&gt;&lt;id assigningAuthorityName="Hospital" extension="hosp svc:EDA" root="2.16.840.1.114060.6.3.1.4.1"/&gt;&lt;location&gt;&lt;name&gt;Inpatient Via Ed-Eda&lt;/name&gt;&lt;addr use="WP"&gt;&lt;streetAddressLine&gt;11111 N 1st Avenue&lt;/streetAddressLine&gt;&lt;city&gt;AnyTown&lt;/city&gt;&lt;state&gt;FL&lt;/state&gt;&lt;postalCode&gt;12345&lt;/postalCode&gt;&lt;country&gt;United States of America&lt;/country&gt;&lt;/addr&gt;&lt;/location&gt;&lt;/healthCareFacility&gt;&lt;/location&gt;&lt;/encompassingEncounter&gt;&lt;/componentOf&gt;&lt;component&gt;&lt;structuredBody&gt;&lt;

为了便于阅读,<Ccd>元素的内容在未转义(解析)时如下所示:

<ClinicalDocument xmlns="urn:hl7-org:v3" xmlns:sdtc="urn:hl7-org:sdtc" xmlns:voc="urn:hl7-org:v3/voc" xmlns:xsi=" http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:hl7-org:v3 CDA.xsd">
  <realmCode code="US"/>
  <typeId extension="POCD_HD000040" root="2.16.840.1.113883.1.3"/>
  <templateId root="2.16.840.1.113883.10.20.22.1.1"/>
  <templateId root="2.16.840.1.113883.10.20.22.1.2"/>
  <id root="2.25.126274596634328418155136651226877799755"/>
  <code code="34133-9" codeSystem="2.16.840.1.113883.6.1" codeSystemName="LOINC" displayName="Summarization of episode note"/>
  <title>Clinical Document Architecture</title>
  <effectiveTime value="20140619191531-0400"/>
  <confidentialityCode code="N" codeSystem="2.16.840.1.113883.5.25"/>
  <languageCode code="en-US"/>
  <recordTarget>
    <patientRole>
      <id assigningAuthorityName="Enterprise MPI Number" extension="000000001" root="1.3.6.1.4.1.26923.99.1.1.1.2.1"/>
      <id assigningAuthorityName="Enterprise MPI Number" extension="000000001" root="1.3.6.1.4.1.26923.99.1.1.1.2.1"/>
      <id assigningAuthorityName="SSN" extension="999999999" root="2.16.840.1.113883.4.1"/>
      <id assigningAuthorityName="Hospital: Locally defined Patient Account Number" extension="000222222222" root="1.3.6.1.4.1.26923.99.1.1.44273.7.3"/>
      ...

1 个答案:

答案 0 :(得分:0)

您的陈述

WHEN 'CCD' THEN @xmlData.value('(hl7ns:ClinicalDocument/hl7ns:recordTarget/
                  hl7ns:patientRole/hl7ns:id/@extension)[1]', 'varchar(128)')
正如你所说,

选择第一个patientRole / id / @扩展名(因为[1])。但是你想要本地定义的患者ID,即

  <id assigningAuthorityName="Hospital: Locally defined Patient Account Number" 
       extension="000222222222" root="1.3.6.1.4.1.26923.99.1.1.44273.7.3"/>

因此,您可以根据assigningAuthorityName属性的值更改WHEN语句的XPath表达式:

WHEN 'CCD' THEN @xmlData.value('(hl7ns:ClinicalDocument/hl7ns:recordTarget/
   hl7ns:patientRole/hl7ns:id[
      assigningAuthorityName="Hospital: Locally defined Patient Account Number"]/
     @extension)[1]', 'varchar(128)')

(分手,所以你不必侧身滚动)。

这将为您提供@extension<id> assigningAuthorityName的第一个"Hospital: Locally defined Patient Account Number"的{​​{1}}。