NAME

RDF::RDFa::Parser - RDFa parser using XML::LibXML.

SYNOPSIS

use RDF::RDFa::Parser;

$parser = RDF::RDFa::Parser->new($xhtml, $baseuri);
$parser->consume;
$graph = $parser->graph;

VERSION

0.20

Note: version 0.20 introduced major incompatibilities with 0.0x and 0.1x.

PUBLIC METHODS

$p = RDF::RDFa::Parser->new($xhtml, $baseuri, \%options, $storage)

This method creates a new RDF::RDFa::Parser object and returns it.

The $xhtml variable may contain an XHTML/XML string, or a XML::LibXML::Document. If a string, the document is parsed using XML::LibXML, which will throw an exception if it is not well-formed. RDF::RDFa::Parser does not catch the exception.

The base URI is used to resolve relative URIs found in the document.

Options (mostly booleans) [default in brackets]:

* full_uris       - Support full URIs in CURIE-only attributes. [0]
* strict_reserved_words  - Be strict about reserved words. [1]
* xfn_reserved_words     - Allow XFN 1.1 reserved words. [0]
* embedded_rdfxml - Find plain RDF/XML chunks within document. [0]
                    0=no, 1=handle, 2=skip.
* xml_base        - Support for 'xml:base' attribute. [0]
                    0=only RDF/XML; 1=except @href/@src; 2=always.
* xhtml_elements  - Process <head> and <body> specially. [1]
* xhtml_lang      - Support @lang rather than just @xml:lang. [0]
* xhtml_base      - Process <base> element. [1]
                    0=no, 1=yes, 2=use it for RDF/XML too
* prefix_attr     - Support @prefix rather than just @xmlns:foo. [0]
* tdb_service     - Use thing-described-by.org to name some bnodes. [0]

The default options attempt to stick to the XHTML+RDFa spec as rigidly as possible.

$storage is an RDF::Trine::Storage object. If undef, then a new temporary store is created.

$p->xhtml

Returns the XHTML source of the document being parsed.

$p->uri

Returns the base URI of the document being parsed. This will usually be the same as the base URI provided to the constructor, but may differ if the document contains a <base> HTML element.

Optionally it may be passed a parameter - an absolute or relative URI - in which case it returns the same URI which it was passed as a parameter, but as an absolute URI, resolved relative to the document's base URI.

This seems like two unrelated functions, but if you consider the consequence of passing a relative URI consisting of a zero-length string, it in fact makes sense.

$p->dom

Returns the parsed XML::LibXML::Document.

$p->set_callbacks(\&func1, \&func2)

Set callbacks for handling RDF triples extracted from RDFa document. The first function is called when a triple is generated taking the form of (resource, resource, resource). The second function is called when a triple is generated taking the form of (resource, resource, literal).

The parameters passed to the first callback function are:

  • A reference to the RDF::RDFa::Parser object

  • A reference to the XML::LibXML element being parsed

  • Subject URI or bnode

  • Predicate URI

  • Object URI or bnode

  • Graph URI or bnode (if named graphs feature is enabled)

The parameters passed to the second callback function are:

  • A reference to the RDF::RDFa::Parser object

  • A reference to the XML::LibXML element being parsed

  • Subject URI or bnode

  • Predicate URI

  • Object literal

  • Datatype URI (possibly undef or '')

  • Language (possibly undef or '')

  • Graph URI or bnode (if named graphs feature is enabled)

In place of either or both functions you can use the string 'print' which sets the callback to a built-in function which prints the triples to STDOUT as Turtle. Either or both can be set to undef, in which case, no callback is called when a triple is found.

Beware that for literal callbacks, sometimes both a datatype *and* a language will be passed. (This goes beyond the normal RDF data model.)

set_callbacks must be used before consume.

IMPORTANT - CHANGED IN VERSION 0.20 - callback functions should return true if they wish to prevent the triple from being added to the parser's built-in model; false otherwise.

$p->named_graphs($xmlns, $attribute, $attributeType)

RDF::RDFa::Parser allows for one RDFa document to generate multiple graphs. A graph is created by enclosing it in an element with an attribute with XML namespace $xmlns and local name $attribute.

Each graph is given a URI - if $attributeType is the string 'id', then the URI is generated by treating the attribute like an 'id' attribute - i.e. the URI is the document's base URI, followed by a hash, followed by the attribute value. If $attributeType is the string 'about', then the URI is generated by treating the attribute like an 'about' attribute - i.e. it is treated as an absolute or relative URI, with safe CURIEs being allowed too. If the $attributeType is omitted, then the default behaviour is 'about'.

Calling this method with no parameters will disable the named graph feature. Named graphs are disabled by default.

named_graphs must be used before consume.

$p->thing_described_by(1)

RDF::RDFa::Parser has a feature that allows it to use thing-described-by.org to create URIs for some blank nodes. It is disabled by default. This function can be used to turn it on (1) or off (0). May be called without a parameter, which just returns the current status.

thing_described_by must be used before consume.

THIS FUNCTION IS DEPRECATED. PASS AN OPTION TO THE CONSTRUCTOR INSTEAD.

$p->consume

The document is parsed for RDFa. Nothing of interest is returned by this function, but the triples extracted from the document are passed to the callbacks as each one is found.

$p->graph( [ $graph_name ] )

Without a graph name, this method will return an RDF::Trine::Model object with all statements of the full graph. As per the RDFa specification, it will always return an unnamed graph containing all the triples of the RDFa document. If the model contains multiple graphs, all triples will be returned unless a graph name is specified.

It will also take an optional graph URI as argument, and return an RDF::Trine::Model tied to a temporary storage with all triples in that graph.

$p->graphs

Will return a hashref of all named graphs, where the graph name is a key and the value is a RDF::Trine::Model tied to a temporary storage.

CONSTANTS

RDF::RDFa::Parser::OPTS_XHTML

Suggested options hashref for parsing XHTML.

RDF::RDFa::Parser::OPTS_SVG

Suggested options hashref for parsing SVG.

RDF::RDFa::Parser::OPTS_XML

Suggested options hashref for parsing generic XML.

SEE ALSO

RDF::RDFa::Parser::Trine, XML::LibXML.

http://buzzword.org.uk/swignition/rdfa

COPYRIGHT

Copyright 2008, 2009 Toby Inkster

This file is dual licensed under:
The Artistic License
GNU General Public License 3.0

You may choose which of those two licences you are going to honour the
terms of, but you cannot pick and choose the parts which you like of
each. You must fulfil the licensing requirements of at least one of the
two licenses.

The Artistic License
<http://www.perl.com/language/misc/Artistic.html>

GNU General Public License 3.0
<http://www.gnu.org/licenses/gpl-3.0.html>

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.