我有一个包含XML列的表,我的数据如下所示:
<Entries>
<Entry Default="No" ValueId="1" Sequence="1" Deleted="Yes"><Undefined></Entry>
<Entry Default="No" ValueId="2" Sequence="1">Non-Purulent</Entry>
<Entry Default="No" ValueId="4" Sequence="2" Deleted="Yes">Sanguinous</Entry>
<Entry Default="No" ValueId="11" Sequence="2">Odor Present</Entry>
<Entry Default="No" ValueId="3" Sequence="3">Purulent</Entry>
<Entry Default="No" ValueId="5" Sequence="3" Deleted="Yes">Serosangiunous</Entry>
<Entry Default="No" ValueId="6" Sequence="4">Sanguineous</Entry>
<Entry Default="No" ValueId="7" Sequence="4" Deleted="Yes">Serosanguineous</Entry>
<Entry Default="No" ValueId="8" Sequence="5">Serous</Entry>
<Entry Default="No" ValueId="10" Sequence="6">Sero-sanguineous</Entry>
<Entry Default="No" ValueId="9" Sequence="7">Other</Entry>
</Entries>
我想检索没有Deleted =“Yes”属性的值。我对使用XML查询非常陌生,所以我将非常感谢我能得到的所有帮助。
答案 0 :(得分:1)
如果您正在撰写SQL
作为标记,并且您的意思是 SQL Server - 那么您可以使用类似的内容(假设您的表名为YourTable
持有XML的列称为XmlColumn
- 请适应根据您的实际情况!):
-- define a CTE (Common Table Expression) and extract detail info from XML
;WITH XmlCte AS
(
SELECT
ID,
XmlText = XC.value('(.)[1]', 'varchar(20)'),
XmlValueId = XC.value('(@ValueId)[1]', 'int'),
XmlSequence = XC.value('(@Sequence)[1]', 'int'),
XmlDeleted = XC.value('(@Deleted)[1]', 'varchar(10)')
FROM dbo.YourTable
CROSS APPLY XmlColumn.nodes('/Entries/Entry') AS XT(XC)
)
SELECT
XmlValueId,
XmlSequence,
XmlText
FROM XmlCte
WHERE XmlDeleted IS NULL
答案 1 :(得分:0)
根据marc_s
,请标记您的RDBMS
关于你的意思是SQL Server,这是使用xquery
执行此操作的一种方法SELECT
Nodes.node.value('@Sequence', 'varchar(50)') AS Sequence,
Nodes.node.value('(./text())[1]', 'varchar(50)') AS Value
FROM
MyTable xyz
cross apply xyz.XmlColumn.nodes('/Entries/Entry[not(@Deleted="Yes")]') as Nodes(node);
请注意,您需要将xml定义为<Undefined>
。将需要添加到CDATA
部分,或者您需要关闭代码。