【问题标题】:How to extract html structure from string?如何从字符串中提取html结构?
【发布时间】:2021-12-12 13:59:40
【问题描述】:

我有一个用 html 写的长文本:

<body>
    <h2>title 1</h2>
    <h2>This is an <b>important</b> title</h2>
        Some text
        <h3>This a subtitle b</h3>
        <h3>This is also <span style="font-weight:500">important</span></h3>        
</body>

我需要从中提取标题以创建目录。我希望结果为:

h2      Title 1
h2      This is an <b>important</b> title
h3      This a subtitle b
h3      This is also <span style="font-weight:500">important</span>

h2      Title 1
h2      This is an important title
h3      This a subtitle b
h3      This is also important

我试过了

select * from xmltable('body/*'  passing xmltype('<body><h2>title 1</h2><h2>This is an <b>important</b> title</h2>Some text<h3>This a subtitle b</h3><h3>This is also <span style="font-weight:500">important</span></h3></body>') 
columns 
tag_name varchar2(1000) path 'name()',
tag_value varchar2(1000) path 'text()')
where tag_name in ('h1','h2','h3','h4','h5')

但我得到了错误:

ORA-19279: XPTY0004 - XQuery dynamic type mismatch: expected singleton sequence - got multi-item sequence
19279. 00000 -  "XPTY0004 - XQuery dynamic type mismatch: expected singleton sequence - got multi-item sequence" 
*Cause:    The XQuery sequence passed in had more than one item.
*Action:   Correct the XQuery expression to return a single item sequence.  

请问有人知道怎么解决吗?

谢谢。

【问题讨论】:

    标签: html oracle plsql


    【解决方案1】:

    这可以作为输出吗?

    TAG_NAME TAG_VALUE                                                             
    -------- ----------------------------------------------------------------------
    h2       <h2>title 1</h2>                                                      
    h2       <h2>This is an <b>important</b> title</h2>                            
    h3       <h3>This a subtitle b</h3>                                            
    h3       <h3>This is also <span style="font-weight:500">important</span></h3>  
    

    这对我来说更有意义 - 然后让您使用的任何 xml 工具根据需要解释标记值。 (他们可能要求标签值是 xmltype 数据类型 - 如果是这样,那么只需删除 select 子句中的 xmlserialize 包装器。)

    如果可以接受,您可以通过对查询稍作修改来获得它。

    select tag_name, xmlserialize(document tag_value) as tag_value
    from xmltable('body/*'  passing xmltype('<body><h2>title 1</h2>
      <h2>This is an <b>important</b> title</h2>Some text<h3>This a subtitle b</h3>
      <h3>This is also <span style="font-weight:500">important</span></h3></body>') 
    columns 
      tag_name varchar2(1000) path 'name()',
      tag_value xmltype path '.')
    where tag_name in ('h1','h2','h3','h4','h5')
    ;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-07
      • 2011-02-22
      • 2016-11-05
      • 2012-03-03
      • 1970-01-01
      相关资源
      最近更新 更多