Skip to content

Instantly share code, notes, and snippets.

@yorickpeterse
Created August 11, 2015 15:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yorickpeterse/869a45eea3d99438e45b to your computer and use it in GitHub Desktop.
Save yorickpeterse/869a45eea3d99438e45b to your computer and use it in GitHub Desktop.
def on_call_id(input, arg)
orig_input = original_input_literal
node = node_literal
ids_var = unique_literal('ids')
matched = unique_literal('id_matched')
id_str_var = unique_literal('id_string')
attr_var = unique_literal('attr')
matched_assign = matched.assign(literal(XML::NodeSet).new)
# When using some sort of path we'll want the text of all matched nodes.
if return_nodeset?(arg)
id_assign = ids_var.assign(literal(:[]))
.followed_by(process(arg, input) { |node| ids_var << node.text })
# For everything else we'll cast the value to a string and split it on
# every space.
else
conversion = literal(Conversion).to_string(ids_var).split(string(' '))
id_assign = ids_var.assign(process(arg, input))
.followed_by(ids_var.assign(conversion))
end
id_str_assign = id_str_var.assign(string('id'))
each_node = orig_input.each_node.add_block(node) do
node.is_a?(XML::Element).if_true do
assign = attr_var.assign(node.attribute(id_str_var))
compare = attr_var.and(ids_var.include?(attr_var.value)).if_true do
matched << node
end
assign.followed_by(compare)
end
end
matched_assign.followed_by(id_assign)
.followed_by(id_str_assign)
.followed_by(each_node)
.followed_by(matched)
end
lambda do |node, variables = nil|
original_input = node
id_matched2 = Oga::XML::NodeSet.new
id1 = []
node.children.each do |node|
if (node.is_a?(Oga::XML::Attribute) || node.is_a?(Oga::XML::Element)) && node.name == "root"
index = 1
node.children.each do |node|
if (node.is_a?(Oga::XML::Attribute) || node.is_a?(Oga::XML::Element)) && node.name == "a"
if index == 2
id1.<<(node.text)
end
index = index.+(1)
end
end
end
end
id_string3 = "id"
original_input.each_node do |node|
if node.is_a?(Oga::XML::Element)
attr4 = node.attribute(id_string3)
if attr4 && id1.include?(attr4.value)
id_matched2.<<(node)
end
end
end
id_matched2
end
def on_call_id(context, expression)
id = process(expression, context)
nodes = XML::NodeSet.new
# Based on Nokogiri's/libxml behaviour it appears that when using a node
# set the text of the set is used as the ID.
id = id.is_a?(XML::NodeSet) ? id.text : id.to_s
ids = id.split(' ')
@document.each_node do |node|
next unless node.is_a?(XML::Element)
attr = node.attribute('id')
if attr and ids.include?(attr.value)
nodes << node
end
end
nodes
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment