Created
January 8, 2015 07:35
-
-
Save rklemme/53d2400cf6251ddb4d47 to your computer and use it in GitHub Desktop.
Create XPath from XML node structure
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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