【发布时间】:2018-01-03 10:14:45
【问题描述】:
我在玩 SQL 和 XML。
我的数据库中有一个列 (properties),其中存储了 XML。
此 XML 的示例如下所示:
<root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://www.xyz.de/activerepository/fileprops">
<props>
<prop ns="ARM:" elem="_NoFilter">
<value xsi:type="xsd:boolean">true</value>
</prop>
<prop ns="DAV:" elem="displayname">
<value xsi:type="xsd:string">Some text in it</value>
</prop>
<prop ns="DAV:" elem="getcontenttype">
<value xsi:type="xsd:string">message/rfc822</value>
</prop>
<prop ns="DAV:" elem="creationdate">
<value xsi:type="xsd:dateTime">2017-01-02T09:38:28.1278078</value>
</prop>
</props>
</root>
我已经写了一个查询,在这里我得到了这个 XML 结构的所有属性。
;WITH XMLNAMESPACES(DEFAULT 'http://www.xyz.de/activerepository/fileprops')
SELECT
cast(properties as xml).query('root/props/prop'),
*
FROM tm_cas_files (nolock)
where id = 'A7ED7D99-8CDB-47F6-8EF5-18E7FCB97F28'
这个查询工作正常,但我得到了 XML 结构的所有节点。
此查询的结果如下所示:
<p1:prop xmlns:p1="http://www.xyz.de/activerepository/fileprops" ns="ARM:" elem="_NoFilter">
<p1:value xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="xsd:boolean">true</p1:value>
</p1:prop>
<p2:prop xmlns:p2="http://www.xyz.de/activerepository/fileprops" ns="DAV:" elem="displayname">
<p2:value xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="xsd:string">Some text in it</p2:value>
</p2:prop>
<p3:prop xmlns:p3="http://www.xyz.de/activerepository/fileprops" ns="DAV:" elem="getcontenttype">
<p3:value xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="xsd:string">message/rfc822</p3:value>
</p3:prop>
<p4:prop xmlns:p4="http://www.xyz.de/activerepository/fileprops" ns="DAV:" elem="creationdate">
<p4:value xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="xsd:dateTime">2017-01-02T09:38:28.1278078</p4:value>
</p4:prop>
我现在如何才能只获取此 XML 的特定节点?
我已经测试过这个查询(获取即“_NoFilter”节点),但是这个 SQL 查询返回 no / empty 结果:
;WITH XMLNAMESPACES(DEFAULT 'http://www.xyz.de/activerepository/fileprops')
SELECT
cast(properties as xml).query('root/props/prop[elem="_NoFilter"]'),
*
FROM tm_cas_files (nolock)
where id = 'A7ED7D99-8CDB-47F6-8EF5-18E7FCB97F28'
感谢您的帮助!
【问题讨论】:
-
嗯,为什么我的问题被标记为重复?我已经看到了这个链接的答案,但它没有帮助。所以请解释一下,为什么这是重复的。谢谢
-
@Tomalak 这与您提供的链接无关。所以,我投票决定重新打开它,因为它是不同的。
-
@YogeshSharma 你是对的。重新打开。
-
感谢您重新打开!
标签: sql sql-server xml tsql