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

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

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

第一部分:XML的内容

Code
 1xml-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>AdventureWorksp1:Name>
11    <p1:Copyright>2002p1:Copyright>
12    <p1:ProductURL>HTTP://www.Adventure-works.comp1:ProductURL>
13  p1:Manufacturer>
14  <p1:Features>These are the product highlights. 
15                 <wm:Warranty><wm:WarrantyPeriod>3 yearswm:WarrantyPeriod><wm:Description>parts and laborwm:Description>wm:Warranty><wm:Maintenance><wm:NoOfYears>10 yearswm: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 designhtml:i> and made from durable leather for a full-day of riding in comfort.wf:saddle><wf:pedal><html:b>Top-of-the-linehtml: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  
19  <p1:Picture>
20    <p1:Angle>frontp1:Angle>
21    <p1:Size>smallp1:Size>
22    <p1:ProductPhotoID>118p1:ProductPhotoID>
23  p1:Picture>
24  
25  <p1:Specifications> These are the product specifications.
26                   <Material>Almuminum AlloyMaterial><Color>Available in most colorsColor><ProductLine>Mountain bikeProductLine><Style>UnisexStyle><RiderExperience>Advanced to Professional ridersRiderExperience>p1:Specifications>
27p1: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 , ' '  
 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

 

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

使用SQL Server的OpenXML解析带有命名空间的XML片段_第1张图片

你可能感兴趣的:(使用SQL Server的OpenXML解析带有命名空间的XML片段)