Skip to content

Instantly share code, notes, and snippets.

@rklemme
Created January 8, 2015 07:35
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 rklemme/53d2400cf6251ddb4d47 to your computer and use it in GitHub Desktop.
Save rklemme/53d2400cf6251ddb4d47 to your computer and use it in GitHub Desktop.
Create XPath from XML node structure
def make_xpath(node, xpath = "//")
xpath << node.name
first = true
node.attributes.values.each do |attr|
if first
first = false
xpath << "["
else
xpath << " and "
end
xpath << "@" << attr.name << ' = "' << attr.value << '"'
end
node.children.each do |ch|
if ch.elem?
if first
first = false
xpath << "["
else
xpath << " and "
end
make_xpath(ch, xpath)
end
end
xpath << "]" unless first
xpath
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment