Xquery:在t-sql中使用键值对查询xml

Xquery:在t-sql中使用键值对查询xml,xml,tsql,xquery,xquery-sql,xquery-update,Xml,Tsql,Xquery,Xquery Sql,Xquery Update,在下面的代码块中,我可以查询数据元素。 对于以下xml DECLARE @xmlString XML; SELECT @xmlString='<Activity xmlns:i="http://www.w3.org/2001/XMLSchema-instance"> <ActivityCode>570</ActivityCode> <ActivityId>0011d966-fa28-440c-9196-5dbe3c40b2ac</Ac

在下面的代码块中,我可以查询数据元素。 对于以下xml

DECLARE @xmlString XML;
SELECT @xmlString='<Activity xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
  <ActivityCode>570</ActivityCode>
  <ActivityId>0011d966-fa28-440c-9196-5dbe3c40b2ac</ActivityId>
  <CreateDateTime>2014-06-19T06:57:46.9854126-04:00</CreateDateTime>
  <DataElements xmlns:a="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
    <a:KeyValueOfstringDataElementLjh4bohd>
      <a:Key>AgentId</a:Key>
      <a:Value>
        <DataType>String</DataType>
        <Name>AgentId</Name>
        <Value>abc\xyxaa</Value>
      </a:Value>
    </a:KeyValueOfstringDataElementLjh4bohd>
    <a:KeyValueOfstringDataElementLjh4bohd>
      <a:Key>PhoneCallStartTime</a:Key>
      <a:Value>
        <DataType>String</DataType>
        <Name>PhoneCallStartTime</Name>
        <Value>06/19/2014 10:57:47</Value>
      </a:Value>
    </a:KeyValueOfstringDataElementLjh4bohd>
  </DataElements>
</Activity
给我的结果是:

AgentIdStringAgentIdabc\xyxaaPhoneCallStartTimeStringPhoneCallStartTime06/19/2014 10:57:47

但我想要的是,如果我通过AgentId,我应该获得abc\xyxaa,如果我通过PhoneCallStartTime,我应该获得2014年6月19日10:57:47。请帮助使用谓词和轴步骤来选择和筛选结果。与您使用的代码类似:

for $activity in /Activity
return data($activity/DataElements//a:Value[Name="PhoneCallStartTime"]/Value)
这里涉及一个名称空间。你要么必须

  • 声明命名空间
    a=”http://schemas.microsoft.com/2003/10/Serialization/Arrays
  • 忽略元素的名称空间,并对
    元素使用通配符名称空间运算符
    *:
    ,而不是
    a:
for $activity in /Activity
return data($activity/DataElements//a:Value[Name="PhoneCallStartTime"]/Value)