use*_*683 8 sql-server-2008 xml sql-server
我正在尝试将 xml(实际上是 docx 文件)导入到 sql server 2008 数据库。我几乎是 XML 编程的新手。我用谷歌搜索了很多,但几乎所有的例子都有简单的 xml 文件。这里的 xml 文件有点复杂(请参见下文)。你能给我一些想法我应该如何为这个 XML 创建表以及我应该在 sql server 中运行什么查询。我需要所有标签的值,例如 w:rsidP,w:rsidRDefault,w:rsidR of w:p,w:pStyle,w:bookmarkStart,w:t 标签等。
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<w:document xmlns:ve="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:wne="http://schemas.microsoft.com/office/word/2006/wordml">
<w:body>
<w:p w:rsidR="00EF42E0" w:rsidRDefault="00EF42E0" w:rsidP="00EF42E0">
<w:pPr><w:pStyle w:val="Heading1"/>
</w:pPr><w:bookmarkStart w:id="0" w:name="_Toc212523610"/>
<w:r>
<w:t>Summary</w:t>
</w:r>
<w:bookmarkEnd w:id="0"/>
</w:p>
<w:p w:rsidR="00EF42E0" w:rsidRDefault="00EF42E0" w:rsidP="00EF42E0"><w:pPr><w:pStyle w:val="mainbodytext"/><w:ind w:right="-694"/><w:rPr><w:b/><w:bCs/></w:rPr></w:pPr><w:r><w:rPr><w:b/><w:bCs/></w:rPr><w:t>What is the Group Defined Practice for Integrity Management?</w:t></w:r></w:p>
<w:p w:rsidR="00EF42E0" w:rsidRDefault="00EF42E0" w:rsidP="00EF42E0"><w:pPr><w:pStyle w:val="mainbodytext"/></w:pPr><w:r><w:t xml:space="preserve">This Practice is derived from the GP Group Standard, GRP 01 January 2006, </w:t></w:r><w:proofErr w:type="gramStart"/><w:r><w:t>Integrity</w:t></w:r><w:proofErr w:type="gramEnd"/><w:r><w:t xml:space="preserve"> Management. In developing QMS it has been possible to embed much of the content of the IM Standard directly into the Group Essentials statements. For elements 2, 7, 8 and 9 of the Standard it was possible to do that in their entirety and therefore content of those elements are not repeated within this Practice.</w:t></w:r></w:p></w:body></w:document>
Run Code Online (Sandbox Code Playgroud)
Mik*_*son 10
在 SQL Server 中使用 XML 时,您使用xml 数据类型方法,而在分解 XML 文档时,您通常使用nodes()和value()方法。您在此处拥有的 XML 还包括许多名称空间,因此您必须使用WITH XMLNAMESPACES (Transact-SQL)指定您需要的名称空间。
XML 非常复杂,因此在不知道您希望如何提取数据的情况下,我只能为您提供几个示例查询,然后您可以将其修改为您需要的任何内容。
您有四个w:p节点,这是一个从这些节点获取属性的查询。Using@指定它是您想要的属性的值 -
with xmlnamespaces('http://schemas.openxmlformats.org/wordprocessingml/2006/main' as w)
select P.X.value('@w:rsidR', 'char(8)') as rsidR,
P.X.value('@w:rsidRDefault', 'char(8)') as rsidRDefault,
P.X.value('@w:rsidP', 'char(8)') as rsidP
from @doc.nodes('/w:document/w:body/w:p') as P(X);
Run Code Online (Sandbox Code Playgroud)
如果除此之外,w:t您还需要节点中的文本,则需要对cross apply第二个nodes()子句进行操作,该子句将分解w:p节点内的 XML 。
with xmlnamespaces('http://schemas.openxmlformats.org/wordprocessingml/2006/main' as w)
select P.X.value('@w:rsidR', 'char(8)') as rsidR,
P.X.value('@w:rsidRDefault', 'char(8)') as rsidRDefault,
P.X.value('@w:rsidP', 'char(8)') as rsidP,
T.X.value('text()[1]', 'nvarchar(max)') as Text
from @doc.nodes('/w:document/w:body/w:p') as P(X)
cross apply P.X.nodes('w:r/w:t') as T(X);
Run Code Online (Sandbox Code Playgroud)
您在问题中说要从所有标签中获取值。我不知道这对您有多大用处,但您可以使用 XML 中的所有属性和元素构建一个名称-值列表。
这将为您提供所有元素。
select T.X.value('local-name(.)', 'nvarchar(max)') Name,
T.X.value('.', 'nvarchar(max)') Value
from @doc.nodes('//*') as T(X)
Run Code Online (Sandbox Code Playgroud)
更改'//*'为'//@*',您将获得所有属性。
select T.X.value('local-name(.)', 'nvarchar(max)') Name,
T.X.value('.', 'nvarchar(max)') Value
from @doc.nodes('//@*') as T(X)
Run Code Online (Sandbox Code Playgroud)
您也可以将它们组合在一个查询中。
select T.X.value('local-name(.)', 'nvarchar(max)') Name,
T.X.value('.', 'nvarchar(max)') Value
from @doc.nodes('//@*, //*') as T(X)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
987 次 |
| 最近记录: |