File: extelem.py

package info (click to toggle)
libxslt 1.1.43-0.2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 25,188 kB
  • sloc: xml: 66,120; ansic: 36,035; sh: 4,582; python: 3,206; makefile: 1,378; javascript: 470; perl: 34
file content (82 lines) | stat: -rwxr-xr-x 2,010 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#!/usr/bin/env python
import sys
import string
import setup_test
import libxml2
# Memory debug specific
libxml2.debugMemory(1)
import libxslt

EXT_URL="http://example.com/foo"

insertNodeName = None
transformNodeName = None

def compile_test(style, inst, func):
    pass

def transform_test(ctx, node, inst, comp):
    global insertNodeName

    #
    # Small check to verify the context is correctly accessed
    #
    try:
        #
        # FIXME add some more sanity checks
        #
        tctxt = libxslt.transformCtxt(_obj=ctx)
        insertNodeName = tctxt.insertNode().name

        # FIXME find and confirm the note being replaced is called 'test'
        # transformNodeName = libxml2.xmlNode(inst).name
    except:
        pass

    tctxt.insertNode().addContent('SUCCESS')
    


styledoc = libxml2.parseDoc("""
<xsl:stylesheet version='1.0'
  xmlns:xsl='http://www.w3.org/1999/XSL/Transform'
  xmlns:foo='%s'
  extension-element-prefixes='foo'>

  <xsl:template match='/'>
    <article><foo:test>FAILURE</foo:test></article>
    <deeper><article><foo:test>something<foo:test>nested</foo:test>even</foo:test></article></deeper>
  </xsl:template>
</xsl:stylesheet>
""" % EXT_URL)

style = libxslt.parseStylesheetDoc(styledoc)
libxslt.registerExtModuleElement("test", EXT_URL, compile_test, transform_test)
doc = libxml2.parseDoc("<doc/>")
result = style.applyStylesheet(doc, None)
style.freeStylesheet()
doc.freeDoc()

root = result.children

if root.name != "article":
    print("Unexpected root node name")
    sys.exit(1)
if root.content != "SUCCESS":
    print("Unexpected root node content, extension function failed")
    sys.exit(1)
if insertNodeName != 'article':
    print("The function callback failed to access its context")
    sys.exit(1)

result.dump(sys.stdout)
result.freeDoc()

# Memory debug specific
libxslt.cleanup()
if libxml2.debugMemory(1) == 0:
    print("OK")
else:
    print("Memory leak %d bytes" % (libxml2.debugMemory(1)))
    libxml2.dumpMemory()
    sys.exit(255)