View Source exml_query (exml v4.1.0)

Easy navigation in XML trees

Summary

Types

Path definition in an XML query, each step is defined by one of these types.

Functions

Like path/3 but with default undefined.

Gets the element/attr/cdata in the leftmost possible described path, or Default if there is no match.

Gets the elements/attrs/cdatas reachable by the described path

Types

path/0

-type path() ::
          [cdata |
           {attr, binary()} |
           {element, binary()} |
           {element_with_ns, binary()} |
           {element_with_ns, binary(), binary()} |
           {element_with_attr, binary(), binary()}].

Path definition in an XML query, each step is defined by one of these types.

  • cdata: selects cdata from the element
  • {attr, Name}: selects a subelement with the given attribute
  • {element, Name}: selects a subelement with the given name
  • {element_with_ns, NS}: selects a subelement with given namespace
  • {element_with_ns, Name, NS}: selects a subelement with given name and namespace
  • {element_with_attr, AttrName, AttrValue}: selects a subelement with the given attribute and value

Functions

attr(Element, Name)

-spec attr(exml:element(), binary()) -> binary() | undefined.

Equivalent to path(Element, [{attr, Name}]).

See also: attr/3.

attr(Xmlel, Name, Default)

-spec attr(exml:element(), binary(), Default) -> binary() | Default.

Equivalent to path(Element, [{attr, Name}], Default).

cdata(Xmlel)

-spec cdata(exml:element()) -> binary().

Equivalent to path(Element, [cdata]).

path(Element, Path)

-spec path(exml:element(), path()) -> exml:element() | binary() | undefined.

Like path/3 but with default undefined.

See also: path/3.

path(Xmlel, Rest, Default)

-spec path(exml:element() | undefined, path(), Default) -> exml:element() | binary() | Default.

Gets the element/attr/cdata in the leftmost possible described path, or Default if there is no match.

Find an element in the xml tree by a path that is pattern-matched against such xml tree structure.

For example, given an xml document like

  <message from='alice@localhost' to='alice@localhost/res1' id='id-1'>
    <result xmlns='urn:xmpp:mam:2' id='BGCH2R2950G1'>
      <forwarded xmlns='urn:xmpp:forward:0'>
        <delay xmlns='urn:xmpp:delay' stamp='2021-05-05T08:36:19Z' from='bob@localhost/res1'/>
        <message from='bob@localhost/res1' xmlns='jabber:client' xml:lang='en' to='alice@localhost/res1' type='chat'>
          <body>Message from bob to alice</body>
        </message>
      </forwarded>
    </result>
  </message>

The path

    [{element_with_ns, <<"result">>, <<"urn:xmpp:mam:2">>},
     {element_with_ns, <<"forwarded">>, <<"urn:xmpp:forward:0">>},
     {element_with_ns, <<"message">>, <<"jabber:client">>},
     {element, <<"body">>},
     cdata}],

will return <<"Message from bob to alice">>

paths(Xmlel, Rest)

-spec paths(exml:element(), path()) -> [exml:element() | binary()].

Gets the elements/attrs/cdatas reachable by the described path

See also: path/3.

subelement(Element, Name)

-spec subelement(exml:element(), binary()) -> exml:element() | undefined.

Equivalent to path(Element, [{element, Name}]).

subelement(Xmlel, Name, Default)

-spec subelement(exml:element(), binary(), Default) -> exml:element() | Default.

Equivalent to path(Element, [{element, Name}], Default).

subelement_with_attr(Element, AttrName, AttrValue)

-spec subelement_with_attr(exml:element(), AttrName :: binary(), AttrValue :: binary()) ->
                              exml:element() | undefined.

Equivalent to path(Element, [{element_with_attr, AttrName, AttrValue}]).

subelement_with_attr(Element, AttrName, AttrValue, Default)

-spec subelement_with_attr(Element, AttrName, AttrValue, Default) -> SubElement | Default
                              when
                                  Element :: exml:element(),
                                  AttrName :: binary(),
                                  AttrValue :: binary(),
                                  SubElement :: exml:element(),
                                  Default :: term().

Equivalent to path(Element, [{element_with_attr, AttrName, AttrValue}], Default).

subelement_with_name_and_ns(Element, Name, NS)

-spec subelement_with_name_and_ns(exml:element(), binary(), binary()) -> exml:element() | undefined.

Equivalent to path(Element, [{element_with_ns, Name, NS}]).

subelement_with_name_and_ns(Element, Name, NS, Default)

-spec subelement_with_name_and_ns(exml:element(), binary(), binary(), Default) ->
                                     exml:element() | Default.

Equivalent to path(Element, [{element_with_ns, Name, NS}], Default).

subelement_with_ns(Element, NS)

-spec subelement_with_ns(exml:element(), binary()) -> exml:element() | undefined.

Equivalent to path(Element, [{element_with_ns, NS}]).

subelement_with_ns(Xmlel, NS, Default)

-spec subelement_with_ns(exml:element(), binary(), Default) -> exml:element() | Default.

Equivalent to path(Element, [{element_with_ns, NS}], Default).

subelements(Xmlel, Name)

-spec subelements(exml:element(), binary()) -> [exml:element()].

Equivalent to paths(Element, [{element, Name}]).

subelements_with_attr(Xmlel, AttrName, Value)

-spec subelements_with_attr(exml:element(), binary(), binary()) -> [exml:element()].

Equivalent to paths(Element, [{element_with_attr, AttrName, AttrValue}]).

subelements_with_name_and_ns(Xmlel, Name, NS)

-spec subelements_with_name_and_ns(exml:element(), binary(), binary()) -> [exml:element()].

Equivalent to paths(Element, [{element_with_ns, Name, NS}]).

subelements_with_ns(Xmlel, NS)

-spec subelements_with_ns(exml:element(), binary()) -> [exml:element()].

Equivalent to paths(Element, [{element_with_ns, NS}]).