首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用xpath从postgres的XML列中提取数据

使用xpath从postgres的XML列中提取数据
EN

Stack Overflow用户
提问于 2013-07-23 08:53:27
回答 1查看 36.1K关注 0票数 13

我做了下面的表格:

代码语言:javascript
复制
create table temp.promotions_xml(id serial promotion_xml xml);

我将以下数据插入到temp.promotions中:

代码语言:javascript
复制
<promotions xmlns="http://www.demandware.com/xml/impex/promotion/2008-01-31">
    <campaign campaign-id="2013-1st-semester-jet-giveaways">
        <description>2013 1st Semester Jet Giveaways</description>
        <enabled-flag>true</enabled-flag>
        <start-date>2013-01-01T05:00:00.000Z</start-date>
        <end-date>2013-07-01T04:00:00.000Z</end-date>
        <customer-groups>
            <customer-group group-id="Everyone"/>
        </customer-groups>
    </campaign>
</promotions>

数据在表中。

我想不出怎么把它弄出来。我可能希望能够填充我将要构建的关系模型,所以我想去掉所有的标记。

下面是我尝试过的一些不起作用的查询。我很确定我只是在绕过正确的语法。这些查询返回空集的行。

FWIW,我们使用Postgres 9.0.4。

谢谢,--sw

代码语言:javascript
复制
select xpath('/promotions/campaign/description/text()',promotion_xml) textcol from temp.promotions_xml
select xpath('./promotions/campaign/description/text()',promotion_xml) textcol from temp.promotions_xml
select xpath('promotions/campaign/description/text()',promotion_xml) textcol from temp.promotions_xml
select xpath('///description/text()',promotion_xml) textcol from temp.promotions_xml
select xpath('//description/text()',promotion_xml) textcol from temp.promotions_xml
select xpath('.//description/text()',promotion_xml) textcol from temp.promotions_xml
select xpath('./campaign/description/text()',promotion_xml) textcol from temp.promotions_xml
select xpath('//campaign/description/text()',promotion_xml) textcol from temp.promotions_xml
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-07-23 10:44:40

这是可行的:

代码语言:javascript
复制
WITH tbl(p_xml) AS (  -- CTE just to provide test table with xml value
   SELECT '<promotions xmlns="http://www.demandware.com/xml/impex/promotion/2008-01-31">
              <campaign campaign-id="2013-1st-semester-jet-giveaways">
                 <description>2013 1st Semester Jet Giveaways</description>
                 <enabled-flag>true</enabled-flag>
                 <start-date>2013-01-01T05:00:00.000Z</start-date>
                 <end-date>2013-07-01T04:00:00.000Z</end-date>
                 <customer-groups>
                    <customer-group group-id="Everyone"/>
                 </customer-groups>
              </campaign>
           </promotions>'::xml
    )  -- end of CTE, the rest is the solution
SELECT xpath('/n:promotions/n:campaign/n:description/text()', p_xml
           , '{{n,http://www.demandware.com/xml/impex/promotion/2008-01-31}}')
FROM   tbl;

返回:

代码语言:javascript
复制
{"2013 1st Semester Jet Giveaways"}

请注意我如何在 third argument of xpath()中为您的名称空间分配名称空间别名 n 并在 xpath 的每个级别使用它。

如果从文档中删除XML名称空间,一切都会变得简单得多:

代码语言:javascript
复制
WITH tbl(p_xml) AS (  -- not the missing namespace below
   SELECT '<promotions>
              <campaign campaign-id="2013-1st-semester-jet-giveaways">
                 <description>2013 1st Semester Jet Giveaways</description>
                 <enabled-flag>true</enabled-flag>
                 <start-date>2013-01-01T05:00:00.000Z</start-date>
                 <end-date>2013-07-01T04:00:00.000Z</end-date>
                 <customer-groups>
                    <customer-group group-id="Everyone"/>
                 </customer-groups>
              </campaign>
           </promotions>'::xml
   )
SELECT xpath('/promotions/campaign/description/text()', p_xml)
FROM   tbl;

<rant> 是我自己还是大家都对json and jsonb感到满意,所以我们不必处理 XML。</rant>

票数 30
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17799790

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档