SQL Server的OpenXML函数可以针对XML片段或者文档进行解析,并处理。有关资料,你可以参考http://msdn.microsoft.com/en-us/library/ms186918.aspx
但如果该片段含有命名空间,情况可能会复杂一点。例如下面这个例子
第一部分:XML的内容
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
第二部分:查询语法
2DECLARE @HANDLER INT
3SELECT @XML=CatalogDescription FROM Production.ProductModel WHERE ProductModelID=19
4
5EXEC SP_XML_PREPAREDOCUMENT @HANDLER OUTPUT,@XML,'<root xmlns:p1="http://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelDescription" />'
6
7--这里要为PREPAREDOCUMENT存储过程加入第三个参数,就是命名空间的声明
8
9SELECT * FROM OPENXML(@HANDLER,'/p1:ProductDescription/p1:Manufacturer',2) WITH ([Name] NVARCHAR(50) 'p1:Name',CopyRight NVARCHAR(50) 'p1:Copyright')
10
11EXEC SP_XML_REMOVEDOCUMENT @HANDLER --一定不要忘记REMOVE
12
第三部分:显示结果(这是我写的一个小的演示工具)