XmlQuery (XmlQuery v0.1.0)

Some simple XML query functions.

Summary

Functions

Finds all elements in an XML document that match xpath, returning a list of records. Depending on the given xpath, the type of the record may be different.

Returns the value of attr from the outermost element of xml.

Finds the first element, attribute, or element text in xml that matches xpath.

Like find/2 but raises unless exactly one node is found.

Parses an XML document using :xmerl_scan.string/2, returning an XmlQuery.Element struct.

Returns the text value of xml.

Types

@type xml() :: xml_binary() | xml_document() | xml_element() | XmlQuery.Element.t()
Link to this type

xml_attribute()

@type xml_attribute() :: XmlQuery.Xmerl.xml_attribute()
Link to this type

xml_binary()

@type xml_binary() :: binary()
Link to this type

xml_document()

@type xml_document() :: XmlQuery.Xmerl.xml_document()
Link to this type

xml_element()

@type xml_element() :: XmlQuery.Xmerl.xml_element()
@type xml_text() :: XmlQuery.Xmerl.xml_text()
@type xpath() :: binary() | charlist()

Functions

Link to this function

all(xml, xpath)

@spec all(xml(), xpath()) :: [XmlQuery.Element.t()]

Finds all elements in an XML document that match xpath, returning a list of records. Depending on the given xpath, the type of the record may be different.

Link to this function

attr(xml, attr)

@spec attr(xml(), String.t()) :: XmlQuery.Attribute.t() | nil

Returns the value of attr from the outermost element of xml.

Link to this function

find(xml, xpath)

@spec find(xml(), xpath()) ::
  XmlQuery.Element.t() | XmlQuery.Attribute.t() | XmlQuery.Text.t() | nil

Finds the first element, attribute, or element text in xml that matches xpath.

iex> alias XmlQuery, as: Xq
iex> xml = """
...> <?xml version="1.0"?>
...> <root><child property="oldest" /><child property="youngest" /></root>
...> """
iex> %Xq.Element{name: :child, attributes: [%Xq.Attribute{value: ~c"oldest"}]} = Xq.find(xml, "//child")
Link to this function

find!(xml, xpath)

Like find/2 but raises unless exactly one node is found.

Link to this macro

is_xml_struct(struct)

(macro)

Parses an XML document using :xmerl_scan.string/2, returning an XmlQuery.Element struct.

Given an xml tuple that has already been created by :xmerl, wraps the tuple in an XmlQuery-specific struct.

iex> xml = """
...> <?xml version="1.0"?>
...> <root />
...> """
iex> %Xq.Element{name: :root} = XmlQuery.parse(xml)

iex> xml = """
...> <?xml version="1.0"?>
...> <root property="root-value" />
...> """
iex> %Xq.Attribute{name: :property, value: ~c"root-value"} = XmlQuery.find(xml, "//root/@property") |> XmlQuery.parse()
@spec text(xml()) :: binary()

Returns the text value of xml.