XML Literals in RDFa

Toby A Inkster mail at tobyinkster.co.uk
Sun Jan 18 15:51:59 CET 2009


On 18 Jan 2009, at 13:26, Kjetil Kjernsmo wrote:

> When working on data, I find it most useful to have a the right  
> objects
> to work on, so I would much prefer to have a XML::LibXML::Node  
> returned
> from the parser, but since the RDFa spec is very clear about how such
> things should be serialised [1], Toby is doing the right thing.

> The easiest way to go about, I suppose is to keep the parser as it is,
> and in any higher levels parse the serialised string again.

Oh, no, no - you don't want to be doing that. It's wasteful.

You should be able to use callback functions to achieve what you  
want. How to do this depends on how you're using RDF::RDFa::Parser -  
there are effectively two ways of using the module:

1. Set callback functions for handling triples, and deal with them  
one-by-one.
2. Parse the whole file, and then call ->graph to get back a complete  
graph of triples.

If you're using the first technique, then you should find that $_[1]  
passed to your callback functions is an XML::LibXML::Element  
representing the current element being parsed. That should be all you  
need to know.

If you're using the second technique, which I'd probably recommend  
for most cases, then you can use a little callback cheat to store the  
XML::LibXML::Element within the graph structure returned by ->graph.

$parser->set_callbacks(undef, sub
{
   my $parser = shift;

   # now...
   # $_[0] is the XML::LibXML::Element
   # $_[1] is the subject URI
   # $_[2] is the predicate URI
   # $_[3] is the object literal
   # $_[4] is the object datatype
   # $_[5] is the object language

   $parser->graph->{$_[1]}->{$_[2]}->[-1]->{'node'} = $_[0]
     if ($_[4] eq 'http://www.w3.org/1999/02/22-rdf-syntax- 
ns#XMLLiteral');
});

The above sets a callback for all XML literals, but because of the if  
clause within the function, it only does anything useful for  
XMLLiterals. Callbacks are called *after* a triple has been added to  
the graph, so they can tinker with the graph.

-- 
Toby A Inkster
<mailto:mail at tobyinkster.co.uk>
<http://tobyinkster.co.uk>





More information about the Dev mailing list