Exquery.Query

Source

Summary

all(tree, spec)

Find the all elements in the tree that matche the spec

before(tree, spec, offset)

Find the element before the specified element

next(tree, spec, offset)

Find the element after the specified element

one(tree, spec)

Find the first element in the tree that matches the spec

Functions

all(tree, spec)

Find the all elements in the tree that matche the spec.

A tree is an HTML tree given from Exquery.tree/1 A spec is an HTML elemement, which a three element tuple of the element type, contents, and attributes. <div id="foo"></div> would look like {:tag, "div", [{"id", "foo"}]}

You may pass :any in for the element type and element contents to select any element.

Examples:

iex> "<div id=\"foo\"><div id=\"bar\">hi</div></div>" |> Exquery.tree |> Exquery.Query.all({:tag, "div", []})
[
  {{:tag, "div", [{"id", "foo"}]}, [
    {{:tag, "div", [{"id", "bar"}]}, [
      {:text, "hi", []}
    ]}
  ]},
  {{:tag, "div", [{"id", "bar"}]}, [
    {:text, "hi", []}
  ]}
]

iex> "<div id=\"foo\"><div id=\"bar\">hi</div></div>" |> Exquery.tree |> Exquery.Query.all({:tag, "div", [{"id", "bar"}]})
[{{:tag, "div", [{"id", "bar"}]}, [{:text, "hi", []}]}]

iex> "<div id=\"foo\"><div id=\"bar\">hi</div></div>" |> Exquery.tree |> Exquery.Query.all({:tag, "div", [{"id", "nope"}]})
[]
Source
before(tree, spec, offset)

Find the element before the specified element

A tree is an HTML tree given from Exquery.tree/1 A spec is an HTML elemement, which a three element tuple of the element type, contents, and attributes. <div id="foo"></div> would look like {:tag, "div", [{"id", "foo"}]}

You may pass :any in for the element type and element contents to select any element.

An optional 3rd argument specifies the offset of the selected element. By default it is 1, but you may select an element n elements before to the specified element by passing an offset of n.

Examples:

iex> "<div id=\"foo\"></div><div id=\"bar\"></div>" |> Exquery.tree |> Exquery.Query.before({:tag, "div", [{"id", "foo"}]})
nil
iex> "<div id=\"foo\"></div><div id=\"bar\"></div>" |> Exquery.tree |> Exquery.Query.before({:tag, "div", [{"id", "bar"}]})
{{:tag, "div", [{"id", "foo"}]}, []}
iex> "<div id=\"foo\"></div><div id=\"bar\"></div><div id=\"baz\"></div>" |> Exquery.tree |> Exquery.Query.before({:tag, "div", [{"id", "baz"}]}, 2)
{{:tag, "div", [{"id", "foo"}]}, []}
Source
next(tree, spec, offset)

Find the element after the specified element

A tree is an HTML tree given from Exquery.tree/1 A spec is an HTML elemement, which a three element tuple of the element type, contents, and attributes. <div id="foo"></div> would look like {:tag, "div", [{"id", "foo"}]}

You may pass :any in for the element type and element contents to select any element.

An optional 3rd argument specifies the offset of the selected element. By default it is 1, but you may select an element n elements next to the specified element by passing an offset of n.

Examples:

iex> "<div id=\"foo\"></div><div id=\"bar\"></div>" |> Exquery.tree |> Exquery.Query.next({:tag, "div", [{"id", "foo"}]})
{{:tag, "div", [{"id", "bar"}]}, []}
iex> "<div id=\"foo\"></div><div id=\"bar\"></div>" |> Exquery.tree |> Exquery.Query.next({:tag, "div", [{"id", "bar"}]})
nil
iex> "<div id=\"foo\"></div><div id=\"bar\"></div><div id=\"baz\"></div>" |> Exquery.tree |> Exquery.Query.next({:tag, "div", [{"id", "foo"}]}, 2)
{{:tag, "div", [{"id", "baz"}]}, []}
Source
one(tree, spec)

Find the first element in the tree that matches the spec.

A tree is an HTML tree given from Exquery.tree/1 A spec is an HTML elemement, which a three element tuple of the element type, contents, and attributes. <div id="foo"></div> would look like {:tag, "div", [{"id", "foo"}]}

You may pass :any in for the element type and element contents to select any element.

Examples:

iex> "<div id=\"foo\"><a id=\"bar\">hi</a></div>" |> Exquery.tree |> Exquery.Query.one({:tag, "a", [{"id", "bar"}]})
{{:tag, "a", [{"id", "bar"}]}, [{:text, "hi", []}]}

iex> "<div id=\"foo\"><a id=\"bar\">hi</a></div>" |> Exquery.tree |> Exquery.Query.one({:tag, "a", []})
{{:tag, "a", [{"id", "bar"}]}, [{:text, "hi", []}]}

iex> "<div id=\"foo\"><a id=\"bar\">hi</a></div>" |> Exquery.tree |> Exquery.Query.one({:tag, :any, [{"id", "bar"}]})
{{:tag, "a", [{"id", "bar"}]}, [{:text, "hi", []}]}

iex> "<div id=\"foo\"><a id=\"bar\">hi</a></div>" |> Exquery.tree |> Exquery.Query.one({:any, :any, [{"id", "bar"}]})
{{:tag, "a", [{"id", "bar"}]}, [{:text, "hi", []}]}

iex> "<div id=\"foo\"><a id=\"bar\">hi</a></div>" |> Exquery.tree |> Exquery.Query.one({:any, :any, [{"id", "does-not-exist"}]})
nil
Source