Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xml PostgreSQL xpath-按日期条件_Xml_Postgresql_Xpath - Fatal编程技术网

Xml PostgreSQL xpath-按日期条件

Xml PostgreSQL xpath-按日期条件,xml,postgresql,xpath,Xml,Postgresql,Xpath,我将xml存储在postgres数据库中 <data> <contract> <details> <code>BAC</code> <type>SU</type> <amount>879</amount> </details> <start_da

我将xml存储在postgres数据库中

<data>
    <contract>
        <details>
            <code>BAC</code>
            <type>SU</type>
            <amount>879</amount>
        </details>
        <start_date>15062020</start_date>
    </contract>
    <contract>
        <details>
            <code>BAC</code>
            <type>SU</type>
            <amount>879</amount>
        </details>
        <start_date>15062019</start_date>
    </contract>
</data>
我需要的是过滤代码等于BAC、类型等于SU的契约(是的,两个契约都等于这个条件)。但我还需要过滤开始日期,因为开始日期小于当前日期,所以应该过滤第一份合同。由于某种原因,它不是

这是我的密码:

select
 id,

  unnest(
    xpath(
      'sum(/data/details[code=="BAC" and type="SU"] and start_date >='
        || to_char(current_date, 'DDMMYYYY') 
        || ']/amount/text())', 
      xmlparse(document xml)
    )
  )::text::numeric as current_contracts


from my_db
您的XPath似乎已关闭。请尝试以下作为起点

XPath

带日期的XPath


/数据/合同[start_date ge“15062020”]/details[code=“BAC”和type=“SU”]/amount/text()

如果您使用的是PG 10+,则可以使用XMLTABLE,然后将结果像表格一样进行操作,更容易

with sub as (
SELECT tabla_xml.*

FROM (VALUES 
 ('<data>
    <contract>
        <details>
            <code>BAC</code>
            <type>SU</type>
            <amount>879</amount>
        </details>
        <start_date>15062020</start_date>
    </contract>
    <contract>
        <details>
            <code>BAC</code>
            <type>SU</type>
            <amount>879</amount>
        </details>
        <start_date>15062019</start_date>
    </contract>
</data>'::xml) )  AS t (data),
XMLTABLE ('//data/contract/details'
PASSING t.data
COLUMNS code text PATH 'code',
type text PATH 'type',
amount real PATH 'amount',
 start_date text path '../start_date' ) tabla_xml)

 select * from sub where code='BAC' and type='SU'
您将获得:

BAC SU  879 15062020
BAC SU  879 15062019

是的,那过滤日期呢?只有第二个合同应该是countedI,我没有Postresql。这就是为什么我明确地说“一个起点”。我给你看了一个正确的XPath。将硬编码值替换为当前日期的剩余部分。
BAC SU  879 15062020
BAC SU  879 15062019