0

I'm trying to parse XML in T-SQL but doesn't understand how to write my code.

My XML output:

<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:tns="https://api.quinyx.com/soap/FlexForce">
<SOAP-ENV:Body>
<ns1:wsdlGetTimePunchesResponse xmlns:ns1="uri:FlexForce">
      <return xsi:type="tns:GetTimePunchesResponse">
        <timepunches xsi:type="SOAP-ENC:Array" SOAP-ENC:arrayType="tns:TimePunch[1868]">
          <item xsi:type="tns:TimePunch">
            <id xsi:type="xsd:int">134054629</id>
            <employeeName xsi:type="xsd:string">Test, Person</employeeName>
            <restId xsi:type="xsd:int">1111</restId>
            <badgeNo xsi:type="xsd:string">5000</badgeNo>
            <socsecNo xsi:type="xsd:string" />
            <cardNo xsi:type="xsd:string">5000</cardNo>
            <punchIn xsi:type="xsd:dateTime">2019-01-14T08:00:00+01:00</punchIn>
            <punchOut xsi:type="xsd:dateTime">2019-01-14T17:00:00+01:00</punchOut>
            <hours xsi:type="xsd:decimal">8</hours>
            <approvedByEmployee xsi:type="xsd:boolean">true</approvedByEmployee>
            <approvedByManager xsi:type="xsd:boolean">true</approvedByManager>
            <approvedByCustomer xsi:type="xsd:boolean">false</approvedByCustomer>
            <costCentre xsi:type="xsd:string">8002</costCentre>
            <projectNo xsi:type="xsd:string" />
            <accountNo xsi:type="xsd:string" />
            <agrmntAdditionalField1 xsi:type="xsd:string" />
            <agrmntAdditionalField2 xsi:type="xsd:string" />
            <agrmntAdditionalField3 xsi:type="xsd:string" />
            <agrmntAdditionalField4 xsi:type="xsd:string" />
            <agrmntAdditionalField5 xsi:type="xsd:string" />
            <externalInfo1 xsi:type="xsd:string" />
            <externalInfo2 xsi:type="xsd:string" />
            <externalInfo3 xsi:type="xsd:string" />
            <scheduleId xsi:type="xsd:int">2000000</scheduleId>
            <transferredToPayroll xsi:type="xsd:boolean">true</transferredToPayroll>
            <deleted xsi:type="xsd:boolean">false</deleted>
            <productiveTime xsi:type="xsd:boolean">true</productiveTime>
            <isOpen xsi:type="xsd:boolean">false</isOpen>
            <comment xsi:type="xsd:string" />
            <managerComment xsi:type="xsd:string" />
            <employeeId xsi:type="xsd:int">1000000</employeeId>
            <ts xsi:type="xsd:dateTime">2019-09-07T09:17:48+02:00</ts>
          </item>
        </timepunches>
        <validationErrors xsi:type="SOAP-ENC:Array" SOAP-ENC:arrayType="xsd:string[0]" />
      </return>

The XML is stored into a single column in my database table.

I have tried multiple ways to get any data out but they all end up showing nothing.

Doesn't even know where to start to be honest.

It's a SQL Server database.

Here is the existing query:

select cast( replace(XMLData, '<?xml version="1.0" encoding="UTF-8"?>','') as xml) as XMLData into #temp from [Quniyx].[xmltest] select XMLData.value('return[1]/TimePunch[1]/item[1]/id[1]','int') from #temp
3
  • Hi Jimmy - can you show us the SQL Query that you are using to try retrieving this content? Commented Oct 10, 2019 at 15:59
  • Hey, An example: select cast( replace(XMLData, '<?xml version="1.0" encoding="UTF-8"?>','') as xml) as XMLData into #temp from [Quniyx].[xmltest] select XMLData.value('return[1]/TimePunch[1]/item[1]/id[1]','int') from #temp Commented Oct 10, 2019 at 16:10
  • Read stackoverflow.com/questions/22818591/… Commented Oct 10, 2019 at 17:19

1 Answer 1

1

The provided XML is not well-formed. I had to adjust it. Additionally, the XML has multiple namespaces. The rest is trivial.

SQL

