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

标签 xml postgresql xpath xml-parsing

我制作了下表:

create table temp.promotions_xml(id serial promotion_xml xml);

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

<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

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

最佳答案

这个有效:

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;

返回:

{"2013 1st Semester Jet Giveaways"}

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

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

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>

关于xml - 使用 xpath 从 postgres 中的 XML 列中提取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17799790/

相关文章:

php - 如何使用 php simplexml_load_file 访问 td 类中的值

c# - 添加基于带有 SignedXml 类的 Id 属性的引用时出现“格式错误的引用元素”

sql - PostgreSQL - 有条件地从 WHERE 子句中排除语句

sql - 优化 ActiveRecord 查询

java - 使用 xpath 定位按钮 - Chrome 中的 JAVA/Selenium 2

Java XPath API 提取选择性文本

c# - 如何在 dataGrid 中将 XML 中的数据排序为数字

mysql - 有没有人让 pgloader 为从 Mysql 迁移到 Postgres 工作

python - LXML .xpath() 方法上下文节点在谓词中未正确识别?

swift - Xcode 10.1, 'Element' 与 'XML.Accessor.Element' 冲突?