# File htree/elem.rb, line 154
    def subst_subnode(pairs)
      hash = {}
      pairs.each {|index, value|
        case index
        when Name, Integer
        when String
          index = Name.parse_attribute_name(index, DefaultContext)
        else
          raise TypeError, "invalid index: #{index.inspect}"
        end
        value = value.to_node if HTree::Location === value
        case value
        when Node
          value = [value]
        when String
          value = [value]
        when Array
          value = value.dup
        when nil
          value = []
        else
          raise TypeError, "invalid value: #{value.inspect}"
        end
        value.map! {|v|
          v = v.to_node if HTree::Location === v
          case v
          when Node
            v
          when String
            Text.new(v)
          else
            raise TypeError, "invalid value: #{v.inspect}"
          end
        }
        if !hash.include?(index)
          hash[index] = []
        end
        hash[index].concat value
      }

      attrs = []
      @stag.attributes.each {|k, v|
        if hash.include? k
          v = hash[k]
          if !v.empty?
            attrs << {k=>Text.concat(*v)}
          end
          hash.delete k
        else
          attrs << {k=>v}
        end
      }
      hash.keys.each {|k|
        if Name === k
          v = hash[k]
          if !v.empty?
            attrs << {k=>Text.concat(*v)}
          end
          hash.delete k
        end
      }

      children_left = []
      children = @children.dup
      children_right = []

      hash.keys.sort.each {|index|
        value = hash[index]
        if index < 0
          children_left << value
        elsif children.length <= index
          children_right << value
        else
          children[index] = value
        end
      }

      children = [children_left, children, children_right].flatten

      if children.empty? && @empty
        Elem.new(
          @stag.element_name,
          @stag.context,
          *attrs)
      else 
        Elem.new(
          @stag.element_name,
          @stag.context,
          children,
          *attrs)
      end
    end