xml2: Add tests for functions xpath_nodeset() and xpath_list()
authorMichael Paquier <michael@paquier.xyz>
Thu, 14 Nov 2024 04:10:36 +0000 (13:10 +0900)
committerMichael Paquier <michael@paquier.xyz>
Thu, 14 Nov 2024 04:10:36 +0000 (13:10 +0900)
These two functions with their different argument lists have never been
tested in this module, so let's add something.

Author: Ronan Dunklau
Discussion: https://postgr.es/m/ZzMSJkiNZhimjXWx@paquier.xyz

contrib/xml2/expected/xml2.out
contrib/xml2/expected/xml2_1.out
contrib/xml2/sql/xml2.sql

index eba6ae60364988ce633db5a1137666f28e270408..3d97b14c3a1e479519bb29c99a9ccc39ddb2613c 100644 (file)
@@ -207,6 +207,45 @@ SELECT xslt_process('<employee><name>cim</name><age>30</age><pay>400</pay></empl
  
 (1 row)
 
+-- xpath_nodeset()
+SELECT xpath_nodeset(article_xml::text, '/article/author|/article/pages')
+  FROM articles;
+             xpath_nodeset              
+----------------------------------------
+ <author>test</author><pages>37</pages>
+(1 row)
+
+SELECT xpath_nodeset(article_xml::text, '/article/author|/article/pages',
+                     'item_without_toptag')
+  FROM articles;
+                                                        xpath_nodeset                                                         
+------------------------------------------------------------------------------------------------------------------------------
+ <item_without_toptag><author>test</author></item_without_toptag><item_without_toptag><pages>37</pages></item_without_toptag>
+(1 row)
+
+SELECT xpath_nodeset(article_xml::text, '/article/author|/article/pages',
+                     'result', 'item')
+  FROM articles;
+                                   xpath_nodeset                                   
+-----------------------------------------------------------------------------------
+ <result><item><author>test</author></item><item><pages>37</pages></item></result>
+(1 row)
+
+-- xpath_list()
+SELECT xpath_list(article_xml::text, '/article/author|/article/pages')
+  FROM articles;
+ xpath_list 
+------------
+ test,37
+(1 row)
+
+SELECT xpath_list(article_xml::text, '/article/author|/article/pages', '|')
+  FROM articles;
+ xpath_list 
+------------
+ test|37
+(1 row)
+
 -- possible security exploit
 SELECT xslt_process('<xml><foo>Hello from XML</foo></xml>',
 $$<xsl:stylesheet version="1.0"
index bac90e5a2a9d199f689def56f00425dd28b63d1c..31700040a604b434338cab02eaaf0d29b71843d2 100644 (file)
@@ -151,6 +151,45 @@ SELECT xslt_process('<employee><name>cim</name><age>30</age><pay>400</pay></empl
   </xsl:template>
 </xsl:stylesheet>$$::text, 'n1="v1",n2="v2",n3="v3",n4="v4",n5="v5",n6="v6",n7="v7",n8="v8",n9="v9",n10="v10",n11="v11",n12="v12"'::text);
 ERROR:  xslt_process() is not available without libxslt
+-- xpath_nodeset()
+SELECT xpath_nodeset(article_xml::text, '/article/author|/article/pages')
+  FROM articles;
+             xpath_nodeset              
+----------------------------------------
+ <author>test</author><pages>37</pages>
+(1 row)
+
+SELECT xpath_nodeset(article_xml::text, '/article/author|/article/pages',
+                     'item_without_toptag')
+  FROM articles;
+                                                        xpath_nodeset                                                         
+------------------------------------------------------------------------------------------------------------------------------
+ <item_without_toptag><author>test</author></item_without_toptag><item_without_toptag><pages>37</pages></item_without_toptag>
+(1 row)
+
+SELECT xpath_nodeset(article_xml::text, '/article/author|/article/pages',
+                     'result', 'item')
+  FROM articles;
+                                   xpath_nodeset                                   
+-----------------------------------------------------------------------------------
+ <result><item><author>test</author></item><item><pages>37</pages></item></result>
+(1 row)
+
+-- xpath_list()
+SELECT xpath_list(article_xml::text, '/article/author|/article/pages')
+  FROM articles;
+ xpath_list 
+------------
+ test,37
+(1 row)
+
+SELECT xpath_list(article_xml::text, '/article/author|/article/pages', '|')
+  FROM articles;
+ xpath_list 
+------------
+ test|37
+(1 row)
+
 -- possible security exploit
 SELECT xslt_process('<xml><foo>Hello from XML</foo></xml>',
 $$<xsl:stylesheet version="1.0"
index ac49cfa7c52315ca48c4829e58253acc7eed49eb..ef99d164f2720c5e3ee999a7abd4b2c3b7c31c85 100644 (file)
@@ -123,6 +123,22 @@ SELECT xslt_process('<employee><name>cim</name><age>30</age><pay>400</pay></empl
   </xsl:template>
 </xsl:stylesheet>$$::text, 'n1="v1",n2="v2",n3="v3",n4="v4",n5="v5",n6="v6",n7="v7",n8="v8",n9="v9",n10="v10",n11="v11",n12="v12"'::text);
 
+-- xpath_nodeset()
+SELECT xpath_nodeset(article_xml::text, '/article/author|/article/pages')
+  FROM articles;
+SELECT xpath_nodeset(article_xml::text, '/article/author|/article/pages',
+                     'item_without_toptag')
+  FROM articles;
+SELECT xpath_nodeset(article_xml::text, '/article/author|/article/pages',
+                     'result', 'item')
+  FROM articles;
+
+-- xpath_list()
+SELECT xpath_list(article_xml::text, '/article/author|/article/pages')
+  FROM articles;
+SELECT xpath_list(article_xml::text, '/article/author|/article/pages', '|')
+  FROM articles;
+
 -- possible security exploit
 SELECT xslt_process('<xml><foo>Hello from XML</foo></xml>',
 $$<xsl:stylesheet version="1.0"