使用SQL Server的OpenXML解析带有命名空间的XML片段

SQL Server的OpenXML函数可以针对XML片段或者文档进行解析,并处理。有关资料,你可以参考http://msdn.microsoft.com/en-us/library/ms186918.aspx

但如果该片段含有命名空间,情况可能会复杂一点。例如下面这个例子

第一部分:XML的内容

Code
 1<?xml-stylesheet href="ProductDescription.xsl" type="text/xsl"?>
 2<p1:ProductDescription xmlns:p1="http://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelDescription" xmlns:wm="http://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelWarrAndMain" xmlns:wf="http://www.adventure-works.com/schemas/OtherFeatures" xmlns:html="http://www.w3.org/1999/xhtml" ProductModelID="19" ProductModelName="Mountain 100">
 3  <p1:Summary>
 4    <html:p>Our top-of-the-line competition mountain bike. 
 5                 Performance-enhancing options include the innovative HL Frame, 
 6                super-smooth front suspension, and traction for all terrain.
 7                        </html:p>
 8  </p1:Summary>
 9  <p1:Manufacturer>
10    <p1:Name>AdventureWorks</p1:Name>
11    <p1:Copyright>2002</p1:Copyright>
12    <p1:ProductURL>HTTP://www.Adventure-works.com</p1:ProductURL>
13  </p1:Manufacturer>
14  <p1:Features>These are the product highlights. 
15                 <wm:Warranty><wm:WarrantyPeriod>3 years</wm:WarrantyPeriod><wm:Description>parts and labor</wm:Description></wm:Warranty><wm:Maintenance><wm:NoOfYears>10 years</wm:NoOfYears><wm:Description>maintenance contract available through your dealer or any AdventureWorks retail store.</wm:Description></wm:Maintenance><wf:wheel>High performance wheels.</wf:wheel><wf:saddle><html:i>Anatomic design</html:i> and made from durable leather for a full-day of riding in comfort.</wf:saddle><wf:pedal><html:b>Top-of-the-line</html:b> clipless pedals with adjustable tension.</wf:pedal><wf:BikeFrame>Each frame is hand-crafted in our Bothell facility to the optimum diameter 
16                and wall-thickness required of a premium mountain frame. 
17                The heat-treated welded aluminum frame has a larger diameter tube that absorbs the bumps.</wf:BikeFrame><wf:crankset> Triple crankset; alumunim crank arm; flawless shifting. </wf:crankset></p1:Features>
18  <!-- add one or more of these elements one for each specific product in this product model -->
19  <p1:Picture>
20    <p1:Angle>front</p1:Angle>
21    <p1:Size>small</p1:Size>
22    <p1:ProductPhotoID>118</p1:ProductPhotoID>
23  </p1:Picture>
24  <!-- add any tags in <specifications> -->
25  <p1:Specifications> These are the product specifications.
26                   <Material>Almuminum Alloy</Material><Color>Available in most colors</Color><ProductLine>Mountain bike</ProductLine><Style>Unisex</Style><RiderExperience>Advanced to Professional riders</RiderExperience></p1:Specifications>
27</p1:ProductDescription> 
28

 

第二部分:查询语法

 

 1 DECLARE   @XML  XML
 2 DECLARE   @HANDLER   INT
 3 SELECT   @XML = CatalogDescription  FROM  Production.ProductModel  WHERE  ProductModelID = 19  
 4
 5 EXEC  SP_XML_PREPAREDOCUMENT  @HANDLER  OUTPUT, @XML , ' <root xmlns:p1="http://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelDescription" /> '  
 6
 7 -- 这里要为PREPAREDOCUMENT存储过程加入第三个参数,就是命名空间的声明 
 8
 9 SELECT   *   FROM  OPENXML( @HANDLER , ' /p1:ProductDescription/p1:Manufacturer ' , 2 WITH  ( [ Name ]   NVARCHAR ( 50 ' p1:Name ' ,CopyRight  NVARCHAR ( 50 ' p1:Copyright '
10
11 EXEC  SP_XML_REMOVEDOCUMENT  @HANDLER    -- 一定不要忘记REMOVE 
12

 

第三部分:显示结果(这是我写的一个小的演示工具)

image

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值