我正在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查询返回否/空结果:
;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'
谢谢你的帮助!
如果只需要该_NoFilter
节点,则可以使用XPath仅选择该节点。
/root/props/prop[@elem="_NoFilter"]
甚至
/root/props/prop[@elem="_NoFilter"]/value
速记:
//prop[@elem="_NoFilter"]/value
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句