[erlang-questions] xmerl and selecting attributes
Robert Raschke
rtrlists@REDACTED
Tue Sep 8 12:43:15 CEST 2009
On Tue, Sep 8, 2009 at 11:21 AM, Ralf Doering <rdoering@REDACTED> wrote:
>
> 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@REDACTED)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?
>
>
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
More information about the erlang-questions
mailing list