Exquery.Query

Source

Summary

all(tree, spec)

Find the all elements in the tree that matche the spec

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
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