コード例 #1
0
ファイル: dynamic.py プロジェクト: abed-hawa/amara
def evaluate_function(context, string):
    """
    The dyn:evaluate function evaluates a string as an XPath expression and
    returns the resulting value, which might be a boolean, number, string,
    node set, result tree fragment or external object. The sole argument is
    the string to be evaluated. If the string is an invalid XPath expression,
    an empty node-set is returned.

    http://www.exslt.org/dyn/functions/evaluate/index.html
    """
    string = string.evaluate_as_string(context)
    try:
        expr = parse_xpath(string)
    except XPathError:
        lines = traceback.format_exception(*sys.exc_info())
        lines[:1] = [("Syntax error in XPath expression '%(expr)s', "
                      "lower-level traceback:\n") % {'expr': string}]
        context.processor.warning(''.join(lines))
        return datatypes.nodeset()
    try:
        result = expr.evaluate(context)
    except:
        lines = traceback.format_exception(*sys.exc_info())
        lines[:1] = [("Runtime error in XPath expression '%(expr)s', "
                      "lower-level traceback:\n") % {'expr': string}]
        context.processor.warning(''.join(lines))
        return datatypes.nodeset()
    return result
コード例 #2
0
ファイル: dynamic.py プロジェクト: mredar/amara
def evaluate_function(context, string):
    """
    The dyn:evaluate function evaluates a string as an XPath expression and
    returns the resulting value, which might be a boolean, number, string,
    node set, result tree fragment or external object. The sole argument is
    the string to be evaluated. If the string is an invalid XPath expression,
    an empty node-set is returned.

    http://www.exslt.org/dyn/functions/evaluate/index.html
    """
    string = string.evaluate_as_string(context)
    try:
        expr = parse_xpath(string)
    except XPathError:
        lines = traceback.format_exception(*sys.exc_info())
        lines[:1] = [("Syntax error in XPath expression '%(expr)s', "
                      "lower-level traceback:\n") % {
                          'expr': string
                      }]
        context.processor.warning(''.join(lines))
        return datatypes.nodeset()
    try:
        result = expr.evaluate(context)
    except:
        lines = traceback.format_exception(*sys.exc_info())
        lines[:1] = [("Runtime error in XPath expression '%(expr)s', "
                      "lower-level traceback:\n") % {
                          'expr': string
                      }]
        context.processor.warning(''.join(lines))
        return datatypes.nodeset()
    return result
コード例 #3
0
ファイル: dynamic.py プロジェクト: mredar/amara
def closure_function(context, nodeset, string):
    """
    The dyn:closure function creates a node set resulting from transitive
    closure of evaluating the expression passed as the second argument on
    each of the nodes passed as the first argument, then on the node set
    resulting from that and so on until no more nodes are found.

    http://www.exslt.org/dyn/functions/closure/index.html
    """
    nodeset = nodeset.evaluate_as_nodeset(context)
    string = string.evaluate_as_string(context)
    try:
        expr = parse_xpath(string)
    except XPathError:
        lines = traceback.format_exception(*sys.exc_info())
        lines[:1] = [("Syntax error in XPath expression '%(expr)s', "
                      "lower-level traceback:\n") % {
                          'expr': string
                      }]
        context.processor.warning(''.join(lines))
        return datatypes.nodeset()
    result = datatypes.nodeset()
    while nodeset:
        nodeset = _map(context, nodeset, expr)
        result.extend(nodeset)
    return result
コード例 #4
0
 def prepare(self, element, value):
     if value is None:
         if self.default is None:
             return None
         value = self.default
     try:
         return parse_xpath(value)
     except SyntaxError, error:
         raise XsltError(XsltError.INVALID_EXPRESSION,
                         value=value,
                         baseuri=element.baseUri,
                         line=element.lineNumber,
                         col=element.columnNumber,
                         msg=str(error))
