【问题标题】:Parse html field in postgresql query在 postgresql 查询中解析 html 字段
【发布时间】:2019-03-31 14:18:12
【问题描述】:

我有一个包含 PostgreSQL 数据库中的 HTML 表的文本字段。我想从该字段中提取一些数据:一行中的每个 TR(不是来自标题)和一列中的每个 TD。这可能吗?

表格的名称是“documentos”,包含 HTML 表格的文本字段的名称称为“props”。道具包含以下内容:

select props
from documentos
where uidref = 'ee41f201-0049-41e9-9c5d-5c35e2cf73ac'

我想获得:

444444444 | Investigador | Daniel | Perez
555555555 | Becario      | Jorge  | Fernandez

提前致谢!

【问题讨论】:

标签: html database postgresql parsing


【解决方案1】:

我没有使用 PostreSQL 的经验,也没有使用 XPATH 的经验,但是我能够为您找到一些东西:

with x as (select
'<TABLE>
        <TBODY>
            <TR>
                <TH class="RowTitle">Identificacion</TH>
                <TH class="colRol">Rol</TH>
            </TR>
            <TR class="tData">
                <TD class="RowTitle">
                    <A href="#">4444</A>
                </TD>
                <TD class="colRow" val="INVARGEXT">Investigador</TD>
            </TR>
            <TR class="tData">
                <TD class="RowTitle">
                    <A href="#">55555</A>
                </TD>
                <TD class="colRow" val="BECARIO">Becario</TD>
            </TR>
        </TBODY>
    </TABLE>'::xml as t
),
y as (select unnest(xpath('//TR[@class="tData"]', t)) td from x)
select -- y.td, -- just to debug
xpath('//TD[@class="RowTitle"]/A/text()', y.td),
xpath('//TD/text()', y.td)
from y;

这个输出:

    xpath   xpath
1   4444                                        Investigador
2   55555                                       Becario

希望这个有用。

更多信息herehere

【讨论】:

    猜你喜欢
    • 2021-07-04
    • 1970-01-01
    • 1970-01-01
    • 2015-06-27
    • 2017-10-19
    • 2017-01-15
    • 2015-04-27
    • 2022-10-05
    • 1970-01-01
    相关资源
    最近更新 更多