[erlang-questions] xmerl and selecting attributes

46 views
Skip to first unread message

Ralf Doering

unread,
Sep 8, 2009, 6:21:31 AM9/8/09
to erlang-q...@erlang.org

Hello,

seems I've got a problem selecting the value of an attribute from an XML tag.

Consider the following short example:

--8<---------------cut here---------------start------------->8---
-module(xmerl_test).

-include_lib("xmerl/include/xmerl.hrl").

-import(xmerl_xs,
[ xslapply/2, value_of/1, select/2, built_in_rules/2 ]).


-export([start/0]).

start() ->
{XmlElement, _Rest} = xmerl_scan:string("<test myattr=\"123\" />"),
template(XmlElement).


template( E = #xmlElement{name='test'}) ->
Attr = value_of(select("@myattr", E)),
io:format("MyAttribute: ~w~n", [Attr]),
xslapply(fun template/1,E),
E;

template(E) ->
built_in_rules( fun template/1, E).

--8<---------------cut here---------------end--------------->8---


When running this, I get:

(emacs@skye)13> xmerl_test:start().
MyAttribute: []
[]

I was expecting that value_of(select("@myattr"),E) would extract "123" as
attribute value. Am I wrong here, do I use the wrong XPath for this?

TIA,
Ralf
--
Ralf Döring | NetSys.IT GbR
Networking & Security Solutions | www.netsys-it.de
rdoe...@netsys-it.de | Tel.: +49 (3677) 20 35 15

________________________________________________________________
erlang-questions mailing list. See http://www.erlang.org/faq.html
erlang-questions (at) erlang.org

Robert Raschke

unread,
Sep 8, 2009, 6:43:15 AM9/8/09
to Ralf Doering, erlang-q...@erlang.org

I think the xmerl_xs:value_of/1 function will only ever return the text
nodes of the element list you supply.

The xmerl_xs:select/2 invocation you have is fine:

Erlang (BEAM) emulator version 5.6.5 [smp:2] [async-threads:0]

Eshell V5.6.5 (abort with ^G)
1> rr(xmerl).
[xmerl_event,xmerl_fun_states,xmerl_scanner,xmlAttribute,
xmlComment,xmlContext,xmlDecl,xmlDocument,xmlElement,
xmlNamespace,xmlNode,xmlNsNode,xmlObj,xmlPI,xmlText]
2> {E,_} = xmerl_scan:string("<test myattr=\"123\" />").
{#xmlElement{
name = test,expanded_name = test,nsinfo = [],
namespace = #xmlNamespace{default = [],nodes = []},
parents = [],pos = 1,
attributes =
[#xmlAttribute{
name = myattr,expanded_name = [],nsinfo = [],namespace = [],
parents = [],pos = 1,language = [],value = "123",
normalized = false}],
content = [],language = [],xmlbase = "C:/Robby",
elementdef = undeclared},
[]}
3> xmerl_xs:select("@myattr", E).
[#xmlAttribute{name = myattr,expanded_name = [],nsinfo = [],
namespace = [],parents = [],pos = 1,language = [],
value = "123",normalized = false}]


So, I guess you have to get the value through the record itself.

Robby

Ralf Doering

unread,
Sep 8, 2009, 8:37:39 AM9/8/09
to erlang-q...@erlang.org

Hello Robert,

Robert Raschke <rtrl...@googlemail.com> writes:
> On Tue, Sep 8, 2009 at 11:21 AM, Ralf Doering <rdoe...@netsys-it.de> wrote:

>> I was expecting that value_of(select("@myattr"),E) would extract "123" as
>> attribute value. Am I wrong here, do I use the wrong XPath for this?
>>
>>
> I think the xmerl_xs:value_of/1 function will only ever return the text
> nodes of the element list you supply.

[...]

> So, I guess you have to get the value through the record itself.

Thank you for your reply. I already guessed that, and with your hints it
works perfectly now.

Thx,
Ralf

--
Ralf Döring
NetSys.IT GbR

www.netsys-it.de

Reply all
Reply to author
Forward
0 new messages