计算xmltable中xmlpath的累积总和(总和)

时间:2019-08-17 16:14:04

标签: postgresql xpath

我想计算xml表的累积页面高度。这似乎很容易,因为preceding-sibling返回所有同级,我们只需要总结一下即可。但是,以下查询失败,显示为

  

SQL错误[XX000]:错误:意外的XPath对象类型3

SELECT array_length(regexp_split_to_array(xmltable.txt, E'\\s+'), 1) as word_count,
       xmltable.txt ~ '^[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?$' as is_number,
       xmltable.*
  FROM (
          select xml('<?xml version="1.0" encoding="UTF-8"?>
                      <content version="2.0" extractor="pdftohtml">
                        <page number="1" height="1263" width="892">
                          <text_block top="80" left="137" width="255" height="159">
                            <text top="85" left="197" font="DDUBPN+GlyphLessFont" size="14">Foo</text>
                            <text top="105" left="142" font="DDUBPN+GlyphLessFont" size="23">Bar</text>
                          </text_block>
                        </page>
                        <page number="2" height="1263" width="892">
                          <text_block top="80" left="137" width="255" height="159">
                            <text top="85" left="197" font="DDUBPN+GlyphLessFont" size="14">Foo</text>
                            <text top="105" left="142" font="DDUBPN+GlyphLessFont" size="23">Bar</text>
                          </text_block>
                        </page>
                      </content>') as report
        ) as t,
       XMLTABLE('//text'
                PASSING report
                COLUMNS ordinality FOR ORDINALITY,
                        x int PATH '@top',
                        y int path '@left',
                        page int path '../../@number',
                        page_hight int path '../../@height',
                        txt varchar path 'text()[1]',
                        cumsum_page_height int path 'sum(../../preceding-sibling::page/@height)'
       );

我不了解错误,我可以轻松地SELECT xpath('sum(/a/value/@h)', '<a><value h="12"/><value h="13"/></a>'::xml);

XMLTABLE中不支持xpath函数吗?我应该如何解决?

1 个答案:

答案 0 :(得分:1)

看来PostgreSQL无法处理除节点集或字符串之外的其他XPath数据类型。使用XPath { "8dfb0c3c-5c2b-47eb-924f-ab9365ca9d89": { "@odata.type": "#microsoft.graph.plannerAssignment", "assignedDateTime": "2018-06-12T09:32:27.9137819Z", "orderHint": "", "assignedBy": { "user": "@{displayName=; id=rHcFhQEnpUm_DNWYU1mTYZYAB64F}" } } } 转换布尔值和数字数据类型表达式。

此SQL表达式:

string()

输出:

SELECT array_length(regexp_split_to_array(xmltable.txt, E'\\s+'), 1) as word_count,
       xmltable.txt ~ '^[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?$' as is_number,
       xmltable.*
  FROM (
          select xml('<?xml version="1.0" encoding="UTF-8"?>
                      <content version="2.0" extractor="pdftohtml">
                        <page number="1" height="1263" width="892">
                          <text_block top="80" left="137" width="255" height="159">
                            <text top="85" 
                                  left="197"
                                  font="DDUBPN+GlyphLessFont"
                                  size="14">Foo</text>
                            <text top="105"left="142"
                                  font="DDUBPN+GlyphLessFont"
                                  size="23">Bar</text>
                          </text_block>
                        </page>
                        <page number="2" height="1263" width="892">
                          <text_block top="80" left="137" width="255" height="159">
                            <text top="85" 
                                  left="197" 
                                  font="DDUBPN+GlyphLessFont"
                                  size="14">Foo</text>
                            <text top="105" 
                                  left="142" 
                                  font="DDUBPN+GlyphLessFont" 
                                  size="23">Bar</text>
                          </text_block>
                        </page>
                      </content>') as report
        ) as t,
       XMLTABLE('//text'
                PASSING report
                COLUMNS ordinality FOR ORDINALITY,
                        x int PATH '@top',
                        y int path '@left',
                        page int path '../../@number',
                        page_hight int path '../../@height',
                        txt varchar path 'normalize-space()',
                        cumsum_page_height int path 
                            'string(sum(../../preceding-sibling::page/@height))'
       );

Check it on DB Fiddle