【问题标题】:How to filter XML data based on an attribute using XQuery如何使用 XQuery 根据属性过滤 XML 数据
【发布时间】:2018-08-16 12:55:11
【问题描述】:

给定下面的XML结构,我需要过滤掉所有<questionSubType/>值等于ABC,并且<option subType=""/>属性等于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>

我尝试了以下操作,但这仅根据 &lt;questionSubType/&gt; 值过滤 XML,因为我不确定如何继续查询 &lt;option/&gt; 节点:

        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]

所以最后,我的输出应该是这样的:

<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>
    </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>
    </options>
  </question>
 </questions>

任何帮助将不胜感激。

【问题讨论】:

    标签: tsql xpath xquery sql-server-2014-express flwor


    【解决方案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 并添加具有给定类型的所有问题,然后添加除 &lt;options&gt; 之外的所有内部节点。最后一个节点使用过滤谓词再次添加:

     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> 
     ');
    

    【讨论】:

    • 啊,如果我没记错的话,是 FLWOR 表达式吗?像魅力一样工作
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-15
    • 2010-10-06
    相关资源
    最近更新 更多