[erlang-questions] xmerl_xpath difficulties

Caoyuan <>
Sun Aug 5 15:56:23 CEST 2007


It's a bit strange, it works in my computer. You may try:
1> {XmlFull, _} = xmerl_scan:string("<text>Hello</text>").
{{xmlElement,text,
             text,
             [],
             {xmlNamespace,[],[]},
             [],
             1,
             [],
             [{xmlText,[{text,1}],1,[],"Hello",text}],
             [],
             "/Users/dcaoyuan/polebeans/ewp",
             undeclared},
 []}
2> XmlTexts = xmerl_xpath:string("/text/text()", XmlFull).
[{xmlText,[{text,1}],1,[],"Hello",text}]

The xpath query should return a list of result, it maybe [], or more
than one element, so it's better to do:

3> lists:flatten([element(5,X) || X <- XmlTexts, element(1, X) == xmlText]).
"Hello"

If you are in writing a module, by including xmerl.hrl, you can:
lists:flatten([X#xmlText.value || X <- XmlTexts, is_record(X, xmlText)]).


On 8/5/07, David Mitchell <> wrote:
> Thanks Caoyuan, but it didn't work for me:
>
> :~$ erl
> Erlang (BEAM) emulator version 5.4.9 [64-bit] [source] [threads:0]
>
> Eshell V5.4.9  (abort with ^G)
> 1> {XmlFull,_} = xmerl_scan:string("<text>Hello</text>").
> {{xmlElement,text,
>             text,
>             [],
>             {xmlNamespace,[],[]},
>             [],
>             1,
>             [],
>             [{xmlText,[{text,1}],1,[],"Hello",text}],
>             [],
>             "/home/davidm",
>             undeclared},
>  []}
> 2> [XmlText] = xmerl_xpath:string("/text/text()", XmlFull).
>
> =ERROR REPORT==== 5-Aug-2007::22:48:45 ===
> Error in process <0.31.0> with exit value: {{badmatch,[]},[{erl_eval,expr,3}]}
>
> ** exited: {{badmatch,[]},[{erl_eval,expr,3}]} **
> 3>
>
> Any thoughts?
>
> Regards
>
> Dave M.
>
> On 05/08/07, Caoyuan <> wrote:
> > > [XmlText] = xmerl_xpath:string("/text/text()", XmlFull).
> > [{xmlText,[{text,1}],1,[],"Hello",text}]
> > > element(5, XmlText).
> > "Hello"
> >
> >
> > On 8/5/07, David Mitchell <> wrote:
> > > Hello group,
> > >
> > > I'm having trouble getting my head around xmerl_xpath - I've gone
> > > through a bunch of Google searches, but can't find the "Simple guide
> > > to xmerl_xpath" I'm looking for ;->
> > >
> > > I've reduced my problem to 3 lines of code:
> > >
> > > 15> {XmlFull, _} = xmerl_scan:string("<text>Hello</text>").
> > > {{xmlElement,text,
> > >              text,
> > >              [],
> > >              {xmlNamespace,[],[]},
> > >              [],
> > >              1,
> > >              [],
> > >              [{xmlText,[{text,1}],1,[],"Hello",text}],
> > >              [],
> > >              "/home/davidm",
> > >              undeclared},
> > >  []}
> > >
> > > 16> xmerl:export([XmlFull], xmerl_xml).
> > > ["<?xml version=\"1.0\"?>",[[["<","text",">"],["Hello"],["</","text",">"]]]]
> > >
> > > 17> xmerl_xpath:string("//text/*", XmlFull).
> > > []
> > >
> > >
> > > Line 16 seems to confirm that XmlFull contains my XML string.
> > > However, I'd expect line 17 to give me something like "Hello" or
> > > ["Hello"], yet it returns an empty list.
> > >
> > > Could someone please point out the (no doubt very simple) mistake I've
> > > made?  Also, if a "Simple guide to xmerl_xpath" exists somewhere,
> > > could someone please point me towards it?
> > >
> > > Thanks in advance
> > >
> > > Dave M.
> > > _______________________________________________
> > > erlang-questions mailing list
> > > 
> > > http://www.erlang.org/mailman/listinfo/erlang-questions
> > >
> >
> >
> > --
> > - Caoyuan
> >
> _______________________________________________
> erlang-questions mailing list
> 
> http://www.erlang.org/mailman/listinfo/erlang-questions
>


-- 
- Caoyuan



More information about the erlang-questions mailing list