鉴于下面的XML结构,我需要过滤掉question
值等于<questionSubType/>
且ABC
属性等于{的所有<option subType=""/>
节点{1}}:
001
我尝试了以下操作,但是由于我不确定如何继续查询<questions>
<question>
<text>Some text</text>
<questionType></questionType>
<questionSubType>ABC</questionSubType>
<options>
<option subType="001">
<text>Y</text>
<mappedCodes>
<code>1</code>
</mappedCodes>
</option>
<option subType="001">
<text>N</text>
<mappedCodes>
<code>2</code>
</mappedCodes>
</option>
<option subType="002">
<text>Y</text>
<mappedCodes>
<code>1</code>
</mappedCodes>
</option>
</options>
</question>
<question>
<text>Some more text</text>
<questionType></questionType>
<questionSubType>DEF</questionSubType>
<options>
<option subType="001">
<text>Single</text>
<mappedCodes>
<code>PL0157</code>
</mappedCodes>
</option>
<option subType="001">
<text>Married</text>
<mappedCodes>
<code>PD0241</code>
</mappedCodes>
</option>
<option subType="002">
<text>Single</text>
<mappedCodes>
<code>PL1157</code>
</mappedCodes>
</option>
<option subType="002">
<text>Married</text>
<mappedCodes>
<code>PD1241</code>
</mappedCodes>
</option>
</options>
</question>
<question>
<text>Some last text</text>
<questionType></questionType>
<questionSubType>ABC</questionSubType>
<options>
<option subType="001">
<text>T</text>
<mappedCodes>
<code>2</code>
</mappedCodes>
</option>
<option subType="002">
<text>V</text>
<mappedCodes>
<code>2</code>
</mappedCodes>
</option>
</options>
</question>
</questions>
节点,因此这仅基于<questionSubType/>
值过滤XML:
<option/>
因此,最后,我的输出应如下所示:
DECLARE
@subType varchar(5) = '001'
, @questionSubType varchar(5) = 'ABC'
SET @XmlOutput = (
SELECT
1 as Tag
, null as Parent
, CONVERT(nvarchar(max), F.N.query('./*')) as [question!1!!XML]
FROM [MyTable] T
CROSS APPLY T.[Configuration].nodes('//question') F(N)
WHERE
F.N.value('(//questionSubType/text())[1]', 'varchar(100)') = @questionSubType
FOR XML EXPLICIT, ROOT('questions')
)
SELECT @XmlOutput as [Configuration]
任何帮助将不胜感激。
答案 0 :(得分:1)
这里XQuery
是您的救命之路:
DECLARE @xml XML=
N'<questions>
<question>
<text>Some text</text>
<questionType></questionType>
<questionSubType>ABC</questionSubType>
<options>
<option subType="001">
<text>Y</text>
<mappedCodes>
<code>1</code>
</mappedCodes>
</option>
<option subType="001">
<text>N</text>
<mappedCodes>
<code>2</code>
</mappedCodes>
</option>
<option subType="002">
<text>Y</text>
<mappedCodes>
<code>1</code>
</mappedCodes>
</option>
</options>
</question>
<question>
<text>Some more text</text>
<questionType></questionType>
<questionSubType>DEF</questionSubType>
<options>
<option subType="001">
<text>Single</text>
<mappedCodes>
<code>PL0157</code>
</mappedCodes>
</option>
<option subType="001">
<text>Married</text>
<mappedCodes>
<code>PD0241</code>
</mappedCodes>
</option>
<option subType="002">
<text>Single</text>
<mappedCodes>
<code>PL1157</code>
</mappedCodes>
</option>
<option subType="002">
<text>Married</text>
<mappedCodes>
<code>PD1241</code>
</mappedCodes>
</option>
</options>
</question>
<question>
<text>Some last text</text>
<questionType></questionType>
<questionSubType>ABC</questionSubType>
<options>
<option subType="001">
<text>T</text>
<mappedCodes>
<code>2</code>
</mappedCodes>
</option>
<option subType="002">
<text>V</text>
<mappedCodes>
<code>2</code>
</mappedCodes>
</option>
</options>
</question>
</questions>';
-声明变量
DECLARE @subType varchar(5) = '001'
,@questionSubType varchar(5) = 'ABC';
-XQuery
将遍历您的XML,并添加具有给定类型的所有问题,然后添加除<options>
之外的所有内部节点。最后一个节点再加上一个过滤谓词:
SELECT @xml.query
('<questions>
{
for $q in /questions/question[(questionSubType/text())[1]=sql:variable("@questionSubType")]
return
<question>
{
$q/*[local-name()!="options"]
}
{
$q/options/option[@subType=sql:variable("@subType")]
}
</question>
}
</questions>
');