コード例 #5
0
ファイル: dynamic.py プロジェクト: abed-hawa/amara
def sum_function(context, nodeset, string):
    """
    The dyn:sum function calculates the sum for the nodes passed as the first
    argument, where the value of each node is calculated dynamically using an
    XPath expression passed as a string as the second argument.

    http://www.exslt.org/dyn/functions/sum/index.html
    """
    nodeset = nodeset.evaluate_as_nodeset(context)
    string = string.evaluate_as_string(context)
    try:
        expr = parse_xpath(string)
    except XPathError:
        lines = traceback.format_exception(*sys.exc_info())
        lines[:1] = [("Syntax error in XPath expression '%(expr)s', "
                      "lower-level traceback:\n") % {'expr': string}]
        context.processor.warning(''.join(lines))
        return datatypes.nodeset()
    return sum(map(datatypes.number, _map(context, nodeset, expr)))
コード例 #6
0
ファイル: dynamic.py プロジェクト: abed-hawa/amara
def map_function(context, nodeset, string):
    """
    The dyn:map function evaluates the expression passed as the second
    argument for each of the nodes passed as the first argument, and returns
    a node set of those values.

    http://www.exslt.org/dyn/functions/map/index.html
    """
    nodeset = nodeset.evaluate_as_nodeset(context)
    string = string.evaluate_as_string(context)
    try:
        expr = parse_xpath(string)
    except XPathError:
        lines = traceback.format_exception(*sys.exc_info())
        lines[:1] = [("Syntax error in XPath expression '%(expr)s', "
                      "lower-level traceback:\n") % {'expr': string}]
        context.processor.warning(''.join(lines))
        return datatypes.nodeset()
    return _map(context, nodeset, expr)
コード例 #7
0
ファイル: dynamic.py プロジェクト: mredar/amara
def sum_function(context, nodeset, string):
    """
    The dyn:sum function calculates the sum for the nodes passed as the first
    argument, where the value of each node is calculated dynamically using an
    XPath expression passed as a string as the second argument.

    http://www.exslt.org/dyn/functions/sum/index.html
    """
    nodeset = nodeset.evaluate_as_nodeset(context)
    string = string.evaluate_as_string(context)
    try:
        expr = parse_xpath(string)
    except XPathError:
        lines = traceback.format_exception(*sys.exc_info())
        lines[:1] = [("Syntax error in XPath expression '%(expr)s', "
                      "lower-level traceback:\n") % {
                          'expr': string
                      }]
        context.processor.warning(''.join(lines))
        return datatypes.nodeset()
    return sum(map(datatypes.number, _map(context, nodeset, expr)))
コード例 #8
0
ファイル: dynamic.py プロジェクト: mredar/amara
def map_function(context, nodeset, string):
    """
    The dyn:map function evaluates the expression passed as the second
    argument for each of the nodes passed as the first argument, and returns
    a node set of those values.

    http://www.exslt.org/dyn/functions/map/index.html
    """
    nodeset = nodeset.evaluate_as_nodeset(context)
    string = string.evaluate_as_string(context)
    try:
        expr = parse_xpath(string)
    except XPathError:
        lines = traceback.format_exception(*sys.exc_info())
        lines[:1] = [("Syntax error in XPath expression '%(expr)s', "
                      "lower-level traceback:\n") % {
                          'expr': string
                      }]
        context.processor.warning(''.join(lines))
        return datatypes.nodeset()
    return _map(context, nodeset, expr)
コード例 #9
0
ファイル: dynamic.py プロジェクト: abed-hawa/amara
def closure_function(context, nodeset, string):
    """
    The dyn:closure function creates a node set resulting from transitive
    closure of evaluating the expression passed as the second argument on
    each of the nodes passed as the first argument, then on the node set
    resulting from that and so on until no more nodes are found.

    http://www.exslt.org/dyn/functions/closure/index.html
    """
    nodeset = nodeset.evaluate_as_nodeset(context)
    string = string.evaluate_as_string(context)
    try:
        expr = parse_xpath(string)
    except XPathError:
        lines = traceback.format_exception(*sys.exc_info())
        lines[:1] = [("Syntax error in XPath expression '%(expr)s', "
                      "lower-level traceback:\n") % {'expr': string}]
        context.processor.warning(''.join(lines))
        return datatypes.nodeset()
    result = datatypes.nodeset()
    while nodeset:
        nodeset = _map(context, nodeset, expr)
        result.extend(nodeset)
    return result