README

Installation and usage

See gopkg.in/xmlpath.v2 for documentation and usage details.

Documentation

Overview

    Package xmlpath implements a strict subset of the XPath specification for the Go language.

    The XPath specification is available at:

    http://www.w3.org/TR/xpath
    

    Path expressions supported by this package are in the following format, with all components being optional:

    /axis-name::node-test[predicate]/axis-name::node-test[predicate]
    

    At the moment, xmlpath is compatible with the XPath specification to the following extent:

    - All axes are supported ("child", "following-sibling", etc)
    - All abbreviated forms are supported (".", "//", etc)
    - All node types except for namespace are supported
    - Predicates may be [N], [path], [not(path)], [path=literal] or [contains(path, literal)]
    - Predicates may be joined with "or", "and", and parenthesis
    - Richer expressions and namespaces are not supported
    

    For example, assuming the following document:

    <library>
      <!-- Great book. -->
      <book id="b0836217462" available="true">
        <isbn>0836217462</isbn>
        <title lang="en">Being a Dog Is a Full-Time Job</title>
        <quote>I'd dog paddle the deepest ocean.</quote>
        <author id="CMS">
          <?echo "go rocks"?>
          <name>Charles M Schulz</name>
          <born>1922-11-26</born>
          <dead>2000-02-12</dead>
        </author>
        <character id="PP">
          <name>Peppermint Patty</name>
          <born>1966-08-22</born>
          <qualification>bold, brash and tomboyish</qualification>
        </character>
        <character id="Snoopy">
          <name>Snoopy</name>
          <born>1950-10-04</born>
          <qualification>extroverted beagle</qualification>
        </character>
      </book>
    </library>
    

    The following examples are valid path expressions, and the first match has the indicated value:

    /library/book/isbn                               =>  "0836217462"
    library/*/isbn                                   =>  "0836217462"
    /library/book/../book/./isbn                     =>  "0836217462"
    /library/book/character[2]/name                  =>  "Snoopy"
    /library/book/character[born='1950-10-04']/name  =>  "Snoopy"
    /library/book//node()[@id='PP']/name             =>  "Peppermint Patty"
    //book[author/@id='CMS']/title                   =>  "Being a Dog Is a Full-Time Job",
    /library/book/preceding::comment()               =>  " Great book. "
    //*[contains(born,'1922')]/name                  =>  "Charles M Schulz"
    //*[@id='PP' or @id='Snoopy']/born               =>  {"1966-08-22", "1950-10-04"}
    

    To run an expression, compile it, and then apply the compiled path to any number of context nodes, from one or more parsed xml documents:

    path := xmlpath.MustCompile("/library/book/isbn")
    root, err := xmlpath.Parse(file)
    if err != nil {
            log.Fatal(err)
    }
    if value, ok := path.String(root); ok {
            fmt.Println("Found:", value)
    }
    

    Index

    Constants

    This section is empty.

    Variables

    This section is empty.

    Functions

    This section is empty.

    Types

    type Iter

    type Iter struct {
    	// contains filtered or unexported fields
    }

      Iter iterates over node sets.

      func (*Iter) Next

      func (iter *Iter) Next() bool

        Next iterates to the next node in the set, if any, and returns whether there is a node available.

        func (*Iter) Node

        func (iter *Iter) Node() *Node

          Node returns the current node. Must only be called after Iter.Next returns true.

          type Node

          type Node struct {
          	// contains filtered or unexported fields
          }

            Node is an item in an xml tree that was compiled to be processed via xml paths. A node may represent:

            - An element in the xml document (<body>)
            - An attribute of an element in the xml document (href="...")
            - A comment in the xml document (<!--...-->)
            - A processing instruction in the xml document (<?...?>)
            - Some text within the xml document
            

            func Parse

            func Parse(r io.Reader) (*Node, error)

              Parse reads an xml document from r, parses it, and returns its root node.

              func ParseDecoder

              func ParseDecoder(d *xml.Decoder) (*Node, error)

                ParseDecoder parses the xml document being decoded by d and returns its root node.

                func ParseHTML

                func ParseHTML(r io.Reader) (*Node, error)

                  ParseHTML reads an HTML document from r, parses it using a proper HTML parser, and returns its root node.

                  The document will be processed as a properly structured HTML document, emulating the behavior of a browser when processing it. This includes putting the content inside proper <html> and <body> tags, if the provided text misses them.

                  func (*Node) Bytes

                  func (node *Node) Bytes() []byte

                    Bytes returns the string value of node as a byte slice. See Node.String for a description of what the string value of a node is.

                    func (*Node) String

                    func (node *Node) String() string

                      String returns the string value of node.

                      The string value of a node is:

                      - For element nodes, the concatenation of all text nodes within the element.
                      - For text nodes, the text itself.
                      - For attribute nodes, the attribute value.
                      - For comment nodes, the text within the comment delimiters.
                      - For processing instruction nodes, the content of the instruction.
                      

                      type Path

                      type Path struct {
                      	// contains filtered or unexported fields
                      }

                        Path is a compiled path that can be applied to a context node to obtain a matching node set. A single Path can be applied concurrently to any number of context nodes.

                        func Compile

                        func Compile(path string) (*Path, error)

                          Compile returns the compiled path.

                          func MustCompile

                          func MustCompile(path string) *Path

                            MustCompile returns the compiled path, and panics if there are any errors.

                            func (*Path) Bytes

                            func (p *Path) Bytes(node *Node) (b []byte, ok bool)

                              Bytes returns as a byte slice the string value of the first node matched by p on the given context.

                              See the documentation of Node.String.

                              func (*Path) Exists

                              func (p *Path) Exists(context *Node) bool

                                Exists returns whether any nodes match p on the given context.

                                func (*Path) Iter

                                func (p *Path) Iter(context *Node) *Iter

                                  Iter returns an iterator that goes over the list of nodes that p matches on the given context.

                                  func (*Path) String

                                  func (p *Path) String(context *Node) (s string, ok bool)

                                    String returns the string value of the first node matched by p on the given context.

                                    See the documentation of Node.String.

                                    Directories

                                    Path Synopsis
                                    cmd