[erlang-questions] xmerl with multiple items

17 views
Skip to first unread message

Joe Williams

unread,
Sep 22, 2008, 9:54:45 AM9/22/08
to - Erlang Questions

I am working on one of the xmerl examples found at
http://blog.tornkvist.org/blog.yaws?id=1193209275268448 specifically,

##########
-module(xp).
-export([go/1]).
-include_lib("xmerl/include/xmerl.hrl").
-define(Val(X),
(fun() ->
[#xmlElement{name = N,
content = [#xmlText{value = V}|_]}] = X,
{N,V} end)()).

go(File) ->
{Xml, _} = xmerl_scan:file(File),
[?Val(xmerl_xpath:string("//SKU", Xml)),
?Val(xmerl_xpath:string("//ItemName", Xml)),
?Val(xmerl_xpath:string("//CollectionNo", Xml)),
?Val(xmerl_xpath:string("//Pages", Xml))].
##########


The issue that I am having is if I add to the example XML file that the
site provides. I am changing the following,


##########
<Export>

| <Product>
<SKU>403276</SKU>
<ItemName>Trivet</ItemName>
<CollectionNo>0</CollectionNo>
<Pages>0</Pages>
</Product>
</Export>
|##########


to

##########

<Export>
<Product>
<SKU>403276</SKU>
<ItemName>Trivet</ItemName>
<CollectionNo>0</CollectionNo>
<Pages>0</Pages>
</Product>
<Product>
<SKU>123412</SKU>
<ItemName>Book</ItemName>
<CollectionNo>1</CollectionNo>
<Pages>2</Pages>
</Product>
</Export>

##########


When I run it against the XML it complains of,

##########

3> xp:go("xp.xml").
** exception error: no match of right hand side value
[{xmlElement,'SKU','SKU',[],
<snip>

##########


What is the best way to fix this issue so I can have the code work for
one to many items?

Thanks.
-joe

--
Name: Joseph A. Williams
Email: j...@joetify.com

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

Steve Vinoski

unread,
Sep 22, 2008, 10:24:34 AM9/22/08
to Joe Williams, - Erlang Questions
On 9/22/08, Joe Williams <j...@joetify.com> wrote:
>
> I am working on one of the xmerl examples found at
> http://blog.tornkvist.org/blog.yaws?id=1193209275268448 specifically,

<snip/>

> What is the best way to fix this issue so I can have the code work for
> one to many items?

Change the Val macro to:

-define(Val(X),
(fun() ->
[{N,V} || #xmlElement{name = N, content = [#xmlText{value
= V}|_]} <- X]
end)()).

When xpath matches multiple items, this allows all matches to appear
in the result, and it still works for one match as well.

--steve

Joe Williams

unread,
Sep 22, 2008, 10:34:48 AM9/22/08
to Steve Vinoski, - Erlang Questions
Works great, thanks for the help.

-joe

Steve Vinoski wrote:
> On 9/22/08, Joe Williams <j...@joetify.com> wrote:
>
>> I am working on one of the xmerl examples found at
>> http://blog.tornkvist.org/blog.yaws?id=1193209275268448 specifically,
>>
>
> <snip/>
>
>
>> What is the best way to fix this issue so I can have the code work for
>> one to many items?
>>
>
> Change the Val macro to:
>
> -define(Val(X),
> (fun() ->
> [{N,V} || #xmlElement{name = N, content = [#xmlText{value
> = V}|_]} <- X]
> end)()).
>
> When xpath matches multiple items, this allows all matches to appear
> in the result, and it still works for one match as well.
>
> --steve
>

--

Name: Joseph A. Williams
Email: j...@joetify.com

_______________________________________________

Reply all
Reply to author
Forward
0 new messages