我正在尝试从XML文件中检索数据。下面是XML文档的外观和下面的SQL代码。它将运行代码并显示列标题 - 但不会填充任何数据。我错过了什么?
<profile xmlns="http://feed.elasticstats.com/schema/mma/v1/participants-profile.xsd" generated="2015-12-10T17:34:54Z">
<fighters>
<fighter id="01585452-852a-4b40-a6dc-fdd04279f02c" height="72" weight="170" reach="" stance="" first_name="Sai" nick_name="The Boss" last_name="Wang">
<record wins="6" losses="4" draws="1" no_contests="0" />
<born date="1988-01-16" country_code="UNK" country="Unknown" state="" city="" />
<out_of country_code="UNK" country="Unknown" state="" city="" />
</fighter>
<fighter id="0168dd6b-b3e1-4954-8b71-877a63772dec" height="" weight="0" reach="" stance="" first_name="Enrique" nick_name="Wasabi" last_name="Marin">
<record wins="8" losses="2" draws="0" no_contests="0" />
<born date="" country_code="UNK" country="Unknown" state="" city="" />
<out_of country_code="UNK" country="Unknown" state="" city="" />
</fighter>
DECLARE @x xml
SELECT @x = P
FROM OPENROWSET (BULK 'C:\Python27\outputMMA.xml', SINGLE_BLOB) AS FIGHTERS(P)
DECLARE @hdoc int
EXEC sp_xml_preparedocument @hdoc OUTPUT, @x
SELECT *
FROM OPENXML (@hdoc, '/fighters/fighter', 1) --1\ IS ATTRIBUTES AND 2 IS ELEMENTS
WITH (
id varchar(100),
height varchar(10),
last_name varchar(100)
) --THIS IS WHERE YOU SELECT FIELDS you want returned
EXEC sp_xml_removedocument @hdoc
答案 0 :(得分:1)
FROM OPENXML
不再是最好的方法了。试试这样:
只需将其复制到一个空查询窗口并执行:
DECLARE @xml XML=
'<profile xmlns="http://feed.elasticstats.com/schema/mma/v1/participants-profile.xsd" generated="2015-12-10T17:34:54Z">
<fighters>
<fighter id="01585452-852a-4b40-a6dc-fdd04279f02c" height="72" weight="170" reach="" stance="" first_name="Sai" nick_name="The Boss" last_name="Wang">
<record wins="6" losses="4" draws="1" no_contests="0" />
<born date="1988-01-16" country_code="UNK" country="Unknown" state="" city="" />
<out_of country_code="UNK" country="Unknown" state="" city="" />
</fighter>
<fighter id="0168dd6b-b3e1-4954-8b71-877a63772dec" height="" weight="0" reach="" stance="" first_name="Enrique" nick_name="Wasabi" last_name="Marin">
<record wins="8" losses="2" draws="0" no_contests="0" />
<born date="" country_code="UNK" country="Unknown" state="" city="" />
<out_of country_code="UNK" country="Unknown" state="" city="" />
</fighter>
</fighters>
</profile>';
WITH XMLNAMESPACES(DEFAULT 'http://feed.elasticstats.com/schema/mma/v1/participants-profile.xsd')
SELECT One.fighter.value('@id','uniqueidentifier') AS Fighter_ID
,One.fighter.value('@height','int') AS Fighter_Height
,One.fighter.value('@weight','int') AS Fighter_Weigth
,One.fighter.value('@reach','varchar(100)') AS Fighter_Height
,One.fighter.value('@stance','varchar(100)') AS Fighter_Height
,One.fighter.value('@first_name','varchar(100)') AS Fighter_FirstName
,One.fighter.value('@nick_name','varchar(100)') AS Fighter_NickName
,One.fighter.value('@last_name','varchar(100)') AS Fighter_LastName
,One.fighter.value('record[1]/@wins','int') AS FighterRecord_Wins
,One.fighter.value('record[1]/@draws','int') AS FighterRecord_Draws
,One.fighter.value('record[1]/@no_contests','int') AS FighterRecord_NoContest
,One.fighter.value('born[1]/@date','date') AS FighterBorn_Date
,One.fighter.value('born[1]/@country_code','varchar(10)') AS FighterBorn_CountryCode
,One.fighter.value('born[1]/@country','varchar(100)') AS FighterBorn_Country
,One.fighter.value('born[1]/@state','varchar(100)') AS FighterBorn_State
,One.fighter.value('born[1]/@city','varchar(100)') AS FighterBorn_City
,One.fighter.value('out_of[1]/@country_code','varchar(10)') AS FighterOutOf_CountryCode
,One.fighter.value('out_of[1]/@country','varchar(100)') AS FighterOutOf_Country
,One.fighter.value('out_of[1]/@state','varchar(100)') AS FighterOutOf_State
,One.fighter.value('out_of[1]/@city','varchar(100)') AS FighterOutOf_City
FROM @xml.nodes('/profile/fighters/fighter') AS One(fighter)
答案 1 :(得分:0)
您的XML文档中有一个未声明的命名空间。考虑声明命名空间并在xpath表达式中引用它的修订:
DECLARE @x xml;
SELECT @x = P
FROM OPENROWSET (BULK 'C:\Python27\outputMMA.xml', SINGLE_BLOB) AS FIGHTERS(P)
DECLARE @hdoc int
EXEC sp_xml_preparedocument @hdoc OUTPUT, @x,
'<root xmlns:doc="http://feed.elasticstats.com/schema/mma/v1/participants-profile.xsd"/>'
SELECT *
FROM OPENXML (@hdoc, '/doc:profile/doc:fighters/doc:fighter', 1)
WITH (
id varchar(100),
height varchar(10),
last_name varchar(100)
)
EXEC sp_xml_removedocument @hdoc
答案 2 :(得分:0)
首先修复数据(:xs
,</fighters>
,</profile>
)
<profile xmlns:xs="http://feed.elasticstats.com/schema/mma/v1/participants-profile.xsd" generated="2015-12-10T17:34:54Z">
<fighters>
<fighter id="01585452-852a-4b40-a6dc-fdd04279f02c" height="72" weight="170" reach="" stance="" first_name="Sai" nick_name="The Boss" last_name="Wang">
<record wins="6" losses="4" draws="1" no_contests="0" />
<born date="1988-01-16" country_code="UNK" country="Unknown" state="" city="" />
<out_of country_code="UNK" country="Unknown" state="" city="" />
</fighter>
<fighter id="0168dd6b-b3e1-4954-8b71-877a63772dec" height="" weight="0" reach="" stance="" first_name="Enrique" nick_name="Wasabi" last_name="Marin">
<record wins="8" losses="2" draws="0" no_contests="0" />
<born date="" country_code="UNK" country="Unknown" state="" city="" />
<out_of country_code="UNK" country="Unknown" state="" city="" />
</fighter>
</fighters>
</profile>
然后是代码
FROM OPENXML (@docHandle, 'profile/fighters/fighter', 1)
我们已经完成了
01585452-852a-4b40-a6dc-fdd04279f02c 72 Wang
0168dd6b-b3e1-4954-8b71-877a63772dec Marin