Skip to content

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
...
  • 2 commits
  • 2 files changed
  • 0 commit comments
  • 1 contributor
Showing with 26 additions and 9 deletions.
  1. +5 −9 src/lxml/apihelpers.pxi
  2. +21 −0 src/lxml/tests/test_xslt.py
View
14 src/lxml/apihelpers.pxi
@@ -575,19 +575,15 @@ cdef list _collectAttributes(xmlNode* c_node, int collecttype):
c_attr = c_attr.next
return attributes
-cdef object __RE_XML_ENCODING
-__RE_XML_ENCODING = re.compile(
- ur'^(\s*<\?\s*xml[^>]+)\s+encoding\s*=\s*["\'][^"\']*["\']\s*', re.U)
+cdef object __RE_XML_ENCODING = re.compile(
+ ur'^(<\?xml[^>]+)\s+encoding\s*=\s*["\'][^"\']*["\'](\s*\?>|)', re.U)
-cdef object __REPLACE_XML_ENCODING
-__REPLACE_XML_ENCODING = __RE_XML_ENCODING.sub
-
-cdef object __HAS_XML_ENCODING
-__HAS_XML_ENCODING = __RE_XML_ENCODING.match
+cdef object __REPLACE_XML_ENCODING = __RE_XML_ENCODING.sub
+cdef object __HAS_XML_ENCODING = __RE_XML_ENCODING.match
cdef object _stripEncodingDeclaration(object xml_string):
# this is a hack to remove the XML encoding declaration from unicode
- return __REPLACE_XML_ENCODING(ur'\g<1> ', xml_string)
+ return __REPLACE_XML_ENCODING(ur'\g<1>\g<2>', xml_string)
cdef bint _hasEncodingDeclaration(object xml_string):
# check if a (unicode) string has an XML encoding declaration
View
21 src/lxml/tests/test_xslt.py
@@ -198,6 +198,27 @@ def test_xslt_unicode(self):
self.assertEqual(expected,
unicode(res))
+ def test_xslt_unicode_standalone(self):
+ tree = self.parse(_bytes('<a><b>\\uF8D2</b><c>\\uF8D2</c></a>'
+ ).decode("unicode_escape"))
+ style = self.parse('''\
+<xsl:stylesheet version="1.0"
+ xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
+ <xsl:output encoding="UTF-16" standalone="no"/>
+ <xsl:template match="/">
+ <foo><xsl:value-of select="/a/b/text()" /></foo>
+ </xsl:template>
+</xsl:stylesheet>''')
+
+ st = etree.XSLT(style)
+ res = st(tree)
+ expected = _bytes('''\
+<?xml version="1.0" standalone="no"?>
+<foo>\\uF8D2</foo>
+''').decode("unicode_escape")
+ self.assertEqual(expected,
+ unicode(res))
+
def test_xslt_input(self):
style = self.parse('''\
<xsl:stylesheet version="1.0"

No commit comments for this range

Something went wrong with that request. Please try again.