0

I have a child element called Request inside my message element of xmpp packet. Therefore, my packet looks like the below:

<message to="b" from="a" type="chat"> 
    <request xmlns="urn:client:send-ack"></request> 
</message>

I want to match the value of xmlns attribute inside the request element. So I want to do something like

case xml:get_attr_s(<<"xmlns">>, xml_get_subtag(<<Request>>,Packet)) of
   "urn:client:send-receipts" ->
   %% Do something
   ok.

But, obviously this densest work. Whats the best way to do it ?

Kamrul Khan
  • 3,260
  • 4
  • 32
  • 59
  • If you really want to use the `xml` module, then `xml:get_tag_attr(<<"xmlns">>, xml:get_subtag(El, <<"request">>))` is the way to go, but (arguably) `exml_query:path/2`, suggested by Piotr, has a more convenient interface. – erszcz Apr 14 '15 at 12:05

2 Answers2

2

Easy and fast way to get this attribute is exml_query:path/2. With it, your case ... of would be following:

case exml_query:path(Stanza, [{element, <<"request">>}, {attr, <<"xmlns">>}]) of
    <<"urn:client:send-receipts">> -> something;
    _ -> something_else
end
Piotr Nosek
  • 106
  • 2
  • hey thanks it worked. But I have a small problem. I have two request tags in my packet. The one im looking for is the second one. But whenever I look for a match using exml_query:path it returns the first match. is there a way to loop through the packet or check the next match or something ? – Kamrul Khan Apr 14 '15 at 17:16
  • In such case use `exml_query:paths/2` with the same arguments and it will return a list of results, in this case the list of `xmlns` values for both elements. Then you can use `lists:member/2` to check if the desired XMLNS is there. – Piotr Nosek Apr 15 '15 at 21:23
1

With latest ejabberd development version, you can do what you want with the following:

xml:get_subtags_with_xmlns(Parsed_xml, <<"request">>, <<"urn:client:send-ack">>).

It will match any number of subtags and return a list.

You need to build ejabberd from source or use the upcoming ejabberd 15.04 version, released before end of month.

Mickaël Rémond
  • 9,035
  • 1
  • 24
  • 44