T-SQL查找具有匹配文本的节点,并从XML字段中的同级节点中提取值

时间:2018-12-20 14:36:31

标签: sql-server xml tsql xpath xml-namespaces

我有一个带有xml字段的表,其内容如下:

<ContentBlock xmlns="http://corp.com/wdpr/ContentBlock">
<key xlink:href="tcm:0-133036" xlink:title="entityCard" xlink:type="simple" xmlns:xlink="http://www.w3.org/1999/xlink"/>
<template xlink:href="tcm:0-132970" xlink:title="card-header-read-more-all-media" xlink:type="simple" xmlns:xlink="http://www.w3.org/1999/xlink"/>
<section>
    <name xlink:href="tcm:0-132988" xlink:title="header" xlink:type="simple" xmlns:xlink="http://www.w3.org/1999/xlink"/>
    <content>
        <p xmlns="http://www.w3.org/1999/xhtml">Make a Friend</p>
    </content>
</section>
<section>
    <name xlink:href="tcm:0-133110" xlink:title="readMore" xlink:type="simple" xmlns:xlink="http://www.w3.org/1999/xlink"/>
    <content>
        <p xmlns="http://www.w3.org/1999/xhtml">Meet and greet friendly animals at the only petting zoo in Disney’s Animal
        Kingdom park.
     </p>
    </content>
</section>
<section>
    <name xlink:href="tcm:0-132939" xlink:title="readAll" xlink:type="simple" xmlns:xlink="http://www.w3.org/1999/xlink"/>
    <content>
        <p xmlns="http://www.w3.org/1999/xhtml">Meet and greet friendly animals.
     </p>
        <p xmlns="http://www.w3.org/1999/xhtml">Pet, brush and feed domesticated creatures
     </p>
        <ul xmlns="http://www.w3.org/1999/xhtml">
            <li xmlns="http://www.w3.org/1999/xhtml">Goats</li>
        </ul>
        <p xmlns="http://www.w3.org/1999/xhtml">Handy animal brushes are available .
     </p>
        <p xmlns="http://www.w3.org/1999/xhtml">
            <strong xmlns="http://www.w3.org/1999/xhtml">Keeping Clean</strong>
            <br xmlns="http://www.w3.org/1999/xhtml"/>Guests are encouraged to cleanse.</p>
    </content>
</section>
<media>
    <name xlink:href="tcm:0-201994" xlink:title="media" xlink:type="simple" xmlns:xlink="http://www.w3.org/1999/xlink"/>
    <key xlink:href="tcm:0-132952" xlink:title="170 x 96" xlink:type="simple" xmlns:xlink="http://www.w3.org/1999/xlink"/>
    <image xlink:href="tcm:0-231377" xlink:title="section-01.jpg" xlink:type="simple" xmlns:xlink="http://www.w3.org/1999/xlink"/>
</media>
</ContentBlock>

我的目标是要进行一个查询,在其中可以找到(例如)包含文本“结交朋友”的<p>,然后在其中找到<name>元素的xlink:href相同的<section>标签。

我尝试了一些类似本文中的选项:herehere,但没有得到所需的结果。

这不起作用

SELECT  a.value1,
    x.XmlCol.value('(section/content/p)[1]','VARCHAR(100)') AS SendMethod
FROM    @test a
CROSS APPLY a.AppConfig.nodes('/ContentBlock') x(XmlCol)
WHERE x.XmlCol.exist('section/content/p[contains(.,"Make a Friend")]') = 1

我如何得到它?

先谢谢了。吉列尔莫。

2 个答案:

答案 0 :(得分:2)

像这样尝试:

DECLARE @mockup TABLE(ID INT IDENTITY,YourXML XML);
INSERT INTO @mockup VALUES
('<ContentBlock xmlns="http://corp.com/wdpr/ContentBlock">
  <key xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="tcm:0-133036" xlink:title="entityCard" xlink:type="simple" />
  <template xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="tcm:0-132970" xlink:title="card-header-read-more-all-media" xlink:type="simple" />
  <section>
    <name xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="tcm:0-132988" xlink:title="header" xlink:type="simple" />
    <content>
      <p xmlns="http://www.w3.org/1999/xhtml">Make a Friend</p>
    </content>
  </section>
  <section>
    <name xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="tcm:0-133110" xlink:title="readMore" xlink:type="simple" />
    <content>
      <p xmlns="http://www.w3.org/1999/xhtml">Meet and greet friendly animals at the only petting zoo in Disney’s Animal
        Kingdom park.
     </p>
    </content>
  </section>
  <section>
    <name xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="tcm:0-132939" xlink:title="readAll" xlink:type="simple" />
    <content>
      <p xmlns="http://www.w3.org/1999/xhtml">Meet and greet friendly animals.
     </p>
      <p xmlns="http://www.w3.org/1999/xhtml">Pet, brush and feed domesticated creatures
     </p>
      <ul xmlns="http://www.w3.org/1999/xhtml">
        <li xmlns="http://www.w3.org/1999/xhtml">Goats</li>
      </ul>
      <p xmlns="http://www.w3.org/1999/xhtml">Handy animal brushes are available .
     </p>
      <p xmlns="http://www.w3.org/1999/xhtml">
        <strong xmlns="http://www.w3.org/1999/xhtml">Keeping Clean</strong>
        <br xmlns="http://www.w3.org/1999/xhtml" />Guests are encouraged to cleanse.</p>
    </content>
  </section>
  <media>
    <name xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="tcm:0-201994" xlink:title="media" xlink:type="simple" />
    <key xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="tcm:0-132952" xlink:title="170 x 96" xlink:type="simple" />
    <image xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="tcm:0-231377" xlink:title="section-01.jpg" xlink:type="simple" />
  </media>
</ContentBlock>');

-首先,我声明一个变量以使搜索字符串动态化:

DECLARE @SearchFor VARCHAR(100)='Make a Friend';

-您的XML在最外面的元素中具有默认名称空间。
-有点奇怪的是:元素<p>上还有另一个默认名称空间
-我们可以用语音前缀声明

WITH XMLNAMESPACES(DEFAULT 'http://corp.com/wdpr/ContentBlock'
                          ,'http://www.w3.org/1999/xlink' AS xlink
                          ,'http://www.w3.org/1999/xhtml' AS InnerDflt )
SELECT YourXML.value('(/ContentBlock/section[(content/InnerDflt:p/text())[1]=sql:variable("@SearchFor")]/name/@xlink:href)[1]','nvarchar(max)') 
FROM @mockup

查询运行如下:

<ContentBlock>开始。寻找一个<section>,其中text()下面<p>的{​​{1}}是搜索字符串。重要提示:在此阶段,我们仍处于<content>级别。因此,我们可以继续<section>XPath并在那里找到属性。

答案 1 :(得分:1)

Shnugo偷走了我的风头,但我仍在发布我整理的内容,因为它也可以正常工作,并展示了一些技巧(例如,*:用于您懒得添加正确的命名空间语法); )。我要提到使用sql:variable将SQL变量传递到您的XPath表达式中-shnugo的帖子演示了如何执行此操作(在我发布的内容中缺少)。

-- bonus sample data
DECLARE @xml XML = 
'<ContentBlock xmlns="http://corp.com/wdpr/ContentBlock">
<key xlink:href="tcm:0-133036" xlink:title="entityCard" xlink:type="simple" xmlns:xlink="http://www.w3.org/1999/xlink"/>
<template xlink:href="tcm:0-132970" xlink:title="card-header-read-more-all-media" xlink:type="simple" xmlns:xlink="http://www.w3.org/1999/xlink"/>
<section>
    <name xlink:href="tcm:0-132988" xlink:title="header" xlink:type="simple" xmlns:xlink="http://www.w3.org/1999/xlink"/>
    <content>
        <p xmlns="http://www.w3.org/1999/xhtml">Make a Friend</p>
    </content>
</section>
<section>
    <name xlink:href="tcm:0-133110" xlink:title="readMore" xlink:type="simple" xmlns:xlink="http://www.w3.org/1999/xlink"/>
    <content>
        <p xmlns="http://www.w3.org/1999/xhtml">Meet and greet friendly animals at the only petting zoo in Disney’s Animal
        Kingdom park.
     </p>
    </content>
</section>
<section>
    <name xlink:href="tcm:0-132939" xlink:title="readAll" xlink:type="simple" xmlns:xlink="http://www.w3.org/1999/xlink"/>
    <content>
        <p xmlns="http://www.w3.org/1999/xhtml">Meet and greet friendly animals.</p>
        <p xmlns="http://www.w3.org/1999/xhtml">Pet, brush and feed domesticated creatures
     </p>
        <ul xmlns="http://www.w3.org/1999/xhtml">
            <li xmlns="http://www.w3.org/1999/xhtml">Goats</li>
        </ul>
        <p xmlns="http://www.w3.org/1999/xhtml">Handy animal brushes are available .
     </p>
        <p xmlns="http://www.w3.org/1999/xhtml">
            <strong xmlns="http://www.w3.org/1999/xhtml">Keeping Clean</strong>
            <br xmlns="http://www.w3.org/1999/xhtml"/>Guests are encouraged to cleanse.</p>
    </content>
</section>
<media>
    <name xlink:href="tcm:0-201994" xlink:title="media" xlink:type="simple" xmlns:xlink="http://www.w3.org/1999/xlink"/>
    <key xlink:href="tcm:0-132952" xlink:title="170 x 96" xlink:type="simple" xmlns:xlink="http://www.w3.org/1999/xlink"/>
    <image xlink:href="tcm:0-231377" xlink:title="section-01.jpg" xlink:type="simple" xmlns:xlink="http://www.w3.org/1999/xlink"/>
</media>
</ContentBlock>';

DECLARE @test TABLE (someId INT IDENTITY, AppConfig XML);
INSERT @test (AppConfig) VALUES (@xml),(CAST(REPLACE(REPLACE(CAST(@xml AS VARCHAR(8000)),
  'Make a Friend','xxx'),'Keeping Clean','Make a Friend') AS XML));

-- SOLUTION
WITH XMLNAMESPACES ('http://www.w3.org/1999/xlink' AS xlink)
SELECT t.someId, href = cb.x.value('(../*:name/@xlink:href)[1]', 'varchar(8000)')
FROM @test AS t
CROSS APPLY t.AppConfig.nodes('*:ContentBlock/*:section/*:content') AS cb(x)
WHERE cb.x.exist('*:p[contains(.,"Make a Friend")]') = 1;

返回:

someId      href
----------- -------------
1           tcm:0-132988
2           tcm:0-132939