Class: REXML::Parsers::XPathParser (Ruby 2.4.2)
Description: Class : REXML::Parsers::XPathParser - Ruby 2.4.2

Support for the Ruby 2.4 series has ended. See here for reference.

### Home Classes Methods

### In Files

- rexml/parsers/xpathparser.rb

### Parent

Object

### Methods

- #abbreviate
- #expand
- #namespaces=
- #parse
- #predicate
- #predicate_to_string

### Included Modules

- REXML::XMLTokens

### Class/Module Index

Quicksearch

- Array
- Float
- Integer
- Object
- REXML
- REXML::AttlistDecl
- REXML::Attribute
- REXML::Attributes
- REXML::CData
- REXML::Child
- REXML::Comment
- REXML::DTD
- REXML::DTD::AttlistDecl
- REXML::DTD::ElementDecl
- REXML::DTD::EntityDecl
- REXML::DTD::NotationDecl
- REXML::DTD::Parser
- REXML::Declaration
- REXML::DocType
- REXML::Document
- REXML::Element
- REXML::ElementDecl
- REXML::Elements
- REXML::Encoding
- REXML::Entity
- REXML::EntityConst
- REXML::ExternalEntity
- REXML::Formatters
- REXML::Formatters::Default
- REXML::Formatters::Pretty
- REXML::Formatters::Transitive
- REXML::Functions
- REXML::IOSource
- REXML::Instruction
- REXML::Light
- REXML::Light::Node
- REXML::Namespace
- REXML::Node
- REXML::NotationDecl
- REXML::Output
- REXML::Parent
- REXML::ParseException
- REXML::Parsers
- REXML::Parsers::BaseParser
- REXML::Parsers::LightParser
- REXML::Parsers::PullEvent
- REXML::Parsers::PullParser
- REXML::Parsers::SAX2Parser
- REXML::Parsers::StreamParser
- REXML::Parsers::TreeParser
- REXML::Parsers::UltraLightParser
- REXML::Parsers::XPathParser
- REXML::QuickPath
- REXML::SAX2Listener
- REXML::Security
- REXML::Source
- REXML::SourceFactory
- REXML::StreamListener
- REXML::SyncEnumerator
- REXML::Text
- REXML::UndefinedNamespaceException
- REXML::Validation
- REXML::Validation::Choice
- REXML::Validation::Event
- REXML::Validation::Interleave
- REXML::Validation::OneOrMore
- REXML::Validation::Optional
- REXML::Validation::Ref
- REXML::Validation::RelaxNG
- REXML::Validation::Sequence
- REXML::Validation::State
- REXML::Validation::ValidationException
- REXML::Validation::Validator
- REXML::Validation::ZeroOrMore
- REXML::XMLDecl
- REXML::XMLTokens
- REXML::XPath
- REXML::XPathParser
- Symbol

No matching classes.

# REXML::Parsers::XPathParser

You don't want to use this class. Really. Use `XPath`, which is a wrapper for this class. Believe me. You don't want to poke around in here. There is strange, dark magic at work in this code. Beware. Go back! Go back while you still can!

### Constants

AXIS

  RelativeLocationPath

      |                                                    Step
        | (AXIS_NAME '::' | '@' | '')                     AxisSpecifier
          NodeTest
            Predicate
        | '.' | '..'                                      AbbreviatedStep
      |  RelativeLocationPath '/' Step
      | RelativeLocationPath '//' Step

LITERAL
NCNAMETEST

      Returns a 1-1 map of the nodeset
      The contents of the resulting array are either:
        true/false, if a positive match
        String, if a name match

  NodeTest

      | ('*' | NCNAME ':' '*' | QNAME)                NameTest
      | NODE_TYPE '(' ')'                              NodeType
      | PI '(' LITERAL ')'                            PI
        | '[' expr ']'                                Predicate

NODE_TYPE
NT
NUMBER
PI
QNAME
VARIABLE_REFERENCE

  | `VARIABLE_REFERENCE` | '(' expr ')' | `LITERAL` | `NUMBER` | FunctionCall

### Public Instance Methods