-- DDL and sample data population, start
DECLARE @tbl TABLE (ID INT IDENTITY PRIMARY KEY, xml_data XML);
INSERT INTO @tbl
VALUES
('<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
                   xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
                   xmlns:xsd="http://www.w3.org/2001/XMLSchema"
                   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                   xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/"
                   xmlns:tns="https://api.quinyx.com/soap/FlexForce">
    <SOAP-ENV:Body>
        <ns1:wsdlGetTimePunchesResponse xmlns:ns1="uri:FlexForce">
            <return xsi:type="tns:GetTimePunchesResponse">
                <timepunches xsi:type="SOAP-ENC:Array" SOAP-ENC:arrayType="tns:TimePunch[1868]">
                    <item xsi:type="tns:TimePunch">
                        <id xsi:type="xsd:int">134054629</id>
                        <employeeName xsi:type="xsd:string">Test, Person</employeeName>
                        <restId xsi:type="xsd:int">1111</restId>
                        <badgeNo xsi:type="xsd:string">5000</badgeNo>
                        <socsecNo xsi:type="xsd:string"/>
                        <cardNo xsi:type="xsd:string">5000</cardNo>
                        <punchIn xsi:type="xsd:dateTime">2019-01-14T08:00:00+01:00</punchIn>
                        <punchOut xsi:type="xsd:dateTime">2019-01-14T17:00:00+01:00</punchOut>
                        <hours xsi:type="xsd:decimal">8</hours>
                        <approvedByEmployee xsi:type="xsd:boolean">true</approvedByEmployee>
                        <approvedByManager xsi:type="xsd:boolean">true</approvedByManager>
                        <approvedByCustomer xsi:type="xsd:boolean">false</approvedByCustomer>
                        <costCentre xsi:type="xsd:string">8002</costCentre>
                        <projectNo xsi:type="xsd:string"/>
                        <accountNo xsi:type="xsd:string"/>
                        <agrmntAdditionalField1 xsi:type="xsd:string"/>
                        <agrmntAdditionalField2 xsi:type="xsd:string"/>
                        <agrmntAdditionalField3 xsi:type="xsd:string"/>
                        <agrmntAdditionalField4 xsi:type="xsd:string"/>
                        <agrmntAdditionalField5 xsi:type="xsd:string"/>
                        <externalInfo1 xsi:type="xsd:string"/>
                        <externalInfo2 xsi:type="xsd:string"/>
                        <externalInfo3 xsi:type="xsd:string"/>
                        <scheduleId xsi:type="xsd:int">2000000</scheduleId>
                        <transferredToPayroll xsi:type="xsd:boolean">true</transferredToPayroll>
                        <deleted xsi:type="xsd:boolean">false</deleted>
                        <productiveTime xsi:type="xsd:boolean">true</productiveTime>
                        <isOpen xsi:type="xsd:boolean">false</isOpen>
                        <comment xsi:type="xsd:string"/>
                        <managerComment xsi:type="xsd:string"/>
                        <employeeId xsi:type="xsd:int">1000000</employeeId>
                        <ts xsi:type="xsd:dateTime">2019-09-07T09:17:48+02:00</ts>
                    </item>
                </timepunches>
                <validationErrors xsi:type="SOAP-ENC:Array" SOAP-ENC:arrayType="xsd:string[0]"/>
            </return>
        </ns1:wsdlGetTimePunchesResponse>
    </SOAP-ENV:Body>
</SOAP-ENV:Envelope>');
-- DDL and sample data population, end

;WITH XMLNAMESPACES ('uri:FlexForce' as ns1, 'http://schemas.xmlsoap.org/soap/envelope/' AS [SOAP-ENV])
SELECT ID
    , col.value('(id/text())[1]','INT') AS id
    , col.value('(employeeName/text())[1]','VARCHAR(200)') AS employeeName
    , col.value('(restId/text())[1]','VARCHAR(200)') AS restId
    -- add the rest of the elements here
FROM @tbl tbl
    CROSS APPLY tbl.xml_data.nodes('/SOAP-ENV:Envelope/SOAP-ENV:Body/ns1:wsdlGetTimePunchesResponse/return/timepunches/item') AS tab(col)

Output

+----+-----------+--------------+--------+
| ID |    id     | employeeName | restId |
+----+-----------+--------------+--------+
|  1 | 134054629 | Test, Person |   1111 |
+----+-----------+--------------+--------+
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.