0

There's already a few similar questions on here, but they don't seem to work for me. I need to query an XML file to extract data from it, so we can use the result of the query for other purposes. I have searched on the net for a method to do this, and it seems the value () method is the recommended method. I tried this with the following code:

DECLARE @x XML
SET @x = 
'<?xml version="1.0" encoding="UTF-8"?>
<BlockOrderMessage xmlns="http://www.test.com/production/block"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.test.com/production/block file:/C:/Users/test.xsd"
BlockNumber="BlockNumber1">
<BlockStartTime>01:01:01.001</BlockStartTime>
<ProductionDate>2006-05-04</ProductionDate>
<BlockType>Bloc</BlockType>
<FlightOrders>
    <FlightOrder>
        <Flight>
            <FlightNr>FlightNr0</FlightNr>
            <DepartureDate>2006-05-04</DepartureDate>
            <DepartureTime>01:01:01.001</DepartureTime>
            <AircraftType>AircraftType0</AircraftType>
            <AircraftSeatConfiguration>AircraftSeatConfiguration0</AircraftSeatConfiguration>
        </Flight>
        <CATC>CATC0</CATC>
    </FlightOrder>
    <FlightOrder>
        <Flight>
            <FlightNr>FlightNr1</FlightNr>
            <DepartureDate>2006-05-04</DepartureDate>
            <DepartureTime>01:01:01.001</DepartureTime>
            <AircraftType>AircraftType1</AircraftType>
            <AircraftSeatConfiguration>AircraftSeatConfiguration1</AircraftSeatConfiguration>
        </Flight>
   </FlightOrder>
</FlightOrders>

'

SELECT @x.value('(/BlockOrderMessage/Blocktype)[1]','int') AS 'BlockType',
   @x.value('(/BlockOrderMessage/FlightOrders/FlightOrder/Flight/FlightNr)[1]','VARCHAR (20)') AS 'FlightNr',
   @x.value('(/BlockOrderMessage/FlightOrders/FlightOrder/Flight/AircraftType)[1]','VARCHAR (20)') AS 'AircraftType'   

Then i receive a query result of three columns (so far so good), but the records are NULL for all three columns. It does work when I remove the namespace declarations in BlockOrderMessage, so I assume I need to declare these namespaces somewhere in my query, but I cannot find how. Does anyone how I should do this?

Thanks in advance!

       

Michiel281
  • 55
  • 1
  • 4

1 Answers1

0

Hy, First of all i think it is case sensitive, so correct Blocktype to BlockType. Do not convert BlockType to int because it is string so use nvarchar(max). Use xml namespaces in your query, like this ; WITH XMLNAMESPACES (default 'http://www.test.com/production/block')

SELECT @x.value('(/BlockOrderMessage/BlockType)[1]','varchar(max)') AS 'BlockType', @x.value('(/BlockOrderMessage/FlightOrders/FlightOrder/Flight/FlightNr)[1]','VARCHAR (20)') AS 'FlightNr', @x.value('(/BlockOrderMessage/FlightOrders/FlightOrder/Flight/AircraftType)[1]','VARCHAR (20)') AS 'AircraftType' `

Here is the full query

DECLARE @x XML
SET @x = 
'<?xml version="1.0" encoding="UTF-8"?>
<BlockOrderMessage xmlns="http://www.test.com/production/block"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.test.com/production/block file:/C:/Users/test.xsd"
BlockNumber="BlockNumber1">
<BlockStartTime>01:01:01.001</BlockStartTime>
<ProductionDate>2006-05-04</ProductionDate>
<BlockType>Bloc</BlockType>
<FlightOrders>
    <FlightOrder>
        <Flight>
            <FlightNr>FlightNr0</FlightNr>
            <DepartureDate>2006-05-04</DepartureDate>
            <DepartureTime>01:01:01.001</DepartureTime>
            <AircraftType>AircraftType0</AircraftType>
            <AircraftSeatConfiguration>AircraftSeatConfiguration0</AircraftSeatConfiguration>
        </Flight>
        <CATC>CATC0</CATC>
    </FlightOrder>
    <FlightOrder>
        <Flight>
            <FlightNr>FlightNr1</FlightNr>
            <DepartureDate>2006-05-04</DepartureDate>
            <DepartureTime>01:01:01.001</DepartureTime>
            <AircraftType>AircraftType1</AircraftType>
            <AircraftSeatConfiguration>AircraftSeatConfiguration1</AircraftSeatConfiguration>
        </Flight>
   </FlightOrder>
</FlightOrders>
</BlockOrderMessage>'
; WITH XMLNAMESPACES (default 'http://www.test.com/production/block')

SELECT @x.value('(/BlockOrderMessage/BlockType)[1]','varchar(50)') AS 'BlockType',
   @x.value('(/BlockOrderMessage/FlightOrders/FlightOrder/Flight/FlightNr)[1]','VARCHAR (20)') AS 'FlightNr',
   @x.value('(/BlockOrderMessage/FlightOrders/FlightOrder/Flight/AircraftType)[1]','VARCHAR (20)') AS 'AircraftType'  
Andras
  • 56
  • 3