Ejemplo n.º 1
0
 def instantiate(self, context):
     context.namespaces = self.namespaces
     targets = self.select.evaluate_as_nodeset(context)
     if not targets:
         raise XUpdateError(XUpdateError.INVALID_SELECT)
     for target in targets:
         if target.xml_type == tree.element.xml_type:
             context.push_tree_writer(target.xml_base)
             for primitive in self:
                 primitive.instantiate(context)
             writer = context.pop_writer()
             tr = writer.get_result()
             while target.xml_first_child:
                 target.xml_remove(target.xml_first_child)
             while tr.xml_first_child:
                 target.xml_append(tr.xml_first_child)
         elif target.xml_type in (tree.attribute.xml_type,
                                  tree.text.xml_type,
                                  tree.comment.xml_type,
                                  tree.processing_instruction.xml_type):
             context.push_string_writer(errors=False)
             for primitive in self:
                 primitive.instantiate(context)
             writer = context.pop_writer()
             value = writer.get_result()
             if not value and target.xml_type == tree.text.xml_type:
                 target.xml_parent.xml_remove(target)
             else:
                 target.xml_value = value
     return
Ejemplo n.º 2
0
    def instantiate(self, context):
        context.push_string_writer(errors=False)
        for primitive in self:
            primitive.instantiate(context)
        writer = context.pop_writer()
        name = writer.get_result()
        prefix, local = splitqname(name)
        if prefix:
            namespace = self.namespaces[prefix]
        else:
            namespace = None

        context.namespaces = self.namespaces
        targets = self.select.evaluate_as_nodeset(context)
        if not targets:
            raise XUpdateError(XUpdateError.INVALID_SELECT)
        for target in targets:
            parent = target.xml_parent
            if target.xml_type == tree.attribute.xml_type:
                parent.xml_attributes[namespace, name] = target.xml_value
            elif target.xml_type == tree.processing_instruction.xml_type:
                pi = tree.processing_instruction(name, target.xml_data)
                parent.xml_replace-child(pi, target)
            elif target.xml_type == tree.element.xml_type:
                #FIXME: Use regular constructor. No more DOM factory
                element = tree.element(namespace, name)
                # Copy any existing attributes to the newly created element
                if target.xml_attributes:
                    for (ns, qname), value in target.xml_attributes.iteritems():
                        element.xml_attributes[ns, qname] = value
                # Now copy any children as well
                while target.xml_first_child:
                    element.xml_append(target.xml_first_child)
                parent.xml_replace(target, element)
        return
Ejemplo n.º 3
0
 def instantiate(self, context):
     if self.select:
         context.namespaces = self.namespaces
         value = self.select.evaluate(context)
     else:
         context.push_string_writer(errors=False)
         for primitive in self:
             primitive.instantiate(context)
         writer = context.pop_writer()
         value = writer.get_result()
     context.variables[self.name] = value
     return