[erlang-questions] xmerl SAX interface

Ulf Wiger (TN/EAB) <>
Tue Feb 6 11:29:40 CET 2007


I was hoping that someone else would answer this, but 
here goes - a very simple example of the underlying 
event mechanism in xmerl_scan:

1> S = "<e1 xmlns='urn'> <e2 a2='b'>2</e2> <e3>3</e3> </e1>".
"<e1 xmlns='urn'> <e2 a2='b'>2</e2> <e3>3</e3> </e1>"
2> xmerl_scan:string(S, [{event_fun, fun(E,S) -> io:format("E = ~p~n", [E]), S end}]).
E = {xmerl_event,started,1,1,undefined,document}
E = {xmerl_event,ended,
                 1,
                 17,
                 undefined,
                 {xmlAttribute,xmlns,[],[],[],[],1,[],"urn",false}}
E = {xmerl_event,started,
                 1,
                 2,
                 undefined,
                 {xmlElement,...}], 
...

>From xmerl_scan.erl:

%%% Events:
%%%
%%% #xmerl_event{event : started | ended,
%%%              line  : integer(),
%%%              col   : integer(),
%%%              data}
%%%
%%% Data                Events
%%% document            started, ended
%%% #xmlElement         started, ended
%%% #xmlAttribute       ended
%%% #xmlPI              ended
%%% #xmlComment         ended
%%% #xmlText            ended


I can't tell you what xmerl_eventp:stream_sax/3
does. I wrote xmerl_eventp:stream/2 a hundred years
ago, and it seems to be largely unchanged since then.
It was mainly meant to demonstrate use of the 
continuation function for stream parsing.

At that time, I didn't want to copy the SAX interface,
but focus more on the simple representation (S expressions)
and finding intuitive mappings between XML and Erlang.
So I made the event function as simple as I could.

The rather bulky records produced by xmerl came as a result
of attacking XPATH and XSLT. I eventually rejected XSLT
and decided that it would be better to do it erlang-style 
instead. I think Mikael Karlsson did a great job of 
illustrating that approach in
http://maven-plugins.sourceforge.net/maven-sdocbook-plugin/docbook/sdocbook.html

(I'm aware of the benefits of supporting an industry 
standard rather than coming up with your own unique
version, but adding XSLT to xmerl went way beyond the
level of effort I was prepared to put in, as I had no
need for it myself.)

I viewed it as a failure that I couldn't deal with XPATH,
namespaces, etc without resorting to huge records. I very
much favoured the simple format myself, but failed to 
come up with a smooth transition into the full XML 
functionality. But the whole xmerl thing was just an
experiment then, aimed at giving me an intuition about
XML. I believe it served that purpose well. Later, others
(notably Johan Blom, Richard Carlsson and Mickaël Remond)
took it further. My own experience of actually using xmerl
(or indeed XML), is - unfortunately - quite limited.  (:


BR,
Ulf W



> -----Original Message-----
> From:  
> [mailto:] On Behalf Of 
> Ladislav Lenart
> Sent: den 5 februari 2007 13:45
> To: Erlang-Questions (E-mail)
> Subject: [erlang-questions] xmerl SAX interface
> 
> Hello,
> 
> I am trying to implement a XML SAX driver using xmerl and I 
> got lost on the way a little. xmerl_eventp seems that it is 
> what I am looking for (SAX driver), but when I try to parse 
> the following
> 
>    <e1 xmlns='urn'>
>      <e2 a2='b'>2</e2>
>      <e3>3</e3>
>    </e1>
> 
> I get only the "closing" events which is not what I would 
> expect (from a SAX driver).
> 
> I have read the examples in xmerl 0.20 and used Google 
> extensively, but with no luck so far.
> 
> Can anyone please show me how to set up and SAX-parse a 
> simple XML document like the one above (namespaces are not 
> necessary but would be great, too).
> 
> Links to xmerl tutorials/faqs/... are also welcome.
> 
> Thanks in advance,
> 
> Ladislav Lenart
> 
> 
> 
> _______________________________________________
> erlang-questions mailing list
> 
> http://www.erlang.org/mailman/listinfo/erlang-questions
> 




More information about the erlang-questions mailing list