Sql server 尝试读取XML的节点值时发生XML分析错误
我有一个XML文件(TestArticles.XML),我需要将其导入SQL SERVER 2014,从各个节点读取数据,并将其插入同一数据库中的其他几个表中 TestArticles.xmlSql server 尝试读取XML的节点值时发生XML分析错误,sql-server,xml,sql-server-2014,Sql Server,Xml,Sql Server 2014,我有一个XML文件(TestArticles.XML),我需要将其导入SQL SERVER 2014,从各个节点读取数据,并将其插入同一数据库中的其他几个表中 TestArticles.xml <?xml version="1.0" encoding="UTF-8"?> <Articles> <sv:node sv:name="test1" xmlns:sv="http://www.jcp.org/jcr/sv/1.0" xmlns:xsi="http://
<?xml version="1.0" encoding="UTF-8"?>
<Articles>
<sv:node sv:name="test1"
xmlns:sv="http://www.jcp.org/jcr/sv/1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<sv:property sv:name="jcr:primaryType" sv:type="Name">
<sv:value>mgnl:tax-article</sv:value>
</sv:property>
<sv:property sv:name="jcr:createdBy" sv:type="String">
<sv:value>system</sv:value>
</sv:property>
</sv:node>
<sv:node sv:name="test2"
xmlns:sv="http://www.jcp.org/jcr/sv/1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<sv:property sv:name="jcr:primaryType" sv:type="Name">
<sv:value>mgnl:tax-article</sv:value>
</sv:property>
<sv:property sv:name="jcr:createdBy" sv:type="String">
<sv:value>admin</sv:value>
</sv:property>
</sv:node>
</Articles>
在执行上述查询时,我得到以下提到的错误: Msg 6603,16级,状态2,第14行 XML分析错误:引用了未声明的命名空间前缀:“sv” 我想从TestArticles.xml获取以下内容
有人能帮我解决这个问题吗?您需要先明确定义前缀,例如,使用和XQuery:
;WITH XMLNAMESPACES('http://www.jcp.org/jcr/sv/1.0' as sv)
SELECT c.value('@sv:name', 'varchar(100)') As Name
FROM ArticlesXML a
CROSS APPLY ArticlesXMLData.nodes('Articles/sv:node') t(c)
通过此查询,您可以读取XML中的所有数据:
CREATE TABLE ArticlesXML
(
Id INT IDENTITY PRIMARY KEY,
ArticlesXMLData XML,
LoadedDateTime DATETIME
)
GO
SET IDENTITY_INSERT ArticlesXML ON;
INSERT INTO ArticlesXML(Id,ArticlesXMLData,LoadedDateTime) VALUES
(1,
'<?xml version="1.0" encoding="UTF-8"?>
<Articles>
<sv:node sv:name="test1"
xmlns:sv="http://www.jcp.org/jcr/sv/1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<sv:property sv:name="jcr:primaryType" sv:type="Name">
<sv:value>mgnl:tax-article</sv:value>
</sv:property>
<sv:property sv:name="jcr:createdBy" sv:type="String">
<sv:value>system</sv:value>
</sv:property>
</sv:node>
<sv:node sv:name="test2"
xmlns:sv="http://www.jcp.org/jcr/sv/1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<sv:property sv:name="jcr:primaryType" sv:type="Name">
<sv:value>mgnl:tax-article</sv:value>
</sv:property>
<sv:property sv:name="jcr:createdBy" sv:type="String">
<sv:value>admin</sv:value>
</sv:property>
</sv:node>
</Articles>',GETDATE());
SET IDENTITY_INSERT ArticlesXML OFF;
WITH XMLNAMESPACES('http://www.jcp.org/jcr/sv/1.0' AS sv
,'http://www.w3.org/2001/XMLSchema-instance' AS xsi)
SELECT Id
,Article.value('@sv:name','varchar(max)') AS ArticleName
,Property.value('@sv:name','varchar(max)') AS PropertyName
,Property.value('@sv:type','varchar(max)') AS PropertyType
,Property.value('sv:value[1]','varchar(max)') AS PropertyValue
,LoadedDateTime
FROM ArticlesXML
CROSS APPLY ArticlesXML.ArticlesXMLData.nodes('/Articles/sv:node') A(Article)
CROSS APPLY A.Article.nodes('sv:property') AS B(Property);
--CleanUp
--DROP TABLE ArticlesXML;
如果您想以某种方式查询(筛选、聚合…),您有几个机会:
- 将结果放入临时表中,并根据需要使用它
- 与声明的表变量相同
- 环绕并将其用作CTE
- 或者您可以使用XQuery谓词(例如
).nodes('/Articles/sv:node[sv:name=“test1”]”)
+----+-------------+-----------------+--------------+------------------+-------------------------+
| Id | ArticleName | PropertyName | PropertyType | PropertyValue | LoadedDateTime |
+----+-------------+-----------------+--------------+------------------+-------------------------+
| 1 | test1 | jcr:primaryType | Name | mgnl:tax-article | 2016-03-31 20:30:27.240 |
+----+-------------+-----------------+--------------+------------------+-------------------------+
| 1 | test1 | jcr:createdBy | String | system | 2016-03-31 20:30:27.240 |
+----+-------------+-----------------+--------------+------------------+-------------------------+
| 1 | test2 | jcr:primaryType | Name | mgnl:tax-article | 2016-03-31 20:30:27.240 |
+----+-------------+-----------------+--------------+------------------+-------------------------+
| 1 | test2 | jcr:createdBy | String | admin | 2016-03-31 20:30:27.240 |
+----+-------------+-----------------+--------------+------------------+-------------------------+
| 1 | test3 | jcr:createdBy | String | admin1 | 2016-03-31 20:30:27.240 |
+----+-------------+-----------------+--------------+------------------+-------------------------+
| 1 | test3 | jcr:createdBy | String | admin2 | 2016-03-31 20:30:27.240 |
+----+-------------+-----------------+--------------+------------------+-------------------------+
| 1 | test3 | jcr:createdBy | String | admin3 | 2016-03-31 20:30:27.240 |
+----+-------------+-----------------+--------------+------------------+-------------------------+
| 1 | test3 | jcr:createdBy | String | admin4 | 2016-03-31 20:30:27.240 |
+----+-------------+-----------------+--------------+------------------+-------------------------+
| 1 | test3 | jcr:createdBy | String | admin5 | 2016-03-31 20:30:27.240 |
+----+-------------+-----------------+--------------+------------------+-------------------------+
| 1 | test3 | jcr:createdBy | String | admin6 | 2016-03-31 20:30:27.240 |
+----+-------------+-----------------+--------------+------------------+-------------------------+
谢谢你的回复。但在这里,我想根据where条件中的sv:name读取sv:property标记中的sv:value节点值。例如sv:value=3fe558bd-d3aa-4c27-a975-f5ee8abc497f,其中sv:name=“jcr:uuid”Btw:
来自OPENXML,在前后调用SPs是绝对过时的,不应该再使用了……非常感谢您的帮助。它真的帮助了我:)。我还需要你的一点帮助。如果此模式中有一个节点:admin1 admin2 admin3 admin4 admin5 admin6,那么在这种情况下,Property.value('sv:value[1]”,'varchar(max')作为PropertyValue对我不起作用。简言之,我需要所有的值:admin1,admin2,admin3,admin4,admin5。选择Id、Property.value('sv:value[1]”、'varchar(max')作为PropertyValue,从ArticlesXML交叉应用ArticlesXML.ArticlesXMLData.nodes('/Articles/sv:node')A(Article)交叉应用A.Article.nodes('sv:Property')作为B(Property);上面的查询在这里不起作用。你能帮我吗?太酷了。你太棒了:)。非常感谢你的帮助。它确实帮助我解决了我的问题:)。再次非常感谢你的帮助。
CREATE TABLE ArticlesXML
(
Id INT IDENTITY PRIMARY KEY,
ArticlesXMLData XML,
LoadedDateTime DATETIME
)
GO
SET IDENTITY_INSERT ArticlesXML ON;
INSERT INTO ArticlesXML(Id,ArticlesXMLData,LoadedDateTime) VALUES
(1,
'<?xml version="1.0" encoding="UTF-8"?>
<Articles>
<sv:node sv:name="test1"
xmlns:sv="http://www.jcp.org/jcr/sv/1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<sv:property sv:name="jcr:primaryType" sv:type="Name">
<sv:value>mgnl:tax-article</sv:value>
</sv:property>
<sv:property sv:name="jcr:createdBy" sv:type="String">
<sv:value>system</sv:value>
</sv:property>
</sv:node>
<sv:node sv:name="test2"
xmlns:sv="http://www.jcp.org/jcr/sv/1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<sv:property sv:name="jcr:primaryType" sv:type="Name">
<sv:value>mgnl:tax-article</sv:value>
</sv:property>
<sv:property sv:name="jcr:createdBy" sv:type="String">
<sv:value>admin</sv:value>
</sv:property>
</sv:node>
</Articles>',GETDATE());
SET IDENTITY_INSERT ArticlesXML OFF;
WITH XMLNAMESPACES('http://www.jcp.org/jcr/sv/1.0' AS sv
,'http://www.w3.org/2001/XMLSchema-instance' AS xsi)
SELECT Id
,Article.value('@sv:name','varchar(max)') AS ArticleName
,Property.value('@sv:name','varchar(max)') AS PropertyName
,Property.value('@sv:type','varchar(max)') AS PropertyType
,Property.value('sv:value[1]','varchar(max)') AS PropertyValue
,LoadedDateTime
FROM ArticlesXML
CROSS APPLY ArticlesXML.ArticlesXMLData.nodes('/Articles/sv:node') A(Article)
CROSS APPLY A.Article.nodes('sv:property') AS B(Property);
--CleanUp
--DROP TABLE ArticlesXML;
+----+-------------+-----------------+--------------+------------------+-------------------------+
| Id | ArticleName | PropertyName | PropertyType | PropertyValue | LoadedDateTime |
+----+-------------+-----------------+--------------+------------------+-------------------------+
| 1 | test1 | jcr:primaryType | Name | mgnl:tax-article | 2016-03-31 13:52:26.753 |
+----+-------------+-----------------+--------------+------------------+-------------------------+
| 1 | test1 | jcr:createdBy | String | system | 2016-03-31 13:52:26.753 |
+----+-------------+-----------------+--------------+------------------+-------------------------+
| 1 | test2 | jcr:primaryType | Name | mgnl:tax-article | 2016-03-31 13:52:26.753 |
+----+-------------+-----------------+--------------+------------------+-------------------------+
| 1 | test2 | jcr:createdBy | String | admin | 2016-03-31 13:52:26.753 |
+----+-------------+-----------------+--------------+------------------+-------------------------+
WITH XMLNAMESPACES('http://www.jcp.org/jcr/sv/1.0' AS sv
,'http://www.w3.org/2001/XMLSchema-instance' AS xsi)
,TableDataCTE AS
(
SELECT Id
,Article.value('@sv:name','varchar(max)') AS ArticleName
,Property.value('@sv:name','varchar(max)') AS PropertyName
,Property.value('@sv:type','varchar(max)') AS PropertyType
,Property.value('sv:value[1]','varchar(max)') AS PropertyValue
,LoadedDateTime
FROM ArticlesXML
CROSS APPLY ArticlesXML.ArticlesXMLData.nodes('/Articles/sv:node') A(Article)
CROSS APPLY A.Article.nodes('sv:property') AS B(Property)
)
SELECT * FROM TableDataCTE;
WHERE ...
WITH XMLNAMESPACES('http://www.jcp.org/jcr/sv/1.0' AS sv
,'http://www.w3.org/2001/XMLSchema-instance' AS xsi)
SELECT Id
,Article.value('@sv:name','varchar(max)') AS ArticleName
,Property.value('@sv:name','varchar(max)') AS PropertyName
,Property.value('@sv:type','varchar(max)') AS PropertyType
,Value.value('.','varchar(max)') AS PropertyValue
,LoadedDateTime
FROM ArticlesXML
CROSS APPLY ArticlesXML.ArticlesXMLData.nodes('/Articles/sv:node') A(Article)
CROSS APPLY A.Article.nodes('sv:property') AS B(Property)
CROSS APPLY B.Property.nodes('sv:value') AS C(Value);
+----+-------------+-----------------+--------------+------------------+-------------------------+
| Id | ArticleName | PropertyName | PropertyType | PropertyValue | LoadedDateTime |
+----+-------------+-----------------+--------------+------------------+-------------------------+
| 1 | test1 | jcr:primaryType | Name | mgnl:tax-article | 2016-03-31 20:30:27.240 |
+----+-------------+-----------------+--------------+------------------+-------------------------+
| 1 | test1 | jcr:createdBy | String | system | 2016-03-31 20:30:27.240 |
+----+-------------+-----------------+--------------+------------------+-------------------------+
| 1 | test2 | jcr:primaryType | Name | mgnl:tax-article | 2016-03-31 20:30:27.240 |
+----+-------------+-----------------+--------------+------------------+-------------------------+
| 1 | test2 | jcr:createdBy | String | admin | 2016-03-31 20:30:27.240 |
+----+-------------+-----------------+--------------+------------------+-------------------------+
| 1 | test3 | jcr:createdBy | String | admin1 | 2016-03-31 20:30:27.240 |
+----+-------------+-----------------+--------------+------------------+-------------------------+
| 1 | test3 | jcr:createdBy | String | admin2 | 2016-03-31 20:30:27.240 |
+----+-------------+-----------------+--------------+------------------+-------------------------+
| 1 | test3 | jcr:createdBy | String | admin3 | 2016-03-31 20:30:27.240 |
+----+-------------+-----------------+--------------+------------------+-------------------------+
| 1 | test3 | jcr:createdBy | String | admin4 | 2016-03-31 20:30:27.240 |
+----+-------------+-----------------+--------------+------------------+-------------------------+
| 1 | test3 | jcr:createdBy | String | admin5 | 2016-03-31 20:30:27.240 |
+----+-------------+-----------------+--------------+------------------+-------------------------+
| 1 | test3 | jcr:createdBy | String | admin6 | 2016-03-31 20:30:27.240 |
+----+-------------+-----------------+--------------+------------------+-------------------------+