简体   繁体   English

SQL Server XML:如何返回具有相同名称的所有元素?

[英]SQL Server XML: How To Return All Elements With The Same Name?

I have data stored in an SQL Server XML column. 我将数据存储在SQL Server XML列中。 The elements in each row are not always in the same order but I need to return all elements that have a certain name. 每行中的元素并不总是相同的顺序,但是我需要返回所有具有特定名称的元素。 Given the example below, how would I return all elements named 'apple'? 给定以下示例,我将如何返回所有名为“ apple”的元素?

<!-- Row 1 -->
<store id="3">
    <apple type="tasty" />
    <orange color="grape" />
</store>

<!-- Row 2 -->
<house id="14">
    <banana condition="rotten" />
    <apple type="fuji" />
</house>

<!-- Row 3 -->
<apple>

<!-- Row 4 -->
<country id="3">
    <state id="14">
        <apple type="GSmith" />
    </state>
</country>

Use the "//elementName" syntax to find an element called "elementName" regardless of where it is in the hierarchy. 使用“ // elementName”语法查找一个名为“ elementName”的元素,而不管其在层次结构中的位置如何。

declare @x xml = '
<x>
<store id="3">
    <apple type="tasty" />
    <orange color="grape" />
</store>

<!-- Row 2 -->
<house id="14">
    <banana condition="rotten" />
    <apple type="fuji" />
</house>

<!-- Row 3 -->
<apple/>

<!-- Row 4 -->
<country id="3">
    <state id="14">
        <apple type="GSmith" />
    </state>
</country>
</x>'

select apple.query('.')
from @x.nodes('//apple') as x(apple)

NB: I had to modify your example XML slightly to make it valid. 注意:我必须稍微修改您的示例XML以使其有效。 Namely providing a top-level element for everything to nest under and making the naked into (so the tag is closed). 即为所有要嵌套的东西提供裸露的顶层元素(使标签封闭)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 检索SQL Server中具有相同前缀的所有XML元素 - Retrieve all XML elements with the same prefix in SQL Server 如何使用sql从同一级别的所有xml元素中选择属性 - How to select an attribute from all xml elements on the same level with sql SQL Server:按元素名称计数的XML元素 - SQL Server : Count of XML Elements By Element Name 当多个节点/元素具有相同名称时,SQL Server在表中查询值的多个XML记录 - SQL Server, query multiple XML records in a table for a value, when multiple nodes/elements have the same name 按属性过滤 SQL Server XML 并返回元素的所有匹配属性值 - Filter SQL Server XML by Attribute and return all matching attribute value for elements 如果所有元素都具有相同的名称,如何从XML检索数据? - How can I retrieve data from XML if all elements have the same name? SQL / XPATH - 如何将具有相同名称的所有可能元素选择到单独的行/列中 - SQL / XPATH - How to select all possible elements that have the same name into separate rows/columns 从SQL Server中具有相同标签名称的XML提取值 - Extract Value from XML having same tag name in SQL Server 生成相同名称节点的SQL Server FOR XML显式错误 - SQL Server FOR XML explicit error in generating same name nodes SQL Server 2005 “FOR XML PATH” 多个同名标签 - SQL Server 2005 “FOR XML PATH” Multiple tags with same name
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM