Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql server T-SQL查找具有匹配文本的节点,并从XML字段中的同级节点提取值_Sql Server_Xml_Tsql_Xpath_Xml Namespaces - Fatal编程技术网

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

Sql server T-SQL查找具有匹配文本的节点,并从XML字段中的同级节点提取值,sql-server,xml,tsql,xpath,xml-namespaces,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"

我有一个带有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 @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>');
我怎样才能得到它

提前谢谢。吉列尔莫。

像这样试试:

<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 @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>');
--您的XML在最外面的元素中有一个默认名称空间。
--让事情变得有点奇怪的是:元素

--我们可以用语音前缀来声明它

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
查询的运行方式如下:


开始。查找
,其中
下面的
是搜索字符串。重要提示:在这个阶段,我们仍然处于
的水平。因此,我们可以使用
继续
XPath
,并在那里找到属性。

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