如何使用条件从 table 中的 XML 列中选择一个值。 XML 表单包含不同的页面
How do I pick a value from XML column in a table with a condition. XML form contains different page
我有一个 xml 数据列。 XML 数据包含具有不同页眉的表单。每个页面都有无线电类型值。我需要从一页及其值中选择值。
我需要页面主题='Outreach'中的值'id',问题值="Type of Outreach",因为我需要 id="1" 值,而且我还需要来自 Question value="Reason for Assessment" 的 id 值。
下面是示例 xml 数据。
<Test name="Assessment " header="" logoimg="" background="" icon="" firstPage="1" allow_abort="1" is_um="0" is_hidden="0" do_only="1" status="1" locale_id="0" version="74" category_id="150011" maxQuestionID="37" id="10234" force_start="0" user_name=" ggg bbb">
<PAGE topic="Outreach " progress="5" background="" headerid="" footerid="" flush_concepts="1" lastPage="false" id="1">
<BRANCH_CONDITION>
<NEXT_PAGE id="2" />
</BRANCH_CONDITION>
<QUESTION value="Type of Outreach " defaultvalue="" style="" tooltip="" read_only="0" usability="5" prefer_concept="0" type="radio" validation="0" id="1" pagename="Outreach ">
<OPTIONS>
<OPTION value="Outbound Telephone " id="1" url="" score="0" />
<OPTION value="Inbound Telephone" id="2" url="" score="0" />
<OPTION value="In Person" id="3" url="" score="0" />
</OPTIONS>
</QUESTION>
<QUESTION value="Reason for Assessment " defaultvalue="" style="" tooltip="" read_only="0" usability="5" prefer_concept="0" type="radio" validation="0" id="2" pagename="Outreach ">
<OPTIONS>
<OPTION value="New" id="1" url="" score="0" />
<OPTION value="Annual" id="2" url="" score="0" />
<OPTION value="Triggering Event " id="3" url="" score="0" />
<OPTION value="LOC Review" id="4" url="" score="0" />
</OPTIONS>
</QUESTION>
我尝试了以下 sql。
SELECT TABLE.META_DATA.value('(/QUESTION//id/node())[1]','int') as outReach
,TABLE.META_DATA.value('(/QUESTION//id/node())[1]','int') as ReasonForAssessment
FROM Table
以下是我的预期结果
+----------+---------------------+
| Outreach | ReasonForAssessment |
+----------+---------------------+
| 1 | 2 |
几个问题:
QUESTION
不是 XML 的根,因此您必须使用 //
或从根向下遍历树:/Test/PAGE/QUESTION/
。
调整 XPath 以按带有 @
前缀的属性搜索而不是节点搜索,因为 id
是 QUESTION
的属性(不是子节点)。
调整节点索引[#]
为return对应的@id
属性值
调整后SQL:
SELECT TABLE.META_DATA.value('(//QUESTION/@id)[1]','int') as outReach
, TABLE.META_DATA.value('(//QUESTION/@id)[2]','int') as ReasonForAssessment
FROM Table;
-- outReach ReasonForAssessment
-- 1 2
或者对列使用方括号条件:
SELECT TABLE.META_DATA.value('(/Test/PAGE/QUESTION[@value=''Type of Outreach '']/@id)[1]','int') as outReach
, TABLE.META_DATA.value('(/Test/PAGE/QUESTION[@value=''Reason for Assessment '']/@id)[1]','int') as ReasonForAssessment
FROM Table;
-- outReach ReasonForAssessment
-- 1 2
我有一个 xml 数据列。 XML 数据包含具有不同页眉的表单。每个页面都有无线电类型值。我需要从一页及其值中选择值。
我需要页面主题='Outreach'中的值'id',问题值="Type of Outreach",因为我需要 id="1" 值,而且我还需要来自 Question value="Reason for Assessment" 的 id 值。
下面是示例 xml 数据。
<Test name="Assessment " header="" logoimg="" background="" icon="" firstPage="1" allow_abort="1" is_um="0" is_hidden="0" do_only="1" status="1" locale_id="0" version="74" category_id="150011" maxQuestionID="37" id="10234" force_start="0" user_name=" ggg bbb">
<PAGE topic="Outreach " progress="5" background="" headerid="" footerid="" flush_concepts="1" lastPage="false" id="1">
<BRANCH_CONDITION>
<NEXT_PAGE id="2" />
</BRANCH_CONDITION>
<QUESTION value="Type of Outreach " defaultvalue="" style="" tooltip="" read_only="0" usability="5" prefer_concept="0" type="radio" validation="0" id="1" pagename="Outreach ">
<OPTIONS>
<OPTION value="Outbound Telephone " id="1" url="" score="0" />
<OPTION value="Inbound Telephone" id="2" url="" score="0" />
<OPTION value="In Person" id="3" url="" score="0" />
</OPTIONS>
</QUESTION>
<QUESTION value="Reason for Assessment " defaultvalue="" style="" tooltip="" read_only="0" usability="5" prefer_concept="0" type="radio" validation="0" id="2" pagename="Outreach ">
<OPTIONS>
<OPTION value="New" id="1" url="" score="0" />
<OPTION value="Annual" id="2" url="" score="0" />
<OPTION value="Triggering Event " id="3" url="" score="0" />
<OPTION value="LOC Review" id="4" url="" score="0" />
</OPTIONS>
</QUESTION>
我尝试了以下 sql。
SELECT TABLE.META_DATA.value('(/QUESTION//id/node())[1]','int') as outReach
,TABLE.META_DATA.value('(/QUESTION//id/node())[1]','int') as ReasonForAssessment
FROM Table
以下是我的预期结果
+----------+---------------------+
| Outreach | ReasonForAssessment |
+----------+---------------------+
| 1 | 2 |
几个问题:
QUESTION
不是 XML 的根,因此您必须使用//
或从根向下遍历树:/Test/PAGE/QUESTION/
。调整 XPath 以按带有
@
前缀的属性搜索而不是节点搜索,因为id
是QUESTION
的属性(不是子节点)。调整节点索引
[#]
为return对应的@id
属性值
调整后SQL:
SELECT TABLE.META_DATA.value('(//QUESTION/@id)[1]','int') as outReach
, TABLE.META_DATA.value('(//QUESTION/@id)[2]','int') as ReasonForAssessment
FROM Table;
-- outReach ReasonForAssessment
-- 1 2
或者对列使用方括号条件:
SELECT TABLE.META_DATA.value('(/Test/PAGE/QUESTION[@value=''Type of Outreach '']/@id)[1]','int') as outReach
, TABLE.META_DATA.value('(/Test/PAGE/QUESTION[@value=''Reason for Assessment '']/@id)[1]','int') as ReasonForAssessment
FROM Table;
-- outReach ReasonForAssessment
-- 1 2