如何从XML文件中提取元素?

时间:2010-12-02 15:15:12

标签: sql-server tsql

我有以下简单的XML片段。如果我想提取'EntityFriendlyName'属性,T-SQL会是什么样子?

<SimpleRuleValue xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <EntityId>0</EntityId>
    <EntityFriendlyName>ALine</EntityFriendlyName>
</SimpleRuleValue>

2 个答案:

答案 0 :(得分:3)

这将是这样的:

SELECT CAST(X.SomeXML AS XML).value('(SimpleRuleValue/EntityFriendlyName)[1]','nvarchar(1000)') AS EntityFriendlyName
FROM XmlSource X

编辑: A little more information.

答案 1 :(得分:0)

这是一种方式:

declare @x xml

set @x = '<SimpleRuleValue xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
              <EntityId>0</EntityId>
              <EntityFriendlyName>ALine</EntityFriendlyName>
          </SimpleRuleValue>'

select @x.query('data(SimpleRuleValue/EntityFriendlyName)')