简体   繁体   English

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

[英]T-SQL Find node with matching text and extract value from sibling node in XML field

I've a table with an xml field having content like this: 我有一个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>

My goal is to have a query where I can find (for example) the <p> that contains the text "Make a Friend", then get the xlink:href of the <name> element in the same <section> tag. 我的目标是在查询中找到(例如)包含文本“Make a Friend”的<p> ,然后在同一<section>标记中获取<name>元素的xlink:href。

I tried a few options like in this posts: here and here but I'm not getting the results I need. 我在这篇文章中尝试了一些选项: 这里这里,但我没有得到我需要的结果。

This doesn't work 这不起作用

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

How can I get it? 我怎么才能得到它?

Thanks in advance. 提前致谢。 Guillermo. 吉列尔莫。

Try it like this: 试试这样:

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

-First I declare a variable to make the search-string dynamic: - 首先我声明一个变量来使搜索字符串动态化:

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

--Your XML has a default namespace in the outermost element. - 您的XML在最外层元素中有一个默认命名空间。
--What makes things a bit weird: There is another default namespace on the element <p> - 什么使事情有点奇怪:元素<p>上有另一个默认命名空间
--We can declare this with a speaking prefix - 我们可以用说话前缀声明这个

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

The query runs as this: 查询运行如下:

Start with <ContentBlock> . <ContentBlock>开始。 Look for a <section> , where the text() of <p> below <content> is the search-string. 查找<section><content>下面的<p>text()是搜索字符串。 Important: At this stage we are still on the level of <section> . 重要提示:在此阶段,我们仍然处于<section>的级别。 So we can continue the XPath with <name> and find the attribute there. 所以我们可以用<name>继续XPath并在那里找到属性。

Shnugo stole my thunder but I'm still posting what I put together as it will work as well and demonstrates a few tricks (eg *: for when you're too lazy to add the correct namespace syntax) ;). Shnugo偷走了我的雷声但是我仍然发布了我放在一起的东西,因为它也会起作用并展示一些技巧(例如*:因为你懒得添加正确的命名空间语法);)。 I was going to mention the use of sql:variable to pass a SQL variable into your XPath expressions - shnugo's post demonstrates how to do that (it's missing in what I'm posting). 我要提到使用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;

Returns: 返回:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM