这里已经有一些类似的问题了,但它们似乎对我没用。我需要查询XML文件以从中提取数据,因此我们可以将查询结果用于其他目的。我在网上搜索了一个方法来执行此操作,似乎value()方法是推荐的方法。我用以下代码尝试了这个:
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'
然后我收到三列的查询结果(到目前为止一直很好),但所有三列的记录都是NULL。当我删除BlockOrderMessage中的名称空间声明时,它确实有效,所以我假设我需要在查询中的某处声明这些名称空间,但我找不到如何。有人怎么做这个?
提前致谢!
答案 0 :(得分:0)
HY,
首先,我认为它区分大小写,因此将Blocktype
更正为BlockType
。不要将BlockType
转换为int
,因为它是string
所以请使用nvarchar(max)
。在查询中使用xml命名空间,如下所示
; 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'`
以下是完整查询
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'