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