Module: OM::XML::TermXpathGenerator
- Defined in:
- lib/om/xml/term_xpath_generator.rb
Class Method Summary collapse
-
.add_node_index_predicate(xpath_query, array_index_value) ⇒ Object
Adds xpath xpath node index predicate to the end of your xpath query Example: add_node_index_predicate(“//oxns:titleInfo”,0) => “//oxns:titleInfo”.
-
.add_position_predicate(xpath_query, array_index_value) ⇒ Object
Adds xpath:position() method call to the end of your xpath query Examples: .
- .add_predicate(xpath_query, predicate) ⇒ Object
- .delimited_list(values_array, delimiter = ", ") ⇒ Object
- .generate_absolute_xpath(mapper) ⇒ Object
- .generate_constrained_xpath(mapper) ⇒ Object
- .generate_relative_xpath(mapper) ⇒ Object
- .generate_xpath(mapper, type) ⇒ Object
-
.generate_xpath_with_indexes(terminology, *pointers) ⇒ Object
Use the given
terminology
to generate an xpath with (optional) node indexes for each of the term pointers.
Class Method Details
.add_node_index_predicate(xpath_query, array_index_value) ⇒ Object
Adds xpath xpath node index predicate to the end of your xpath query Example: add_node_index_predicate(“//oxns:titleInfo”,0)
=> "//oxns:titleInfo[1]"
add_node_index_predicate(“//oxns:titleInfo”,0)
=> "//oxns:titleInfo[@lang=\"finnish\"][1]"
195 196 197 198 |
# File 'lib/om/xml/term_xpath_generator.rb', line 195 def self.add_node_index_predicate(xpath_query, array_index_value) modified_query = xpath_query.dup modified_query << "[#{array_index_value + 1}]" end |
.add_position_predicate(xpath_query, array_index_value) ⇒ Object
Adds xpath:position() method call to the end of your xpath query Examples:
add_position_predicate(“//oxns:titleInfo”,0)
> “//oxns:titleInfo”
add_position_predicate(“//oxns:titleInfo”,0)
> “//oxns:titleInfo[@lang="finnish" and position()=1]”
208 209 210 211 |
# File 'lib/om/xml/term_xpath_generator.rb', line 208 def self.add_position_predicate(xpath_query, array_index_value) position_function = "position()=#{array_index_value + 1}" self.add_predicate(xpath_query, position_function) end |
.add_predicate(xpath_query, predicate) ⇒ Object
213 214 215 216 217 218 219 220 221 222 |
# File 'lib/om/xml/term_xpath_generator.rb', line 213 def self.add_predicate(xpath_query, predicate) modified_query = xpath_query.dup # if xpath_query.include?("]") if xpath_query[xpath_query.length-1..xpath_query.length] == "]" modified_query.insert(xpath_query.rindex("]"), " and #{predicate}") else modified_query << "[#{predicate}]" end return modified_query end |
.delimited_list(values_array, delimiter = ", ") ⇒ Object
184 185 186 |
# File 'lib/om/xml/term_xpath_generator.rb', line 184 def self.delimited_list( values_array, delimiter=", ") result = values_array.collect{|a| a + delimiter}.to_s.chomp(delimiter) end |
.generate_absolute_xpath(mapper) ⇒ Object
40 41 42 43 44 45 46 47 |
# File 'lib/om/xml/term_xpath_generator.rb', line 40 def self.generate_absolute_xpath(mapper) relative = generate_relative_xpath(mapper) if mapper.parent.nil? return "//#{relative}" else return mapper.parent.xpath_absolute + "/" + relative end end |
.generate_constrained_xpath(mapper) ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/om/xml/term_xpath_generator.rb', line 49 def self.generate_constrained_xpath(mapper) if mapper.namespace_prefix.nil? complete_prefix = "" else complete_prefix = mapper.namespace_prefix + ":" end absolute = generate_absolute_xpath(mapper) constraint_predicates = [] arguments_for_contains_function = [] if !mapper.default_content_path.nil? arguments_for_contains_function << "#{complete_prefix}#{mapper.default_content_path}" end # If no subelements have been specified to search within, set contains function to search within the current node if arguments_for_contains_function.empty? arguments_for_contains_function << "." end arguments_for_contains_function << "\":::constraint_value:::\"" contains_function = "contains(#{delimited_list(arguments_for_contains_function)})" template = add_predicate(absolute, contains_function) return template.gsub( /:::(.*?):::/ ) { '#{'+$1+'}' }.gsub('"', '\"') end |
.generate_relative_xpath(mapper) ⇒ Object
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/om/xml/term_xpath_generator.rb', line 3 def self.generate_relative_xpath(mapper) template = "" predicates = [] if mapper.namespace_prefix.nil? complete_prefix = "" else complete_prefix = mapper.namespace_prefix + ":" end if mapper.path.kind_of?(Hash) if mapper.path.has_key?(:attribute) base_path = "@"+mapper.path[:attribute] else raise "#{mapper.path} is an invalid path for an OM::XML::Term. You should provide either a string or {:attributes=>XXX}" end else unless mapper.namespace_prefix.nil? template << complete_prefix end base_path = mapper.path end template << base_path unless mapper.attributes.nil? mapper.attributes.each_pair do |attr_name, attr_value| predicates << "@#{attr_name}=\"#{attr_value}\"" end end unless predicates.empty? template << "["+ delimited_list(predicates, " and ")+"]" end return template end |
.generate_xpath(mapper, type) ⇒ Object
78 79 80 81 82 83 84 85 86 87 |
# File 'lib/om/xml/term_xpath_generator.rb', line 78 def self.generate_xpath(mapper, type) case type when :relative self.generate_relative_xpath(mapper) when :absolute self.generate_absolute_xpath(mapper) when :constrained self.generate_constrained_xpath(mapper) end end |
.generate_xpath_with_indexes(terminology, *pointers) ⇒ Object
Use the given terminology
to generate an xpath with (optional) node indexes for each of the term pointers. Ex. OM::XML::TermXpathGenerator.xpath_with_indexes(my_terminology, :conference=>0, :role=>1, :text )
will yield an xpath similar to this: '//oxns:name[@type="conference"][1]/oxns:role[2]/oxns:roleTerm[@type="text"]'
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 |
# File 'lib/om/xml/term_xpath_generator.rb', line 92 def self.generate_xpath_with_indexes(terminology, *pointers) if pointers.first.nil? root_term = terminology.root_terms.first if root_term.nil? return "/" else return root_term.xpath end end query_constraints = nil if pointers.length > 1 && pointers.last.kind_of?(Hash) query_constraints = pointers.pop end if pointers.length == 1 && pointers.first.instance_of?(String) return xpath_query = pointers.first end # if pointers.first.kind_of?(String) # return pointers.first # end keys = [] xpath = "//" pointers = OM.destringify(pointers) pointers.each_with_index do |pointer, pointer_index| if pointer.kind_of?(Hash) k = pointer.keys.first index = pointer[k] else k = pointer index = nil end keys << k term = terminology.retrieve_term(*keys) # Return nil if there is no term to work with if term.nil? then return nil end # If we've encountered a NamedTermProxy, insert path sections corresponding to # terms corresponding to each entry in its proxy_pointer rather than just the final term that it points to. if term.kind_of? OM::XML::NamedTermProxy current_location = term.parent.nil? ? term.terminology : term.parent relative_path = "" term.proxy_pointer.each_with_index do |proxy_pointer, proxy_pointer_index| proxy_term = current_location.retrieve_term(proxy_pointer) proxy_relative_path = proxy_term.xpath_relative if proxy_pointer_index > 0 proxy_relative_path = "/"+proxy_relative_path end relative_path << proxy_relative_path current_location = proxy_term end else relative_path = term.xpath_relative unless index.nil? relative_path = add_node_index_predicate(relative_path, index) end end if pointer_index > 0 relative_path = "/"+relative_path end xpath << relative_path end final_term = terminology.retrieve_term(*keys) if query_constraints.kind_of?(Hash) contains_functions = [] query_constraints.each_pair do |k,v| if k.instance_of?(Symbol) constraint_path = final_term.children[k].xpath_relative else constraint_path = k end contains_functions << "contains(#{constraint_path}, \"#{v}\")" end xpath = add_predicate(xpath, delimited_list(contains_functions, " and ") ) end return xpath end |