简体   繁体   中英

Read XML node value from SQL column

I am trying to extract data from an xml in a sql column and return that into a query result, but I have been struggling to read this partciluar xml. I am not sure what I am doing wrong or if the way the xml is structured needs a different approach. I have tried simpler XMLs without the attributes and references and was able to extract from a single node.

These are the XML file contents:

<InstanceXmlData xmlns="http://dev.docuware.com/bpw" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
  <ContextProperties>
    <FieldValue>
      <FieldID>GV_Divisional__Manager</FieldID>
      <Value i:type="AssignToItem">
        <EntryType>User</EntryType>
        <ID>0a28a5ad-db79-4369-9e53-84d0c0bbd0f7</ID>
        <Name>Onward TUBELA</Name>
        <ParentId>00000000-0000-0000-0000-000000000000</ParentId>
      </Value>
    </FieldValue>
    <FieldValue>
      <FieldID>GV_GM__Finance</FieldID>
      <Value i:nil="true" />
    </FieldValue>
    <FieldValue>
      <FieldID>GV_skip__accounts__review</FieldID>
      <Value i:nil="true" />
    </FieldValue>
    <FieldValue>
      <FieldID>DW_DWWorkflowCoreData</FieldID>
      <Value i:type="DWWorkflowCoreData">
        <ActivityName>Accountant Review</ActivityName>
        <AssignToData>
          <Definition>
            <AssignToItem>
              <EntryType>GlobalVariable</EntryType>
              <ID>00000000-0000-0000-0000-000000000000</ID>
              <Name>GV_Accountant</Name>
              <ParentId>00000000-0000-0000-0000-000000000000</ParentId>
            </AssignToItem>
          </Definition>
          <TaskUsers>
            <TaskUser>
              <ID>d1fceeb3-7833-44c2-b6e7-b4883300b987</ID>
              <Name>Hector THOLE</Name>
              <SubstitutionRuleID>00000000-0000-0000-0000-000000000000</SubstitutionRuleID>
            </TaskUser>
          </TaskUsers>
        </AssignToData>
        <CurrentUserId>058b7c3b-9dff-46c2-8654-93b84eb1eac0</CurrentUserId>
        <CurrentUserName>Livhuwani NEMBILWI</CurrentUserName>
        <DocumentLink i:nil="true" />
        <DocumentUserVersion>1.0</DocumentUserVersion>
        <DocumentVersionId>17973</DocumentVersionId>
        <ExpirationDate>0001-01-01T00:00:00</ExpirationDate>
        <FileCabinetID>62005e95-7a62-470e-9fca-c0a2968a22d8</FileCabinetID>
        <HistoryStepCount>25</HistoryStepCount>
        <InstanceStartDate>2020-03-20T14:43:43.9549487Z</InstanceStartDate>
        <NotificationDate>0001-01-01T00:00:00</NotificationDate>
        <OrganizationID>e6674399-4abc-48dc-95a7-7a484ceab4e5</OrganizationID>
        <ReceivedOn>2020-03-31T12:48:15.8432349Z</ReceivedOn>
        <RetryCount>0</RetryCount>
        <TaskLink i:nil="true" />
        <TaskListLink i:nil="true" />
        <TaskUserName>admin</TaskUserName>
        <WorkflowId>bdaee87a-8b5c-4c8e-8cda-c84ec31ae333</WorkflowId>
        <WorkflowName>Journals</WorkflowName>
        <WorkflowVersion>61</WorkflowVersion>
      </Value>
    </FieldValue>
    <FieldValue>
      <FieldID>GV_GM</FieldID>
      <Value i:nil="true" />
    </FieldValue>
    <FieldValue>
      <FieldID>GV_pp__ref</FieldID>
      <Value i:nil="true" />
    </FieldValue>
    <FieldValue>
      <FieldID>GV_query__comment</FieldID>
      <Value xmlns:a="http://www.w3.org/2001/XMLSchema" i:type="a:string">support for the amount not attached </Value>
    </FieldValue>
    <FieldValue>
      <FieldID>GV_Financial__Accounting__Manager</FieldID>
      <Value i:type="AssignToItem">
        <EntryType>User</EntryType>
        <ID>3df4acb0-b321-4845-a274-84e4210914f2</ID>
        <Name>Mmasikati Nkgadima</Name>
        <ParentId>00000000-0000-0000-0000-000000000000</ParentId>
      </Value>
    </FieldValue>
    <FieldValue>
      <FieldID>GV_company__code</FieldID>
      <Value xmlns:a="http://www.w3.org/2001/XMLSchema" i:type="a:string">083H</Value>
    </FieldValue>
    <FieldValue>
      <FieldID>GV_igg</FieldID>
      <Value xmlns:a="http://www.w3.org/2001/XMLSchema" i:type="a:string">MABELKOB</Value>
    </FieldValue>
    <FieldValue>
      <FieldID>GV_Originator</FieldID>
      <Value i:type="AssignToItem">
        <EntryType>User</EntryType>
        <ID>0bfef02c-4222-41b4-bd9f-4e04984b74c7</ID>
        <Name>Mabel KOBE</Name>
        <ParentId>00000000-0000-0000-0000-000000000000</ParentId>
      </Value>
    </FieldValue>
    <FieldValue>
      <FieldID>GV_sap__post__url</FieldID>
      <Value xmlns:a="http://www.w3.org/2001/XMLSchema" i:type="a:string">http://zarmtsaros-ap08:8082/post_to_sap_auto</Value>
    </FieldValue>
    <FieldValue>
      <FieldID>GV_exchange__rate</FieldID>
      <Value xmlns:a="http://www.w3.org/2001/XMLSchema" i:type="a:string">1.8331</Value>
    </FieldValue>
    <FieldValue>
      <FieldID>GV_Line__Manager</FieldID>
      <Value i:type="AssignToItem">
        <EntryType>User</EntryType>
        <ID>cb845699-5dd8-4ea6-98a8-931d0e36f06a</ID>
        <Name>Letsogile Remmonye MOAMPE</Name>
        <ParentId>00000000-0000-0000-0000-000000000000</ParentId>
      </Value>
    </FieldValue>
    <FieldValue>
      <FieldID>GV_posting__month</FieldID>
      <Value xmlns:a="http://www.w3.org/2001/XMLSchema" i:type="a:string">31-03-2020</Value>
    </FieldValue>
    <FieldValue>
      <FieldID>GV_General__Manager</FieldID>
      <Value i:type="AssignToItem">
        <EntryType>User</EntryType>
        <ID>0a28a5ad-db79-4369-9e53-84d0c0bbd0f7</ID>
        <Name>Onward TUBELA</Name>
        <ParentId>00000000-0000-0000-0000-000000000000</ParentId>
      </Value>
    </FieldValue>
    <FieldValue>
      <FieldID>GV_Department</FieldID>
      <Value i:nil="true" />
    </FieldValue>
    <FieldValue>
      <FieldID>GV_Accountant</FieldID>
      <Value i:type="AssignToItem">
        <EntryType>User</EntryType>
        <ID>058b7c3b-9dff-46c2-8654-93b84eb1eac0</ID>
        <Name>Livhuwani Nembilwi</Name>
        <ParentId>00000000-0000-0000-0000-000000000000</ParentId>
      </Value>
    </FieldValue>
    <FieldValue>
      <FieldID>GV_total__amount</FieldID>
      <Value xmlns:a="http://www.w3.org/2001/XMLSchema" i:type="a:decimal">1992943.771991</Value>
    </FieldValue>
    <FieldValue>
      <FieldID>GV_DW</FieldID>
      <Value xmlns:a="http://www.w3.org/2001/XMLSchema" i:type="a:string">DWPP2342</Value>
    </FieldValue>
    <FieldValue>
      <FieldID>GV_MDCEO</FieldID>
      <Value i:type="AssignToItem">
        <EntryType>User</EntryType>
        <ID>8cd8362e-31d3-4603-bf65-cbcb1484956a</ID>
        <Name>Mariam KANE-GARCIA</Name>
        <ParentId>00000000-0000-0000-0000-000000000000</ParentId>
      </Value>
    </FieldValue>
    <FieldValue>
      <FieldID>GV_division</FieldID>
      <Value xmlns:a="http://www.w3.org/2001/XMLSchema" i:type="a:string">Botswana</Value>
    </FieldValue>
    <FieldValue>
      <FieldID>GV_result</FieldID>
      <Value i:nil="true" />
    </FieldValue>
  </ContextProperties>
  <FaultedInfo i:nil="true" />
  <StopWorkflowInfo i:nil="true" />
  <UserCredentialsInfo i:nil="true" />
</InstanceXmlData>

Ideally I would want the value in the name child node of the TaskUser : "Hector THOLE" but as a simple way to debug I tried to use the FieldID : "GV_Divisional__Manager" with this query:

 select XmlData.value('(/InstanceXmlData/ContextProperties/FieldValue/FieldID)[1]','varchar(max)') as 
 TaskUser
 from [dwworkflowengine].[dbo].[Instances]

The results are:

Query Results

I am not sure why the results are all null and any help would be much appreciated.

You have a default XML namespace in your XML document:

<InstanceXmlData xmlns="http://dev.docuware.com/bpw" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
                 ***********************************

but so far, you're totally ignoring it - don't do that!

To get the ID element's value for Hector TOHLE , use this SQL:

-- define the default XML namespace to use
WITH XMLNAMESPACES(DEFAULT 'http://dev.docuware.com/bpw') 
    SELECT XmlData.value('(/InstanceXmlData/ContextProperties/FieldValue/Value/AssignToData/TaskUsers/TaskUser/ID)[1]', 'varchar(50)') 
    FROM [dwworkflowengine].[dbo].[Instances]
    WHERE XmlData.value('(/InstanceXmlData/ContextProperties/FieldValue/Value/AssignToData/TaskUsers/TaskUser/Name)[1]', 'varchar(50)') = 'Hector THOLE'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM