def test_xslt_copy(self): tree = self.parse('<a><b>B</b><c>C</c></a>') style = self.parse('''\ <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="*" /> <xsl:template match="/"> <foo><xsl:value-of select="/a/b/text()" /></foo> </xsl:template> </xsl:stylesheet>''') transform = etree.XSLT(style) res = transform(tree) self.assertEquals('''\ <?xml version="1.0"?> <foo>B</foo> ''', str(res)) transform_copy = copy.deepcopy(transform) res = transform_copy(tree) self.assertEquals('''\ <?xml version="1.0"?> <foo>B</foo> ''', str(res)) transform = etree.XSLT(style) res = transform(tree) self.assertEquals('''\ <?xml version="1.0"?> <foo>B</foo> ''', str(res))
def test_xslt_input(self): style = self.parse('''\ <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="*" /> <xsl:template match="/"> <foo><xsl:value-of select="/a/b/text()" /></foo> </xsl:template> </xsl:stylesheet>''') st = etree.XSLT(style) st = etree.XSLT(style.getroot())
def test_xslt_resolver_url_building(self): assertEquals = self.assertEquals called = {'count' : 0} expected_url = None class TestResolver(etree.Resolver): def resolve(self, url, id, context): assertEquals(url, expected_url) called['count'] += 1 return self.resolve_string('<CALLED/>', context) stylesheet_xml = _bytes("""\ <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:l="local"> <xsl:template match="/"> <xsl:copy-of select="document('test.xml')"/> </xsl:template> </xsl:stylesheet> """) parser = etree.XMLParser() parser.resolvers.add(TestResolver()) # test without base_url => relative path only expected_url = 'test.xml' xslt = etree.XSLT(etree.XML(stylesheet_xml, parser)) self.assertEquals(called['count'], 0) result = xslt(etree.XML('<a/>')) self.assertEquals(called['count'], 1) # now the same thing with a stylesheet base URL on the filesystem called['count'] = 0 expected_url = os.path.join('MY', 'BASE', 'test.xml') xslt = etree.XSLT(etree.XML(stylesheet_xml, parser, base_url=os.path.join('MY', 'BASE', 'FILE'))) self.assertEquals(called['count'], 0) result = xslt(etree.XML('<a/>')) self.assertEquals(called['count'], 1) # now the same thing with a stylesheet base URL called['count'] = 0 expected_url = 'http://server.com/BASE/DIR/test.xml' xslt = etree.XSLT(etree.XML(stylesheet_xml, parser, base_url='http://server.com/BASE/DIR/FILE')) self.assertEquals(called['count'], 0) result = xslt(etree.XML('<a/>')) self.assertEquals(called['count'], 1)
def run_thread(): try: etree.XSLT(style) except etree.XSLTParseError as e: error_logs.append(e.error_log) else: self.assertFalse(True, "XSLT parsing should have failed but didn't")
def test_xslt_pi_embedded_id(self): # test XPath lookup mechanism tree = self.parse('''\ <?xml version="1.0"?> <?xml-stylesheet type="text/xsl" href="#style"?> <a> <b>B</b> <c>C</c> </a>''') style = self.parse('''\ <xsl:stylesheet version="1.0" xml:id="style" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="*" /> <xsl:template match="/"> <foo><xsl:value-of select="/a/b/text()" /></foo> </xsl:template> </xsl:stylesheet> ''') tree.getroot().append(style.getroot()) style_root = tree.getroot().getprevious().parseXSL().getroot() self.assertEquals("{http://www.w3.org/1999/XSL/Transform}stylesheet", style_root.tag) st = etree.XSLT(style_root) res = st(tree) self.assertEquals('''\ <?xml version="1.0"?> <foo>B</foo> ''', str(res))
def test_xslt_encoding_override(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-8"/> <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' encoding='UTF-16'?>\ <foo>\\uF8D2</foo>""").decode("unicode_escape") f = BytesIO() res.write(f, encoding='UTF-16') if is_python3: result = str(f.getvalue(), 'UTF-16').replace('\n', '') else: result = unicode(str(f.getvalue()), 'UTF-16').replace('\n', '') self.assertEquals(expected, result)
def test_xslt_default_parameters(self): tree = self.parse('<a><b>B</b><c>C</c></a>') style = self.parse('''\ <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:param name="bar" select="'Default'" /> <xsl:template match="*" /> <xsl:template match="/"> <foo><xsl:value-of select="$bar" /></foo> </xsl:template> </xsl:stylesheet>''') st = etree.XSLT(style) res = st(tree, bar="'Bar'") self.assertEquals('''\ <?xml version="1.0"?> <foo>Bar</foo> ''', str(res)) res = st(tree) self.assertEquals('''\ <?xml version="1.0"?> <foo>Default</foo> ''', str(res))
def _test_exslt_regexp_match4(self): # taken from http://www.exslt.org/regexp/functions/match/index.html # THIS IS NOT SUPPORTED! xslt = etree.XSLT(etree.XML(_bytes("""\ <xsl:stylesheet version="1.0" xmlns:regexp="http://exslt.org/regular-expressions" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <test> <xsl:for-each select="regexp:match( 'This is a test string', '([a-z])+ ', 'gi')"> <test1><xsl:value-of select="."/></test1> </xsl:for-each> </test> </xsl:template> </xsl:stylesheet> """))) result = xslt(etree.XML(_bytes('<a/>'))) root = result.getroot() self.assertEquals(root.tag, 'test') self.assertEquals(len(root), 4) self.assertEquals(root[0].text, "This") self.assertEquals(root[1].text, "is") self.assertEquals(root[2].text, "a") self.assertEquals(root[3].text, "test")
def test_exslt_regexp_match1(self): # taken from http://www.exslt.org/regexp/functions/match/index.html xslt = etree.XSLT(etree.XML(_bytes("""\ <xsl:stylesheet version="1.0" xmlns:regexp="http://exslt.org/regular-expressions" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <test> <xsl:for-each select="regexp:match( 'http://www.bayes.co.uk/xml/index.xml?/xml/utils/rechecker.xml', '(\w+):\/\/([^/:]+)(:\d*)?([^# ]*)')"> <test1><xsl:value-of select="."/></test1> </xsl:for-each> </test> </xsl:template> </xsl:stylesheet> """))) result = xslt(etree.XML(_bytes('<a/>'))) root = result.getroot() self.assertEquals(root.tag, 'test') self.assertEquals(len(root), 5) self.assertEquals( root[0].text, "http://www.bayes.co.uk/xml/index.xml?/xml/utils/rechecker.xml") self.assertEquals( root[1].text, "http") self.assertEquals( root[2].text, "www.bayes.co.uk") self.assertFalse(root[3].text) self.assertEquals( root[4].text, "/xml/index.xml?/xml/utils/rechecker.xml")
def run_thread(): transform = etree.XSLT(style) try: transform(tree) except etree.XSLTApplyError: error_logs.append(transform.error_log) else: self.assertFalse(True, "XSLT parsing should have failed but didn't")
def test_xslt_document_elementtree(self): # make sure document('') works from loaded files xslt = etree.XSLT(etree.ElementTree(file=fileInTestDir("test-document.xslt"))) result = xslt(etree.XML('<a/>')) root = result.getroot() self.assertEquals(root.tag, 'test') self.assertEquals(root[0].tag, '{http://www.w3.org/1999/XSL/Transform}stylesheet')
def run_xslt(): style = XML(_bytes('''\ <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="*"> <xsl:copy><foo><xsl:value-of select="/a/b/text()" /></foo></xsl:copy> </xsl:template> </xsl:stylesheet>''')) st = etree.XSLT(style) result.append( st(root).getroot() )
def test_xslt_document_error(self): xslt = etree.XSLT(etree.XML("""\ <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <test>TEXT<xsl:copy-of select="document('uri:__junkfood__is__evil__')//test"/></test> </xsl:template> </xsl:stylesheet> """)) self.assertRaises(etree.XSLTApplyError, xslt, etree.XML('<a/>'))
def test_xslt_document_parse_allow(self): access_control = etree.XSLTAccessControl(read_file=True) xslt = etree.XSLT(etree.parse(fileInTestDir("test-document.xslt")), access_control = access_control) result = xslt(etree.XML('<a/>')) root = result.getroot() self.assertEquals(root.tag, 'test') self.assertEquals(root[0].tag, '{http://www.w3.org/1999/XSL/Transform}stylesheet')
def test_xslt_multiple_transforms(self): xml = '<a/>' xslt = '''\ <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:template match="/"> <response>Some text</response> </xsl:template> </xsl:stylesheet> ''' source = self.parse(xml) styledoc = self.parse(xslt) style = etree.XSLT(styledoc) result = style(source) etree.tostring(result.getroot()) source = self.parse(xml) styledoc = self.parse(xslt) style = etree.XSLT(styledoc) result = style(source) etree.tostring(result.getroot())
def run_thread(): style = XML(_bytes('''\ <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:output method="xml" /> <xsl:template match="/"> <div id="test"> <xsl:apply-templates/> </div> </xsl:template> </xsl:stylesheet>''')) stylesheets.append( etree.XSLT(style) )
def test_xslt_empty(self): # could segfault if result contains "empty document" xml = '<blah/>' xslt = ''' <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:template match="/" /> </xsl:stylesheet> ''' source = self.parse(xml) styledoc = self.parse(xslt) style = etree.XSLT(styledoc) result = style(source) self.assertEqual('', str(result))
def test_xslt_parameter_missing(self): # apply() without needed parameter will lead to XSLTApplyError tree = self.parse('<a><b>B</b><c>C</c></a>') style = self.parse('''\ <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <foo><xsl:value-of select="$bar" /></foo> </xsl:template> </xsl:stylesheet>''') st = etree.XSLT(style) self.assertRaises(etree.XSLTApplyError, st.apply, tree)
def test_xslt_input_partial_doc(self): style = self.parse('''\ <otherroot> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="*" /> <xsl:template match="/"> <foo><xsl:value-of select="/a/b/text()" /></foo> </xsl:template> </xsl:stylesheet> </otherroot>''') self.assertRaises(etree.XSLTParseError, etree.XSLT, style) root_node = style.getroot() self.assertRaises(etree.XSLTParseError, etree.XSLT, root_node) st = etree.XSLT(root_node[0])
def test_xslt_html_output(self): tree = self.parse('<a><b>B</b><c>C</c></a>') style = self.parse('''\ <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="html"/> <xsl:strip-space elements="*"/> <xsl:template match="/"> <html><body><xsl:value-of select="/a/b/text()" /></body></html> </xsl:template> </xsl:stylesheet>''') st = etree.XSLT(style) res = st(tree) self.assertEquals('<html><body>B</body></html>', str(res).strip())
def test_xslt_parameter_invalid(self): tree = self.parse('<a><b>B</b><c>C</c></a>') style = self.parse('''\ <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:param name="bar"/> <xsl:template match="/"> <foo><xsl:value-of select="$bar" /></foo> </xsl:template> </xsl:stylesheet>''') st = etree.XSLT(style) res = self.assertRaises(etree.XSLTApplyError, st, tree, bar="<test/>") res = self.assertRaises(etree.XSLTApplyError, st, tree, bar="....")
def test_xslt_message_terminate(self): xml = '<blah/>' xslt = ''' <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:template match="/"> <xsl:message terminate="yes">TEST TEST TEST</xsl:message> </xsl:template> </xsl:stylesheet> ''' source = self.parse(xml) styledoc = self.parse(xslt) style = etree.XSLT(styledoc) self.assertRaises(etree.XSLTApplyError, style, source) self.assert_("TEST TEST TEST" in [entry.message for entry in style.error_log])
def test_xslt_message(self): xml = '<blah/>' xslt = ''' <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:template match="/"> <xsl:message>TEST TEST TEST</xsl:message> </xsl:template> </xsl:stylesheet> ''' source = self.parse(xml) styledoc = self.parse(xslt) style = etree.XSLT(styledoc) result = style(source) self.assertEqual('', str(result)) self.assert_("TEST TEST TEST" in [entry.message for entry in style.error_log])
def test_xslt_string_parameters(self): tree = self.parse('<a><b>B</b><c>C</c></a>') style = self.parse('''\ <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <foo><xsl:value-of select="$bar" /></foo> </xsl:template> </xsl:stylesheet>''') st = etree.XSLT(style) res = st(tree, bar=etree.XSLT.strparam('''it's me, "Bar"''')) self.assertEquals('''\ <?xml version="1.0"?> <foo>it's me, "Bar"</foo> ''', str(res))
def test_xslt_result_memoryview(self): tree = self.parse('<a><b>B</b><c>C</c></a>') style = self.parse('''\ <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="*" /> <xsl:template match="/"> <foo><xsl:value-of select="/a/b/text()" /></foo> </xsl:template> </xsl:stylesheet>''') st = etree.XSLT(style) res = st(tree) self.assertEquals(_bytes('''\ <?xml version="1.0"?> <foo>B</foo> '''), bytes(memoryview(res)))
def test_exslt_regexp_test(self): xslt = etree.XSLT(etree.XML(_bytes("""\ <xsl:stylesheet version="1.0" xmlns:regexp="http://exslt.org/regular-expressions" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="*"> <test><xsl:copy-of select="*[regexp:test(string(.), '8.')]"/></test> </xsl:template> </xsl:stylesheet> """))) result = xslt(etree.XML(_bytes('<a><b>123</b><b>098</b><b>987</b></a>'))) root = result.getroot() self.assertEquals(root.tag, 'test') self.assertEquals(len(root), 1) self.assertEquals(root[0].tag, 'b') self.assertEquals(root[0].text, '987')
def test_exslt_regexp_replace(self): xslt = etree.XSLT(etree.XML("""\ <xsl:stylesheet version="1.0" xmlns:regexp="http://exslt.org/regular-expressions" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="*"> <test> <xsl:copy-of select="regexp:replace(string(.), 'd.', '', 'XX')"/> <xsl:text>-</xsl:text> <xsl:copy-of select="regexp:replace(string(.), 'd.', 'gi', 'XX')"/> </test> </xsl:template> </xsl:stylesheet> """)) result = xslt(etree.XML(_bytes('<a>abdCdEeDed</a>'))) root = result.getroot() self.assertEquals(root.tag, 'test') self.assertEquals(len(root), 0) self.assertEquals(root.text, 'abXXdEeDed-abXXXXeXXd')
def test_xslt_document_XML(self): # make sure document('') works from parsed strings xslt = etree.XSLT(etree.XML("""\ <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <test>TEXT<xsl:copy-of select="document('')//test"/></test> </xsl:template> </xsl:stylesheet> """)) result = xslt(etree.XML('<a/>')) root = result.getroot() self.assertEquals(root.tag, 'test') self.assertEquals(root[0].tag, 'test') self.assertEquals(root[0].text, 'TEXT') self.assertEquals(root[0][0].tag, '{http://www.w3.org/1999/XSL/Transform}copy-of')
def test_xslt_unicode(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"/> <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"?> <foo>\\uF8D2</foo> ''').decode("unicode_escape") self.assertEquals(expected, unicode(res))
def test_exslt_str_attribute_replace(self): tree = self.parse('<a><b>B</b><c>C</c></a>') style = self.parse('''\ <xsl:stylesheet version = "1.0" xmlns:xsl='http://www.w3.org/1999/XSL/Transform' xmlns:str="http://exslt.org/strings" extension-element-prefixes="str"> <xsl:template match="/"> <h1 class="{str:replace('abc', 'b', 'x')}">test</h1> </xsl:template> </xsl:stylesheet>''') st = etree.XSLT(style) res = st(tree) self.assertEquals('''\ <?xml version="1.0"?> <h1 class="axc">test</h1> ''', str(res))