@@ -1529,6 +1529,7 @@ xml_parse(text *data, XmlOptionType xmloption_arg, bool preserve_whitespace,
1529
1529
PgXmlErrorContext * xmlerrcxt ;
1530
1530
volatile xmlParserCtxtPtr ctxt = NULL ;
1531
1531
volatile xmlDocPtr doc = NULL ;
1532
+ volatile int save_keep_blanks = -1 ;
1532
1533
1533
1534
len = VARSIZE_ANY_EXHDR (data ); /* will be useful later */
1534
1535
string = xml_text2xmlChar (data );
@@ -1545,7 +1546,6 @@ xml_parse(text *data, XmlOptionType xmloption_arg, bool preserve_whitespace,
1545
1546
PG_TRY ();
1546
1547
{
1547
1548
bool parse_as_document = false;
1548
- int options ;
1549
1549
int res_code ;
1550
1550
size_t count = 0 ;
1551
1551
xmlChar * version = NULL ;
@@ -1571,25 +1571,28 @@ xml_parse(text *data, XmlOptionType xmloption_arg, bool preserve_whitespace,
1571
1571
parse_as_document = true;
1572
1572
}
1573
1573
1574
- /*
1575
- * Select parse options.
1576
- *
1577
- * Note that here we try to apply DTD defaults (XML_PARSE_DTDATTR)
1578
- * according to SQL/XML:2008 GR 10.16.7.d: 'Default values defined by
1579
- * internal DTD are applied'. As for external DTDs, we try to support
1580
- * them too (see SQL/XML:2008 GR 10.16.7.e), but that doesn't really
1581
- * happen because xmlPgEntityLoader prevents it.
1582
- */
1583
- options = XML_PARSE_NOENT | XML_PARSE_DTDATTR
1584
- | (preserve_whitespace ? 0 : XML_PARSE_NOBLANKS );
1585
-
1586
1574
if (parse_as_document )
1587
1575
{
1576
+ int options ;
1577
+
1578
+ /* set up parser context used by xmlCtxtReadDoc */
1588
1579
ctxt = xmlNewParserCtxt ();
1589
1580
if (ctxt == NULL || xmlerrcxt -> err_occurred )
1590
1581
xml_ereport (xmlerrcxt , ERROR , ERRCODE_OUT_OF_MEMORY ,
1591
1582
"could not allocate parser context" );
1592
1583
1584
+ /*
1585
+ * Select parse options.
1586
+ *
1587
+ * Note that here we try to apply DTD defaults (XML_PARSE_DTDATTR)
1588
+ * according to SQL/XML:2008 GR 10.16.7.d: 'Default values defined
1589
+ * by internal DTD are applied'. As for external DTDs, we try to
1590
+ * support them too (see SQL/XML:2008 GR 10.16.7.e), but that
1591
+ * doesn't really happen because xmlPgEntityLoader prevents it.
1592
+ */
1593
+ options = XML_PARSE_NOENT | XML_PARSE_DTDATTR
1594
+ | (preserve_whitespace ? 0 : XML_PARSE_NOBLANKS );
1595
+
1593
1596
doc = xmlCtxtReadDoc (ctxt , utf8string ,
1594
1597
NULL , /* no URL */
1595
1598
"UTF-8" ,
@@ -1608,43 +1611,36 @@ xml_parse(text *data, XmlOptionType xmloption_arg, bool preserve_whitespace,
1608
1611
}
1609
1612
else
1610
1613
{
1611
- xmlNodePtr root ;
1612
-
1613
- /* set up document with empty root node to be the context node */
1614
+ /* set up document that xmlParseBalancedChunkMemory will add to */
1614
1615
doc = xmlNewDoc (version );
1615
1616
Assert (doc -> encoding == NULL );
1616
1617
doc -> encoding = xmlStrdup ((const xmlChar * ) "UTF-8" );
1617
1618
doc -> standalone = standalone ;
1618
1619
1619
- root = xmlNewNode (NULL , (const xmlChar * ) "content-root" );
1620
- if (root == NULL || xmlerrcxt -> err_occurred )
1621
- xml_ereport (xmlerrcxt , ERROR , ERRCODE_OUT_OF_MEMORY ,
1622
- "could not allocate xml node" );
1623
- /* This attaches root to doc, so we need not free it separately. */
1624
- xmlDocSetRootElement (doc , root );
1620
+ /* set parse options --- have to do this the ugly way */
1621
+ save_keep_blanks = xmlKeepBlanksDefault (preserve_whitespace ? 1 : 0 );
1625
1622
1626
1623
/* allow empty content */
1627
1624
if (* (utf8string + count ))
1628
1625
{
1629
1626
xmlNodePtr node_list = NULL ;
1630
- xmlParserErrors res ;
1631
1627
1632
- res = xmlParseInNodeContext (root ,
1633
- (char * ) utf8string + count ,
1634
- strlen ((char * ) utf8string + count ),
1635
- options ,
1636
- & node_list );
1628
+ res_code = xmlParseBalancedChunkMemory (doc , NULL , NULL , 0 ,
1629
+ utf8string + count ,
1630
+ & node_list );
1637
1631
1638
1632
xmlFreeNodeList (node_list );
1639
1633
1640
- if (res != XML_ERR_OK || xmlerrcxt -> err_occurred )
1634
+ if (res_code != 0 || xmlerrcxt -> err_occurred )
1641
1635
xml_ereport (xmlerrcxt , ERROR , ERRCODE_INVALID_XML_CONTENT ,
1642
1636
"invalid XML content" );
1643
1637
}
1644
1638
}
1645
1639
}
1646
1640
PG_CATCH ();
1647
1641
{
1642
+ if (save_keep_blanks != -1 )
1643
+ xmlKeepBlanksDefault (save_keep_blanks );
1648
1644
if (doc != NULL )
1649
1645
xmlFreeDoc (doc );
1650
1646
if (ctxt != NULL )
@@ -1656,6 +1652,9 @@ xml_parse(text *data, XmlOptionType xmloption_arg, bool preserve_whitespace,
1656
1652
}
1657
1653
PG_END_TRY ();
1658
1654
1655
+ if (save_keep_blanks != -1 )
1656
+ xmlKeepBlanksDefault (save_keep_blanks );
1657
+
1659
1658
if (ctxt != NULL )
1660
1659
xmlFreeParserCtxt (ctxt );
1661
1660
0 commit comments