示例#1
0
    def test_xmlschema_nested_resolvers(self):
        # test that resolvers work in a nested fashion.

        resolver_schema = self.resolver_schema_ext

        class res_nested(etree.Resolver):
            def __init__(self, ext_schema):
                self.ext_schema = ext_schema

            def resolve(self, url, id, context):
                assert url == 'YYY.xsd'
                return self.resolve_string(self.ext_schema, context)

        class res(etree.Resolver):
            def __init__(self, ext_schema_1, ext_schema_2):
                self.ext_schema_1 = ext_schema_1
                self.ext_schema_2 = ext_schema_2

            def resolve(self, url, id, context):
                assert url == 'XXX.xsd'

                new_parser = etree.XMLParser()
                new_parser.resolvers.add(res_nested(self.ext_schema_2))
                new_schema_doc = etree.parse(self.ext_schema_1, parser = new_parser)
                new_schema = etree.XMLSchema(new_schema_doc)

                return self.resolve_string(resolver_schema, context)

        parser = etree.XMLParser()
        parser.resolvers.add(res(self.resolver_schema_int2, self.resolver_schema_ext))
        schema_doc = etree.parse(self.resolver_schema_int, parser = parser)
        schema = etree.XMLSchema(schema_doc)
示例#2
0
 def test_xslt_document_parse(self):
     # make sure document('') works from loaded files
     xslt = etree.XSLT(etree.parse(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")
示例#3
0
 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")
示例#4
0
 def _parse_file(self):
     pos = self._file.tell()
     self._target.seek(0)
     try:
         return etree.parse(self._target)
     finally:
         self._target.seek(pos)
示例#5
0
    def test_xmlschema_nested_resolvers(self):
        """Test that resolvers work in a nested fashion."""

        resolver_schema = self.resolver_schema_ext

        class res_nested(etree.Resolver):
            def __init__(self, ext_schema):
                self.ext_schema = ext_schema

            def resolve(self, url, id, context):
                assert url == 'YYY.xsd'
                return self.resolve_string(self.ext_schema, context)

        class res(etree.Resolver):
            def __init__(self, ext_schema_1, ext_schema_2):
                self.ext_schema_1 = ext_schema_1
                self.ext_schema_2 = ext_schema_2

            def resolve(self, url, id, context):
                assert url == 'XXX.xsd'

                new_parser = etree.XMLParser()
                new_parser.resolvers.add(res_nested(self.ext_schema_2))
                new_schema_doc = etree.parse(self.ext_schema_1,
                                             parser=new_parser)
                new_schema = etree.XMLSchema(new_schema_doc)

                return self.resolve_string(resolver_schema, context)

        parser = etree.XMLParser()
        parser.resolvers.add(
            res(self.resolver_schema_int2, self.resolver_schema_ext))
        schema_doc = etree.parse(self.resolver_schema_int, parser=parser)
        schema = etree.XMLSchema(schema_doc)
 def _parse_file(self):
     pos = self._file.tell()
     self._target.seek(0)
     try:
         return etree.parse(self._target)
     finally:
         self._target.seek(pos)
示例#7
0
 def test_xmlschema_resolvers_root(self):
     # test that the default resolver will get called if there's no
     # specific parser resolver.
     root_resolver = self.simple_resolver(self.resolver_schema_ext)
     etree.get_default_parser().resolvers.add(root_resolver)
     schema_doc = etree.parse(self.resolver_schema_int)
     schema = etree.XMLSchema(schema_doc)
     etree.get_default_parser().resolvers.remove(root_resolver)
示例#8
0
 def test_xmlschema_resolvers_root(self):
     """Test that the default resolver will get called if there's no
     specific parser resolver."""
     root_resolver = self.simple_resolver(self.resolver_schema_ext)
     etree.get_default_parser().resolvers.add(root_resolver)
     schema_doc = etree.parse(self.resolver_schema_int)
     schema = etree.XMLSchema(schema_doc)
     etree.get_default_parser().resolvers.remove(root_resolver)
示例#9
0
文件: test_dtd.py 项目: zym1010/lxml
    def test_parse_file_dtd(self):
        parser = etree.XMLParser(attribute_defaults=True)

        tree = etree.parse(fileInTestDir('test.xml'), parser)
        root = tree.getroot()

        self.assertEqual("valueA", root.get("default"))
        self.assertEqual("valueB", root[0].get("default"))
示例#10
0
            def resolve(self, url, id, context):
                assert url == 'XXX.xsd'

                new_parser = etree.XMLParser()
                new_parser.resolvers.add(res_nested(self.ext_schema_2))
                new_schema_doc = etree.parse(self.ext_schema_1, parser = new_parser)
                new_schema = etree.XMLSchema(new_schema_doc)

                return self.resolve_string(resolver_schema, context)
示例#11
0
 def test_xslt_document_parse(self):
     # make sure document('') works from loaded files
     xslt = etree.XSLT(etree.parse(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')
示例#12
0
            def resolve(self, url, id, context):
                assert url == 'XXX.xsd'

                new_parser = etree.XMLParser()
                new_parser.resolvers.add(res_nested(self.ext_schema_2))
                new_schema_doc = etree.parse(self.ext_schema_1, parser = new_parser)
                new_schema = etree.XMLSchema(new_schema_doc)

                return self.resolve_string(resolver_schema, context)
示例#13
0
 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')
示例#14
0
    def test_parse_file_dtd(self):
        parser = etree.XMLParser(attribute_defaults=True)

        tree = etree.parse(fileInTestDir('test.xml'), parser)
        root = tree.getroot()

        self.assertEquals(
            "valueA",
            root.get("default"))
        self.assertEquals(
            "valueB",
            root[0].get("default"))
    def test_parser_based_lookup(self):
        class TestElement(etree.ElementBase):
            FIND_ME = "parser_based"

        lookup = etree.ParserBasedElementClassLookup()
        etree.set_element_class_lookup(lookup)

        class MyLookup(etree.CustomElementClassLookup):
            def lookup(self, t, d, ns, name):
                return TestElement

        parser = etree.XMLParser()
        parser.set_element_class_lookup(MyLookup())

        root = etree.parse(BytesIO(xml_str), parser).getroot()
        self.assertEquals(root.FIND_ME, TestElement.FIND_ME)
        self.assertEquals(root[0].FIND_ME, TestElement.FIND_ME)

        root = etree.parse(BytesIO(xml_str)).getroot()
        self.assertFalse(hasattr(root, "FIND_ME"))
        self.assertFalse(hasattr(root[0], "FIND_ME"))
示例#16
0
    def test_parser_based_lookup(self):
        class TestElement(etree.ElementBase):
            FIND_ME = "parser_based"

        lookup = etree.ParserBasedElementClassLookup()
        etree.set_element_class_lookup(lookup)

        class MyLookup(etree.CustomElementClassLookup):
            def lookup(self, t, d, ns, name):
                return TestElement

        parser = etree.XMLParser()
        parser.set_element_class_lookup(MyLookup())

        root = etree.parse(BytesIO(xml_str), parser).getroot()
        self.assertEqual(root.FIND_ME, TestElement.FIND_ME)
        self.assertEqual(root[0].FIND_ME, TestElement.FIND_ME)

        root = etree.parse(BytesIO(xml_str)).getroot()
        self.assertFalse(hasattr(root, 'FIND_ME'))
        self.assertFalse(hasattr(root[0], 'FIND_ME'))
示例#17
0
    def test_xmlschema_resolvers_noroot(self):
        """Test that the default resolver will not get called when a more
        specific resolver is registered."""
        class res_root(etree.Resolver):
            def resolve(self, url, id, context):
                assert False
                return None

        root_resolver = res_root()
        etree.get_default_parser().resolvers.add(root_resolver)

        parser = etree.XMLParser()
        parser.resolvers.add(self.simple_resolver(self.resolver_schema_ext))

        schema_doc = etree.parse(self.resolver_schema_int, parser=parser)
        schema = etree.XMLSchema(schema_doc)
        etree.get_default_parser().resolvers.remove(root_resolver)
示例#18
0
    def test_xmlschema_resolvers_noroot(self):
        # test that the default resolver will not get called when a
        # more specific resolver is registered.

        class res_root(etree.Resolver):
            def resolve(self, url, id, context):
                assert False
                return None

        root_resolver = res_root()
        etree.get_default_parser().resolvers.add(root_resolver)

        parser = etree.XMLParser()
        parser.resolvers.add(self.simple_resolver(self.resolver_schema_ext))

        schema_doc = etree.parse(self.resolver_schema_int, parser = parser)
        schema = etree.XMLSchema(schema_doc)
        etree.get_default_parser().resolvers.remove(root_resolver)
示例#19
0
 def test_xmlschema_resolvers(self):
     # test that resolvers work with schema.
     parser = etree.XMLParser()
     parser.resolvers.add(self.simple_resolver(self.resolver_schema_ext))
     schema_doc = etree.parse(self.resolver_schema_int, parser = parser)
     schema = etree.XMLSchema(schema_doc)
示例#20
0
 def _parse_file(self):
     self._file.seek(0)
     return etree.parse(self._file)
示例#21
0
 def test_xslt_include_from_filelike(self):
     f = open(fileInTestDir('test1.xslt'), 'rb')
     tree = etree.parse(f)
     f.close()
     st = etree.XSLT(tree)
示例#22
0
 def test_xslt_include(self):
     tree = etree.parse(fileInTestDir('test1.xslt'))
     st = etree.XSLT(tree)
示例#23
0
 def test_xslt_document_parse_deny_all(self):
     access_control = etree.XSLTAccessControl.DENY_ALL
     xslt = etree.XSLT(etree.parse(fileInTestDir("test-document.xslt")),
                       access_control = access_control)
     self.assertRaises(etree.XSLTApplyError, xslt, etree.XML('<a/>'))
示例#24
0
 def _parse_file(self):
     self._target.seek(0)
     return etree.parse(self._target)
示例#25
0
 def test_xmlschema_resolvers(self):
     """Test that resolvers work with schema."""
     parser = etree.XMLParser()
     parser.resolvers.add(self.simple_resolver(self.resolver_schema_ext))
     schema_doc = etree.parse(self.resolver_schema_int, parser=parser)
     schema = etree.XMLSchema(schema_doc)
示例#26
0
 def _parse_file(self):
     self._tmpfile.seek(0)
     return etree.parse(self._tmpfile)
示例#27
0
        expr = etree.ETXPath(_bytes("/a/{http://nsa/\\uf8d2}b").decode("unicode_escape"))
        r = expr(x)
        self.assertEquals(1, len(r))
        self.assertEquals(_bytes('{http://nsa/\\uf8d2}b').decode("unicode_escape"), r[0].tag)

        expr = etree.ETXPath(_bytes("/a/{http://nsb/\\uf8d1}b").decode("unicode_escape"))
        r = expr(x)
        self.assertEquals(1, len(r))
        self.assertEquals(_bytes('{http://nsb/\\uf8d1}b').decode("unicode_escape"), r[0].tag)

SAMPLE_XML = etree.parse(BytesIO("""
<body>
  <tag>text</tag>
  <section>
    <tag>subtext</tag>
  </section>
  <tag />
  <tag />
</body>
"""))

def tag(elem):
    return elem.tag

def stringTest(ctxt, s1):
    return "Hello "+s1
    
def floatTest(ctxt, f1):
    return f1+4

def booleanTest(ctxt, b1):
示例#28
0
        expr = etree.ETXPath(_bytes("/a/{http://nsa/\\uf8d2}b").decode("unicode_escape"))
        r = expr(x)
        self.assertEquals(1, len(r))
        self.assertEquals(_bytes('{http://nsa/\\uf8d2}b').decode("unicode_escape"), r[0].tag)

        expr = etree.ETXPath(_bytes("/a/{http://nsb/\\uf8d1}b").decode("unicode_escape"))
        r = expr(x)
        self.assertEquals(1, len(r))
        self.assertEquals(_bytes('{http://nsb/\\uf8d1}b').decode("unicode_escape"), r[0].tag)

SAMPLE_XML = etree.parse(BytesIO("""
<body>
  <tag>text</tag>
  <section>
    <tag>subtext</tag>
  </section>
  <tag />
  <tag />
</body>
"""))

def tag(elem):
    return elem.tag

def tag_or_value(elem):
    return getattr(elem, 'tag', elem)

def stringTest(ctxt, s1):
    return "Hello "+s1

def stringListTest(ctxt, s1):
示例#29
0
 def test_xslt_document_parse_deny(self):
     access_control = etree.XSLTAccessControl(read_file=False)
     xslt = etree.XSLT(etree.parse(fileInTestDir("test-document.xslt")), access_control=access_control)
     self.assertRaises(etree.XSLTApplyError, xslt, etree.XML("<a/>"))
示例#30
0
 def test_unicode_parse_stringio(self):
     el = etree.parse(StringIO(_str('<p>%s</p>') % uni)).getroot()
     self.assertEquals(uni, el.text)