Class: Rexle::Element
Instance Attribute Summary collapse
-
#child_elements ⇒ Object
readonly
Returns the value of attribute child_elements.
-
#doc_id ⇒ Object
readonly
Returns the value of attribute doc_id.
-
#instructions ⇒ Object
readonly
Returns the value of attribute instructions.
-
#name ⇒ Object
Returns the value of attribute name.
-
#parent ⇒ Object
Returns the value of attribute parent.
-
#value ⇒ Object
Returns the value of attribute value.
Instance Method Summary collapse
- #add(item) ⇒ Object
-
#add_attribute(*x) ⇒ Object
alias add add_element.
- #add_element(item) ⇒ Object
- #add_text(s) ⇒ Object
- #at_css(selector) ⇒ Object
- #attribute(key) ⇒ Object
- #attributes ⇒ Object
- #cdata? ⇒ Boolean
- #cdatas ⇒ Object
- #children ⇒ Object
-
#children=(a) ⇒ Object
alias child_elements children.
- #clone ⇒ Object
- #contains(raw_args) ⇒ Object
- #content(options = {}) ⇒ Object
- #count(path) ⇒ Object
- #css(selector) ⇒ Object
- #deep_clone ⇒ Object
- #delete(obj = nil) ⇒ Object (also: #remove)
- #doc_root ⇒ Object
- #each(&blk) ⇒ Object
- #each_recursive(&blk) ⇒ Object (also: #traverse)
- #element(s) ⇒ Object
- #elements(s = nil) ⇒ Object
- #filter_xpath(path, rlist = [], &blk) ⇒ Object
- #has_elements? ⇒ Boolean
-
#initialize(name = nil, value: nil, attributes: {}, rexle: nil) ⇒ Element
constructor
A new instance of Element.
- #insert_after(node) ⇒ Object
- #insert_before(node) ⇒ Object
- #inspect ⇒ Object
- #map(&blk) ⇒ Object
- #max(path) ⇒ Object
- #next_element ⇒ Object (also: #next_sibling)
- #original_clone ⇒ Object
- #previous_element ⇒ Object (also: #previous_sibling)
- #query_xpath(raw_xpath_value, rlist = [], &blk) ⇒ Object
- #root ⇒ Object
- #text(s = '') ⇒ Object
- #texts ⇒ Object
- #to_a ⇒ Object
- #xml(options = {}) ⇒ Object (also: #to_s)
- #xpath(path, rlist = [], &blk) ⇒ Object
Methods included from XMLhelper
#doc_pretty_print, #doc_print, #pretty_print, #processing_instructions, #scan_print, #scan_to_a
Constructor Details
#initialize(name = nil, value: nil, attributes: {}, rexle: nil) ⇒ Element
Returns a new instance of Element.
325 326 327 328 329 330 331 332 333 334 335 336 |
# File 'lib/rexle.rb', line 325 def initialize(name=nil, value: nil, attributes: {}, rexle: nil) @rexle = rexle super() @name, @attributes = name.to_s, attributes raise "Element name must not be blank" unless name @child_elements = [] self.add_text value if value end |
Instance Attribute Details
#child_elements ⇒ Object (readonly)
Returns the value of attribute child_elements.
321 322 323 |
# File 'lib/rexle.rb', line 321 def child_elements @child_elements end |
#doc_id ⇒ Object (readonly)
Returns the value of attribute doc_id.
321 322 323 |
# File 'lib/rexle.rb', line 321 def doc_id @doc_id end |
#instructions ⇒ Object (readonly)
Returns the value of attribute instructions.
321 322 323 |
# File 'lib/rexle.rb', line 321 def instructions @instructions end |
#name ⇒ Object
Returns the value of attribute name.
320 321 322 |
# File 'lib/rexle.rb', line 320 def name @name end |
#parent ⇒ Object
Returns the value of attribute parent.
320 321 322 |
# File 'lib/rexle.rb', line 320 def parent @parent end |
#value ⇒ Object
Returns the value of attribute value.
320 321 322 |
# File 'lib/rexle.rb', line 320 def value @value end |
Instance Method Details
#add(item) ⇒ Object
682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 |
# File 'lib/rexle.rb', line 682 def add(item) if item.is_a? Rexle::Element then if self.doc_id == item.doc_id then new_item = item.deep_clone add_element new_item item.delete item = new_item new_item else add_element item end else add_element item end end |
#add_attribute(*x) ⇒ Object
alias add add_element
712 713 714 715 716 717 718 719 720 721 722 723 |
# File 'lib/rexle.rb', line 712 def add_attribute(*x) procs = { Hash: lambda {|x| x[0] || {}}, String: lambda {|x| Hash[*x]}, Symbol: lambda {|x| Hash[*x]} } h = procs[x[0].class.to_s.to_sym].call(x) @attributes.merge! h end |
#add_element(item) ⇒ Object
662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 |
# File 'lib/rexle.rb', line 662 def add_element(item) if item.is_a? String then @child_elements << item elsif item.is_a? Rexle::CData then @child_elements << item elsif item.is_a? Rexle::Element then @child_elements << item # add a reference from this element (the parent) to the child item.parent = self item elsif item.is_a? Rexle then self.add_element(item.root) end end |
#add_text(s) ⇒ Object
725 726 727 728 |
# File 'lib/rexle.rb', line 725 def add_text(s) self.value = (self.value || '') + s.to_s self end |
#at_css(selector) ⇒ Object
358 359 360 |
# File 'lib/rexle.rb', line 358 def at_css(selector) self.root.element RexleCSS.new(selector).to_xpath end |
#attribute(key) ⇒ Object
730 731 732 733 734 735 736 737 738 739 |
# File 'lib/rexle.rb', line 730 def attribute(key) key = key.to_sym if key.is_a? String if @attributes[key].is_a? String then @attributes[key].gsub('<','<').gsub('>','>') else @attributes[key] end end |
#attributes ⇒ Object
741 |
# File 'lib/rexle.rb', line 741 def attributes() @attributes end |
#cdatas ⇒ Object
743 744 745 |
# File 'lib/rexle.rb', line 743 def cdatas() self.children.inject([]){|r,x| x.is_a?(Rexle::CData) ? r << x.to_s : r } end |
#children ⇒ Object
747 748 749 750 751 752 753 754 755 756 757 |
# File 'lib/rexle.rb', line 747 def children() #return unless @value #r = (@value.to_s.empty? ? [] : [@value]) + @child_elements r = @child_elements def r.is_an_empty_string?() self.length == 1 and self.first == '' end return r end |
#children=(a) ⇒ Object
alias child_elements children
761 |
# File 'lib/rexle.rb', line 761 def children=(a) @child_elements = a if a.is_a? Array end |
#clone ⇒ Object
765 766 767 |
# File 'lib/rexle.rb', line 765 def clone() Element.new(@name, attributes: @attributes) end |
#contains(raw_args) ⇒ Object
342 343 344 345 346 347 348 349 350 351 |
# File 'lib/rexle.rb', line 342 def contains(raw_args) path, raw_val = raw_args.split(',',2) val = raw_val.strip[/^["']?.*["']?$/] anode = query_xpath(path) return unless anode a = scan_contents(anode.first) [a.grep(/#{val}/).length > 0] end |
#content(options = {}) ⇒ Object
893 894 895 |
# File 'lib/rexle.rb', line 893 def content(={}) CGI.unescapeHTML(xml()) end |
#count(path) ⇒ Object
353 354 355 356 |
# File 'lib/rexle.rb', line 353 def count(path) length = query_xpath(path).flatten.compact.length length end |
#css(selector) ⇒ Object
362 363 364 365 366 |
# File 'lib/rexle.rb', line 362 def css(selector) selector.split(',')\ .flat_map{|x| self.root.xpath RexleCSS.new(x).to_xpath} end |
#deep_clone ⇒ Object
763 |
# File 'lib/rexle.rb', line 763 def deep_clone() Rexle.new(self.xml).root end |
#delete(obj = nil) ⇒ Object Also known as: remove
769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 |
# File 'lib/rexle.rb', line 769 def delete(obj=nil) if obj then if obj.is_a? String then e = self.element obj e.delete if e else #jr010215 [@child_elements, @child_lookup].each{|x| x.delete_at i} if i i = @child_elements.index(obj) [@child_elements].each{|x| x.delete_at i} if i end else self.parent.delete(self) if self.parent end end |
#doc_root ⇒ Object
804 |
# File 'lib/rexle.rb', line 804 def doc_root() @rexle.root end |
#each(&blk) ⇒ Object
805 |
# File 'lib/rexle.rb', line 805 def each(&blk) self.children.each(&blk) end |
#each_recursive(&blk) ⇒ Object Also known as: traverse
806 |
# File 'lib/rexle.rb', line 806 def each_recursive(&blk) recursive_scan(self.children,&blk) end |
#element(s) ⇒ Object
790 791 792 793 |
# File 'lib/rexle.rb', line 790 def element(s) r = self.xpath(s) r.is_a?(Array) ? r.first : r end |
#elements(s = nil) ⇒ Object
795 796 797 798 799 800 801 802 |
# File 'lib/rexle.rb', line 795 def elements(s=nil) procs = { NilClass: proc {Elements.new(@child_elements.select{|x| x.kind_of? Rexle::Element })}, String: proc {|x| @child_elements[x]} } procs[s.class.to_s.to_sym].call(s) end |
#filter_xpath(path, rlist = [], &blk) ⇒ Object
413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 |
# File 'lib/rexle.rb', line 413 def filter_xpath(path, rlist=[], &blk) # is it a function fn_match = path.match(/^(\w+)\(["']?([^\)]*)["']?\)(?:\[(.*)\])?$/) # Array: proc {|x| x.flatten.compact}, if (fn_match and fn_match.captures.first[/^(attribute|@)/]) or fn_match.nil? then procs = { #jr061012 Array: proc {|x| block_given? ? x : x.flatten.uniq }, Array: proc { |x| if block_given? then x.flatten(1) else rs = x.flatten rs.any?{|x| x == true or x == false} ? rs : rs.uniq(&:object_id) end }, String: proc {|x| x}, Hash: proc {|x| x}, TrueClass: proc{|x| x}, FalseClass: proc{|x| x}, :"Rexle::Element" => proc {|x| [x]} } bucket = [] raw_results = path.split('|').map do |xp| query_xpath(xp.strip, bucket, &blk) end results = raw_results procs[results.class.to_s.to_sym].call(results) if results else m, xpath_value, index = fn_match.captures if m == 'text' then a = texts() return index ? a[index.to_i - 1].to_s : a end xpath_value.empty? ? method(m.to_sym).call : method(m.to_sym).call(xpath_value) end end |
#has_elements? ⇒ Boolean
808 |
# File 'lib/rexle.rb', line 808 def has_elements?() !self.elements.empty? end |
#insert_after(node) ⇒ Object
809 |
# File 'lib/rexle.rb', line 809 def insert_after(node) insert(node, 1) end |
#insert_before(node) ⇒ Object
810 |
# File 'lib/rexle.rb', line 810 def insert_before(node) insert(node) end |
#inspect ⇒ Object
702 703 704 705 706 707 708 |
# File 'lib/rexle.rb', line 702 def inspect() if self.xml.length > 30 then "%s ... </>" % self.xml[/<[^>]+>/] else self.xml end end |
#map(&blk) ⇒ Object
811 |
# File 'lib/rexle.rb', line 811 def map(&blk) self.children.map(&blk) end |
#max(path) ⇒ Object
368 369 370 371 |
# File 'lib/rexle.rb', line 368 def max(path) a = query_xpath(path).flatten.select{|x| x.is_a? String}.map(&:to_i) a.max end |
#next_element ⇒ Object Also known as: next_sibling
382 383 384 385 386 387 388 389 390 |
# File 'lib/rexle.rb', line 382 def next_element() id = self.object_id a = self.parent.child_elements i = a.index {|x| x.object_id == id} + 1 a[i] if i < a.length end |
#original_clone ⇒ Object
323 |
# File 'lib/rexle.rb', line 323 alias original_clone clone |
#previous_element ⇒ Object Also known as: previous_sibling
394 395 396 397 398 399 400 401 402 |
# File 'lib/rexle.rb', line 394 def previous_element() id = self.object_id a = self.parent.child_elements i = a.index {|x| x.object_id == id} - 1 a[i] if i >= 0 end |
#query_xpath(raw_xpath_value, rlist = [], &blk) ⇒ Object
461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 |
# File 'lib/rexle.rb', line 461 def query_xpath(raw_xpath_value, rlist=[], &blk) #remove any pre'fixes #@rexle.prefixes.each {|x| xpath_value.sub!(x + ':','') } flag_func = false xpath_value = raw_xpath_value.sub('child::','./') #xpath_value.sub!(/\.\/(?=[\/])/,'') if xpath_value[/^[\w\/]+\s*=.*/] then flag_func = true xpath_value.sub!(/^\w+\s*=.*/,'.[\0]') xpath_value.sub!(/\/([\w]+\s*=.*)/,'[\1]') #result = self.element xpath_value #return [(result.is_a?(Rexle::Element) ? true : false)] end #xpath_value.sub!(/^attribute::/,'*/attribute::') raw_path, raw_condition = xpath_value.sub(/^\.?\/(?!\/)/,'')\ .match(/([^\[]+)(\[[^\]]+\])?/).captures remaining_path = ($').to_s r = raw_path[/^([^\/]+)(?=\/\/)/,1] if r then a_path = raw_path.split(/(?=\/\/)/,2) else a_path = raw_path.split('/',2) end condition = raw_condition if a_path.length <= 1 if raw_path[0,2] == '//' then s = '' elsif raw_path == 'text()' a_path.shift #return @value return self.texts else attribute = xpath_value[/^(attribute::|@)(.*)/,2] return @attributes if attribute == '*' return [@attributes[attribute.to_sym]] if attribute and @attributes and @attributes.has_key?(attribute.to_sym) s = a_path.shift end # isolate the xpath to return just the path to the current element elmnt_path = s[/^([\w:\-\*]+\[[^\]]+\])|[\/]+{,2}[^\/]+/] element_part = elmnt_path[/(^@?[^\[]+)?/,1] if elmnt_path if element_part then unless element_part[/^(@|[@\.\w]+[\s=])/] then element_name = element_part[/^[\w:\-\*\.]+/] else if xpath_value[/^\[/] then condition = xpath_value element_name = nil else condition = element_part attr_search = format_condition('[' + condition + ']') return [attribute_search(attr_search, self, self.attributes) != nil] end end end #element_name ||= '*' raw_condition = '' if condition attr_search = format_condition(condition) if condition and condition.length > 0 attr_search2 = xpath_value[/^\[(.*)\]$/,1] if attr_search2 then r4 = attribute_search(attr_search, self, self.attributes) return r4 end return_elements = [] if raw_path[0,2] == '//' then regex = /\[(\d+)\]$/ n = xpath_value[regex,1] xpath_value.slice!(regex) rs = scan_match(self, xpath_value).flatten.compact return n ? rs[n.to_i-1] : rs else if element_name.is_a? String then ename, raw_selector = (element_name.split('::',2)).reverse selector = case raw_selector when 'following-sibling' then 1 when 'preceding-sibling' then -1 end else ename = element_name end if ename != '..' then return_elements = @child_elements.map.with_index.select do |x, i| next unless x.is_a? Rexle::Element (x.name == ename || ename == '.') or (ename == '*') end if selector then ne = return_elements.inject([]) do |r,x| i = x.last + selector if i >= 0 then r << i else r end end return_elements = ne.map {|x| [@child_elements[x], x] if x} end else remaining_xpath = raw_path[/\.\.\/(.*)/,1] # select the parent element r2 = self.parent.xpath(remaining_xpath) return r2 end end if return_elements.length > 0 then if (a_path + [remaining_path]).join.empty? then rlist = return_elements.map.with_index {|x,i| filter(x, i+1, attr_search, &blk)}.compact rlist = rlist[0] if rlist.length == 1 else rlist << return_elements.map.with_index do |x,i| rtn_element = filter(x, i+1, attr_search) do |e| r = e.xpath(a_path.join('/') + raw_condition.to_s \ + remaining_path, &blk) r #(r || e) end next if rtn_element.nil? or (rtn_element.is_a? Array and rtn_element.empty?) if rtn_element.is_a? Hash then rtn_element elsif rtn_element.is_a? Array then rtn_element elsif (rtn_element.is_a? String) || (rtn_element.is_a?(Array) and not(rtn_element[0].is_a? String)) rtn_element elsif rtn_element.is_a? Rexle::Element rtn_element end end # rlist = rlist.flatten(1) unless rlist.length > 1 and rlist[0].is_a? Array end rlist.compact! if rlist.is_a? Array else # strip off the 1st element from the XPath new_xpath = xpath_value[/^\/\/[\w:\-]+\/(.*)/,1] if new_xpath then self.xpath(new_xpath + raw_condition.to_s + remaining_path, rlist,&blk) end end rlist = rlist.flatten(1) unless not(rlist.is_a? Array) or (rlist.length > 1 and rlist[0].is_a? Array) rlist = [rlist] if rlist.is_a? Rexle::Element rlist = (rlist.length > 0 ? true : false) if flag_func == true rlist end |
#root ⇒ Object
812 |
# File 'lib/rexle.rb', line 812 def root() self end |
#text(s = '') ⇒ Object
814 815 816 817 818 819 |
# File 'lib/rexle.rb', line 814 def text(s='') return @child_elements.first if s.empty? e = self.element(s) e.text if e end |
#texts ⇒ Object
821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 |
# File 'lib/rexle.rb', line 821 def texts() r = @child_elements.select do |x| x.respond_to? :to_s end r.map do |x| def x.unescape() s = self.to_s.clone %w(< < > > & & &pos; ').each_slice(2){|x| s.gsub!(*x)} s end end return r end |
#to_a ⇒ Object
874 875 876 |
# File 'lib/rexle.rb', line 874 def to_a() [self.name, self.attributes, self.value, *scan_to_a(self.children)] end |
#xml(options = {}) ⇒ Object Also known as: to_s
878 879 880 881 882 883 884 885 886 887 888 889 890 891 |
# File 'lib/rexle.rb', line 878 def xml(={}) h = { Hash: lambda {|x| o = {pretty: false}.merge(x) msg = o[:pretty] == false ? :doc_print : :doc_pretty_print method(msg).call(self.children) }, String: lambda {|x| r = self.element(x) r ? r.xml : '' } } h[.class.to_s.to_sym].call end |
#xpath(path, rlist = [], &blk) ⇒ Object
406 407 408 409 410 411 |
# File 'lib/rexle.rb', line 406 def xpath(path, rlist=[], &blk) #return if path[/^(?:preceding|following)-sibling/] r = filter_xpath(path, rlist=[], &blk) r.is_a?(Array) ? r.compact : r end |