Query XML data for information based on a sibling value(根据同级值查询 XML 数据以获取信息)
问题描述
我有以下 XML 数据,我对其进行零控制.请注意,它基本上是属性组的集合.我需要选择一个属性的值,其中另一个属性的值为true".问题是,没有什么可分组的,我不知道如何正确关联.
I have the following XML data, which I have zero control over. Note that it's basically a collection of property groups. I need to select the value of one property where the value of another property is 'true'. The problem is, there's nothing to group on, and I cannot figure out how to associate things correctly.
这是 XML 数据,以及我目前想到的查询:
Here's the XML Data, and the query I came up with so far:
DECLARE @xml xml = '
<Container>
<Collection>
<ItemName>SomeItem</ItemName>
<IsDeletable>true</IsDeletable>
<IsPersisted>false</IsPersisted>
</Collection>
<Collection>
<ItemName>AnotherItem</ItemName>
<IsDeletable>true</IsDeletable>
<IsPersisted>true</IsPersisted>
<ExistsInDB>true</ExistsInDB>
</Collection>
<Collection>
<ItemName>ItemFoo</ItemName>
<IsDeletable>true</IsDeletable>
<IsPersisted>true</IsPersisted>
<ExistsInDB>true</ExistsInDB>
</Collection>
<Collection>
<ItemName>BarBazItem</ItemName>
<IsDeletable>true</IsDeletable>
<IsPersisted>true</IsPersisted>
<ExistsInDB>false</ExistsInDB>
</Collection>
</Container>
'
;WITH XmlStuff AS (
SELECT CAST(xmlShredded.colXmlItem.query('local-name(.)') AS nvarchar(4000)) as XmlNodeName,
xmlShredded.colXmlItem.value('.', 'nvarchar(4000)') AS XmlNodeValue
FROM @xml.nodes('/*/Collection/child::node()') as xmlShredded(colXmlItem)
)
SELECT *
FROM XmlStuff
现在,我需要做的是为ExistsInDB"为true"的每个分组获取ItemName"值.请注意,第一个属性集合中不存在ExistsInDB"属性(即,应将其视为 NULL/false).
Now, what I need to do, is get the "ItemName" value for each grouping where "ExistsInDB" is 'true'. Note that the "ExistsInDB" property doesn't exist in the first property collection (i.e. it should be considered NULL/false).
所以在这种情况下,我需要查询这个xml数据,并取回以下集合:
So in this case, I need to query this xml data, and get back the following set:
另一个项目ItemFoo
AnotherItem ItemFoo
我不应该得到SomeItem"或BarBazItem".
I should NOT get "SomeItem" or "BarBazItem".
我一直在努力弄清楚如何制定获取所有 ItemName 值,其中关联的 ExistsInDB 值都存在且为真"的查询.
I've been beating my head against the desk trying to figure out how to formulate the query for "Get all ItemName values where the associated ExistsInDB value is both present, and true".
这甚至可能吗?
推荐答案
也许试试兄弟匹配
/Container/Collection/ItemName[../ExistsInDB='true']
获取 ItemName 元素,其父元素包含等于true"的 ExistsInDb 子元素.
That gets ItemName elements whose parents contain an ExistsInDb child equal to "true".
DECLARE @xml xml = '
<Container>
<Collection>
<ItemName>SomeItem</ItemName>
<IsDeletable>true</IsDeletable>
<IsPersisted>false</IsPersisted>
</Collection>
<Collection>
<ItemName>AnotherItem</ItemName>
<IsDeletable>true</IsDeletable>
<IsPersisted>true</IsPersisted>
<ExistsInDB>true</ExistsInDB>
</Collection>
<Collection>
<ItemName>ItemFoo</ItemName>
<IsDeletable>true</IsDeletable>
<IsPersisted>true</IsPersisted>
<ExistsInDB>true</ExistsInDB>
</Collection>
<Collection>
<ItemName>BarBazItem</ItemName>
<IsDeletable>true</IsDeletable>
<IsPersisted>true</IsPersisted>
<ExistsInDB>false</ExistsInDB>
</Collection>
</Container>
';
SELECT node.value('.', 'nvarchar(100)')
FROM @xml.nodes('/Container/Collection/ItemName[../ExistsInDB="true"]') AS x(node)
这篇关于根据同级值查询 XML 数据以获取信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:根据同级值查询 XML 数据以获取信息


- 以一个值为轴心,但将一行上的数据按另一行分组? 2022-01-01
- 使用 Oracle PL/SQL developer 生成测试数据 2021-01-01
- 如何使用 pip 安装 Python MySQLdb 模块? 2021-01-01
- 如何将 Byte[] 插入 SQL Server VARBINARY 列 2021-01-01
- 更改自动增量起始编号? 2021-01-01
- SQL 临时表问题 2022-01-01
- 在SQL中,如何为每个组选择前2行 2021-01-01
- 远程 mySQL 连接抛出“无法使用旧的不安全身份验证连接到 MySQL 4.1+"来自 XAMPP 的错误 2022-01-01
- 导入具有可变标题的 Excel 文件 2021-01-01
- 如何将 SonarQube 6.7 从 MySQL 迁移到 postgresql 2022-01-01