コード例 #1
0
def transform(source, transforms, params=None, output=None):
    """
    Convenience function for applying an XSLT transform.  Returns
    a result object.

    source - XML source document in the form of a string (not Unicode
             object), file-like object (stream), file path, URI or
             amara.lib.inputsource instance.  If string or stream
             it must be self-contained  XML (i.e. not requiring access to
             any other resource such as external entities or includes)
    transforms - XSLT document (or list thereof) in the form of a string, stream, URL,
                file path or amara.lib.inputsource instance
    params - optional dictionary of stylesheet parameters, the keys of
             which may be given as unicode objects if they have no namespace,
             or as (uri, localname) tuples if they do.
    output - optional file-like object to which output is written (incrementally, as processed)
    """
    #do the imports within the function: a tad bit less efficient, but
    #avoid circular crap
    from amara.lib import inputsource
    from amara.xpath.util import parameterize
    from amara.xslt.result import streamresult, stringresult
    from amara.xslt.processor import processor
    params = parameterize(params) if params else {}
    proc = processor()
    if isinstance(transforms, (list, tuple)):
        for transform in transforms:
            proc.append_transform(inputsource(transform))
    else:
        proc.append_transform(inputsource(transforms))
    if output is not None:
        result = streamresult(output)
    else:
        result = stringresult()
    return proc.run(inputsource(source), params, result)
コード例 #2
0
ファイル: __init__.py プロジェクト: abed-hawa/amara
def transform(source, transforms, params=None, output=None):
    """
    Convenience function for applying an XSLT transform.  Returns
    a result object.

    source - XML source document in the form of a string (not Unicode
             object), file-like object (stream), file path, URI or
             amara.lib.inputsource instance.  If string or stream
             it must be self-contained  XML (i.e. not requiring access to
             any other resource such as external entities or includes)
    transforms - XSLT document (or list thereof) in the form of a string, stream, URL,
                file path or amara.lib.inputsource instance
    params - optional dictionary of stylesheet parameters, the keys of
             which may be given as unicode objects if they have no namespace,
             or as (uri, localname) tuples if they do.
    output - optional file-like object to which output is written (incrementally, as processed)
    """
    #do the imports within the function: a tad bit less efficient, but
    #avoid circular crap
    from amara.lib import inputsource
    from amara.xpath.util import parameterize
    from amara.xslt.result import streamresult, stringresult
    from amara.xslt.processor import processor
    params = parameterize(params) if params else {}
    proc = processor()
    if isinstance(transforms, (list, tuple)):
        for transform in transforms:
            proc.append_transform(inputsource(transform))
    else:
        proc.append_transform(inputsource(transforms))
    if output is not None:
        result = streamresult(output)
    else:
        result = stringresult()
    return proc.run(inputsource(source), params, result)
コード例 #3
0
ファイル: test_strip_space.py プロジェクト: bmackattack/amara
def setup_blank_node():
    global source
    global trans
    global xslt_proc

    _source1 = '''<?xml version="1.0"?>
<document>
<text>   </text>
</document>'''

    _trans1 = '''<?xml version='1.0'?>
<xsl:stylesheet version="1.0"
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="text"/>
  <xsl:strip-space elements="*"/>

  <xsl:template match="/">
    <xsl:apply-templates select="//text"/>
  </xsl:template>

<xsl:template match="text">
Chars: <xsl:value-of select="string-length(text())"/>
</xsl:template>

</xsl:stylesheet>'''

    xslt_proc = processor()

    source = inputsource(_source1, None)
    trans = inputsource(_trans1, None)
コード例 #4
0
ファイル: test_strip_space.py プロジェクト: bmackattack/amara
def setup_blank_text():
    global source
    global trans
    global xslt_proc

    _source1 = '''<?xml version="1.0"?>
<test>
  <item/>
  <item/>
  <item/>
</test>
'''

    _trans1 = '''<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:strip-space  elements="*"/>
  <xsl:template  match="/">
    <test>
      <xsl:apply-templates/>
    </test>
  </xsl:template>
  <xsl:template  match="item">
    <no>
      <xsl:value-of select="position()"/>
    </no>
  </xsl:template>
</xsl:stylesheet>
'''

    xslt_proc = processor()

    source = inputsource(_source1, None)
    trans = inputsource(_trans1, None)
コード例 #5
0
ファイル: __init__.py プロジェクト: abed-hawa/amara
 def test_processor(self):
     P = processor()
     for transform in self.transform:
         P.append_transform(transform)
     parameters = self.parameters
     if parameters:
         parameters = util.parameterize(parameters)
     result = P.run(self.source, parameters=parameters)
     self._assert_result(result)
コード例 #6
0
ファイル: __init__.py プロジェクト: mredar/amara
 def test_processor(self):
     P = processor()
     for transform in self.transform:
         P.append_transform(transform)
     parameters = self.parameters
     if parameters:
         parameters = util.parameterize(parameters)
     result = P.run(self.source, parameters=parameters)
     self._assert_result(result)
コード例 #7
0
ファイル: xslt_support.py プロジェクト: mredar/amara
def _run(source_xml, transform_xml, expected, parameters,
         compare_method, source_uri=None, transform_uri=None,
         processor_kwargs={}):
    P = processor(**processor_kwargs)
    source = inputsource(source_xml, source_uri)
    transform = inputsource(transform_xml, transform_uri)
    P.append_transform(transform)
    if parameters is not None:
        parameters = util.parameterize(parameters)
    result = str(P.run(source, parameters=parameters))
    try:
        diff = compare_method(result, expected)
        diff = list(diff)
        assert not diff, (source_xml, transform_xml, result, expected, diff)
    except Exception, err:
        # I don't have a quick way to tell which string caused
        # the error, so let the person debugging figure it out.
        print "=== RESULT ==="
        print result
        print "=== EXPECTED ==="
        print expected
        print "=== DONE ==="
        raise