Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/13.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
在PostgreSQL中使用XPath过滤多个XML节点_Sql_Xml_Postgresql_Xpath - Fatal编程技术网

在PostgreSQL中使用XPath过滤多个XML节点

在PostgreSQL中使用XPath过滤多个XML节点,sql,xml,postgresql,xpath,Sql,Xml,Postgresql,Xpath,有一个具有以下结构的XML: - Item - Documents - Document - Records - Record - Category - Code - Value 下面是选择按类别代码过滤的记录值的SQL查询 SELECT (xpath('/ns:Record/ns:Value/text()', rec, ARRAY[ARRAY['ns', 'http://some-ns'

有一个具有以下结构的XML:

- Item
  - Documents
    - Document
      - Records
        - Record
          - Category
            - Code
          - Value
下面是选择按类别代码过滤的记录值的SQL查询

SELECT  (xpath('/ns:Record/ns:Value/text()', rec, ARRAY[ARRAY['ns', 'http://some-ns']]))[1]::text AS val
FROM    (
    SELECT  unnest(xpath('/ns:Item/ns:Documents/ns:Document/ns:Records/ns:Record[ns:Category/ns:Code/text()="MAIN.CAT001"]',
               '<Item xmlns="http://some-ns"><Documents><Document><Records><Record><Category><Code>MAIN.CAT001</Code></Category><Value>Value 001</Value></Record><Record><Category><Code>MAIN.CAT002</Code></Category><Value>Value 002</Value></Record><Record><Category><Code>MAIN.CAT003</Code></Category><Value>Value 003</Value></Record></Records></Document></Documents></Item>'::xml,
               ARRAY[ARRAY['ns', 'http://some-ns']])) AS rec
) t
是否可以不仅按一个“类别代码”,而且按多个“类别代码”过滤“记录”? 我的意思是我想用这样的过滤器

ns:Record[ns:Category/ns:Code/text()=("MAIN.CAT001", "MAIN.CAT003")]
还是这个

ns:Record[ns:Category/ns:Code/text()="MAIN.CAT001" or ns:Category/ns:Code/text()="MAIN.CAT003"]
但是这两种解决方案都不起作用

尝试使用
contains(ns:code,“MAIN.CAT001”)或contains(ns:code,“MAIN.CAT003”)]

SELECT  (xpath('/ns:Record/ns:Value/text()', rec, ARRAY[ARRAY['ns', 'http://some-ns']]))[1]::text AS val
FROM    (
  SELECT
    unnest(xpath('/ns:Item/ns:Documents/ns:Document/ns:Records/ns:Record[ns:Category[contains(ns:Code,"MAIN.CAT001") or contains(ns:Code,"MAIN.CAT003")]]',
                 '<Item xmlns="http://some-ns"><Documents><Document><Records><Record><Category><Code>MAIN.CAT001</Code></Category><Value>Value 001</Value></Record><Record><Category><Code>MAIN.CAT002</Code></Category><Value>Value 002</Value></Record><Record><Category><Code>MAIN.CAT003</Code></Category><Value>Value 003</Value></Record></Records></Document></Documents></Item>'::xml,
                 ARRAY[ARRAY['ns', 'http://some-ns']])) AS rec
) t;

    val    
-----------
 Value 001
 Value 003