abbreviate( path ) click to toggle source

                   # File rexml/parsers/xpathparser.rb, line 35
    def abbreviate( path )
      path = path.kind_of?(String) ? parse( path ) : path
      string = ""
      document = false
      while path.size > 0
        op = path.shift
        case op
        when :node
        when :attribute
          string << "/" if string.size > 0
          string << "@"
        when :child
          string << "/" if string.size > 0
        when :descendant_or_self
          string << "/"
        when :self
          string << "."
        when :parent
          string << ".."
        when :any
          string << "*"
        when :text
          string << "text()"
        when :following, :following_sibling,
              :ancestor, :ancestor_or_self, :descendant,
              :namespace, :preceding, :preceding_sibling
          string << "/" unless string.size == 0
          string << op.to_s.tr("_", "-")
          string << "::"
        when :qname
          prefix = path.shift
          name = path.shift
          string << prefix+":" if prefix.size > 0
          string << name
        when :predicate
          string << '['
          string << predicate_to_string( path.shift ) {|x| abbreviate( x ) }
          string << ']'
        when :document
          document = true
        when :function
          string << path.shift
          string << "( "
          string << predicate_to_string( path.shift[0] ) {|x| abbreviate( x )}
          string << " )"
        when :literal
          string << %Q{ "#{path.shift}" }
        else
          string << "/" unless string.size == 0
          string << "UNKNOWN("
          string << op.inspect
          string << ")"
        end
      end
      string = "/"+string if document
      return string
    end

expand( path ) click to toggle source

                   # File rexml/parsers/xpathparser.rb, line 93
    def expand( path )
      path = path.kind_of?(String) ? parse( path ) : path
      string = ""
      document = false
      while path.size > 0
        op = path.shift
        case op
        when :node
          string << "node()"
        when :attribute, :child, :following, :following_sibling,
              :ancestor, :ancestor_or_self, :descendant, :descendant_or_self,
              :namespace, :preceding, :preceding_sibling, :self, :parent
          string << "/" unless string.size == 0
          string << op.to_s.tr("_", "-")
          string << "::"
        when :any
          string << "*"
        when :qname
          prefix = path.shift
          name = path.shift
          string << prefix+":" if prefix.size > 0
          string << name
        when :predicate
          string << '['
          string << predicate_to_string( path.shift ) { |x| expand(x) }
          string << ']'
        when :document
          document = true
        else
          string << "/" unless string.size == 0
          string << "UNKNOWN("
          string << op.inspect
          string << ")"
        end
      end
      string = "/"+string if document
      return string
    end

namespaces=( namespaces ) click to toggle source

                   # File rexml/parsers/xpathparser.rb, line 15
    def namespaces=( namespaces )
      Functions::namespace_context = namespaces
      @namespaces = namespaces
    end

parse(path) click to toggle source

                   # File rexml/parsers/xpathparser.rb, line 20
    def parse path
      path = path.dup
      path.gsub!(/([\(\[])\s+/, '\1') # Strip ignorable spaces
      path.gsub!( /\s+([\]\)])/, '\1')
      parsed = []
      OrExpr(path, parsed)
      parsed
    end

predicate(path) click to toggle source

                   # File rexml/parsers/xpathparser.rb, line 29
    def predicate path
      parsed = []
      Predicate( "[#{path}]", parsed )
      parsed
    end

predicate_to_string( path, &block ) click to toggle source

                   # File rexml/parsers/xpathparser.rb, line 132
    def predicate_to_string( path, &block )
      string = ""
      case path[0]
      when :and, :or, :mult, :plus, :minus, :neq, :eq, :lt, :gt, :lteq, :gteq, :div, :mod, :union
        op = path.shift
        case op
        when :eq
          op = "="
        when :lt
          op = "<"
        when :gt
          op = ">"
        when :lteq
          op = "<="
        when :gteq
          op = ">="
        when :neq
          op = "!="
        when :union
          op = "|"
        end
        left = predicate_to_string( path.shift, &block )
        right = predicate_to_string( path.shift, &block )
        string << " "
        string << left
        string << " "
        string << op.to_s
        string << " "
        string << right
        string << " "
      when :function
        path.shift
        name = path.shift
        string << name
        string << "( "
        string << predicate_to_string( path.shift, &block )
        string << " )"
      when :literal
        path.shift
        string << " "
        string << path.shift.inspect
        string << " "
      else
        string << " "
        string << yield( path )
        string << " "
      end
      return string.squeeze(" ")
    end