xmerl_xpath difficulties

View: New views
5 Messages — Rating Filter:   Alert me  

xmerl_xpath difficulties

by David Mitchell-5 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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
erlang-questions@...
http://www.erlang.org/mailman/listinfo/erlang-questions

Parent Message unknown Re: xmerl_xpath difficulties

by David Mitchell-5 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Thanks Caoyuan, but it didn't work for me:

davidm@davidm-desktop:~$ 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 <dcaoyuan@...> wrote:

> > [XmlText] = xmerl_xpath:string("/text/text()", XmlFull).
> [{xmlText,[{text,1}],1,[],"Hello",text}]
> > element(5, XmlText).
> "Hello"
>
>
> On 8/5/07, David Mitchell <monch1962@...> 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
> > erlang-questions@...
> > http://www.erlang.org/mailman/listinfo/erlang-questions
> >
>
>
> --
> - Caoyuan
>
_______________________________________________
erlang-questions mailing list
erlang-questions@...
http://www.erlang.org/mailman/listinfo/erlang-questions

Re: xmerl_xpath difficulties

by Jeremy Thurgood :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

David Mitchell wrote:

> 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 ;->

You might want to look at the W3C's xpath tutorial at
http://www.w3schools.com/xpath/default.asp

> 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.

You're asking it for xml elements below <text>, of which there are none.

5> xmerl_xpath:string("//text/text()", XmlFull).
[{xmlText,[{text,1}],1,[],"Hello",text}]

Look like you'll still have to extract the string from the #xmlText record.

HTH,
--J
_______________________________________________
erlang-questions mailing list
erlang-questions@...
http://www.erlang.org/mailman/listinfo/erlang-questions

Re: xmerl_xpath difficulties

by dcaoyuan :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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 <monch1962@...> wrote:

> Thanks Caoyuan, but it didn't work for me:
>
> davidm@davidm-desktop:~$ 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 <dcaoyuan@...> wrote:
> > > [XmlText] = xmerl_xpath:string("/text/text()", XmlFull).
> > [{xmlText,[{text,1}],1,[],"Hello",text}]
> > > element(5, XmlText).
> > "Hello"
> >
> >
> > On 8/5/07, David Mitchell <monch1962@...> 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
> > > erlang-questions@...
> > > http://www.erlang.org/mailman/listinfo/erlang-questions
> > >
> >
> >
> > --
> > - Caoyuan
> >
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@...
> http://www.erlang.org/mailman/listinfo/erlang-questions
>


--
- Caoyuan
_______________________________________________
erlang-questions mailing list
erlang-questions@...
http://www.erlang.org/mailman/listinfo/erlang-questions

Re: xmerl_xpath difficulties

by David Mitchell-5 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Thanks again Caoyuan,

OK, I think I've got it - there's a bug in Erlang on Ubuntu 6.06.  No
idea if it's a bug in Erlang itself (i.e. version 5.4.9 vs. 5.5.5), or
a bug in the implementation of Erlang in Ubuntu, but it seems pretty
straightforward.  The Windows version works OK, but the Ubuntu version
doesn't.

Here's 2 sessions: the first on Ubuntu and the 2nd on Windows

davidm@davidm-desktop:~$ 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).
[]
3> lists:flatten([element(5,X)|| X <- XmlTexts, element(1,X) == xmlText]).
[]
4>


Now the Windows version:

D:\Program Files\erl5.5.5\bin>erl
Eshell V5.5.5  (abort with ^G)
1> {XmlFull, _} = xmerl_scan:string("<text>Hello</text>").
{{xmlElement,text,
             text,
             [],
             {xmlNamespace,[],[]},
             [],
             1,
             [],
             [{xmlText,[{text,1}],1,[],"Hello",text}],
             [],
             "D:/Program Files/erl5.5.5/bin",
             undeclared},
 []}
2> XmlText = xmerl_xpath:string("/text/text()", XmlFull).
[{xmlText,[{text,1}],1,[],"Hello",text}]
3>


Note the difference in the value of XmlText in both cases - this is a
fairly trivial issue to reproduce, being only 2 lines of code.

I'm still finding my feet in the Erlang world - can someone pls give
me a pointer as to how I should file a bug report on this?  Even if it
doesn't get fixed, I'd like it documented (and searchable) so that the
next person who stumbles on it doesn't get bogged down as I did.

Thanks to everyone for their help tracking this down.

Regards

Dave M.



On 05/08/07, Caoyuan <dcaoyuan@...> wrote:

> 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 <monch1962@...> wrote:
> > Thanks Caoyuan, but it didn't work for me:
> >
> > davidm@davidm-desktop:~$ 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 <dcaoyuan@...> wrote:
> > > > [XmlText] = xmerl_xpath:string("/text/text()", XmlFull).
> > > [{xmlText,[{text,1}],1,[],"Hello",text}]
> > > > element(5, XmlText).
> > > "Hello"
> > >
> > >
> > > On 8/5/07, David Mitchell <monch1962@...> 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
> > > > erlang-questions@...
> > > > http://www.erlang.org/mailman/listinfo/erlang-questions
> > > >
> > >
> > >
> > > --
> > > - Caoyuan
> > >
> > _______________________________________________
> > erlang-questions mailing list
> > erlang-questions@...
> > http://www.erlang.org/mailman/listinfo/erlang-questions
> >
>
>
> --
> - Caoyuan
>
_______________________________________________
erlang-questions mailing list
erlang-questions@...
http://www.erlang.org/mailman/listinfo/erlang-questions
LightInTheBox - Buy quality products at wholesale price!