Esempio n. 1
0
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
Esempio n. 2
0
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
Esempio n. 3
0
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
Esempio n. 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))
Esempio n. 5
0
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)))
Esempio n. 6
0
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)
Esempio n. 7
0
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)))
Esempio n. 8
0
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)
Esempio n. 9
0
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