sql xml 查询指定节点值,以及节点属性值

SELECT   top 10  [HFMBDATA].query('(/Items/Item[@ID=''tbBryj''])').value('(//TextValue)[1]','nvarchar(max)') as mz,

[HFMBDATA].value('(Items/Item[@ID="rb_fzhd_y"]/@ControlType)[1]','varchar(50)')   FROM [EMRCP].[dbo].[T_ZYBR_HF] as at

获取结果:

mz        at
无         CheckBox.RadioBox

 

示例XML(HFMBDATA)

<Items>
  <Item ID="rb_cyhhjzk_h" Name="cyhhjzk" ControlType="CheckBox.RadioBox" Value="" Checked="True">
    <TextValue>好</TextValue>
  </Item>
  <Item ID="rb_cyhhjzk_yb" Name="cyhhjzk" ControlType="CheckBox.RadioBox" Value="" Checked="False">
    <TextValue>一般</TextValue>
  </Item>
  <Item ID="rb_cyhhjzk_bh" Name="cyhhjzk" ControlType="CheckBox.RadioBox" Value="" Checked="False">
    <TextValue>不好</TextValue>
  </Item>
  <Item ID="rb_sy_h" Name="sy" ControlType="CheckBox.RadioBox" Value="" Checked="True">
    <TextValue>好</TextValue>
  </Item>
  <Item ID="rb_sy_yb" Name="sy" ControlType="CheckBox.RadioBox" Value="" Checked="False">
    <TextValue>一般</TextValue>
  </Item>
  <Item ID="rb_sy_bh" Name="sy" ControlType="CheckBox.RadioBox" Value="" Checked="False">
    <TextValue>不好</TextValue>
  </Item>
  <Item ID="rb_asfy_y" Name="asfy" ControlType="CheckBox.RadioBox" Value="" Checked="False">
    <TextValue>有</TextValue>
  </Item>
  <Item ID="rb_asfy_my" Name="asfy" ControlType="CheckBox.RadioBox" Value="" Checked="False">
    <TextValue>没有</TextValue>
  </Item>
  <Item ID="rb_cxzz_y" Name="cxzz" ControlType="CheckBox.RadioBox" Value="" Checked="False">
    <TextValue>有</TextValue>
  </Item>
  <Item ID="rb_cxzz_my" Name="cxzz" ControlType="CheckBox.RadioBox" Value="" Checked="False">
    <TextValue>没有</TextValue>
  </Item>
  <Item ID="tbBryj" Name="" ControlType="InputField.Text" Value="">
    <TextValue>无</TextValue>
    <Items />
  </Item>
  <Item ID="tb_hfryyj" Name="" ControlType="InputField.Text" Value="">
    <TextValue>无</TextValue>
    <Items />
  </Item>
  <Item ID="tb_yshfyj" Name="" ControlType="InputField.Text" Value="">
    <TextValue>无</TextValue>
    <Items />
  </Item>
</Items>

原文地址:https://www.cnblogs.com/chuhaida/p/4586200.html