문제

I have a loaded an RDF::Redland::Model parsed from an RDF/XML document and I want to add tag <foaf::nick> in it. How can I do it?

Update:

This is my code so far:

my $st = RDF::Redland::Node->new_xml_literal("<foaf:nick>content</foaf:content>");
$self->{Model}->add_typed_literal_statement($st);
print $self->{Model}->to_string; 

and it doesn't work. What am I doing wrong?

도움이 되었습니까?

해결책

RDF models don't contain tags (or even XML elements); an RDF model is a set of triples of the form subject predicate object. foaf:nick is a property that relates a foaf:Person to a nickname. Based on the title of your question, it sounds like that what you want to do is add a triple something foaf:nick somenick to the model. In this case, the documentation for RDF::Redland::Model indicates that what you'll want is either add or add_typed_literal_statement:

add SUBJECT PREDICATE OBJECT

Add a new statement to the model with SUBJECT, PREDICATE and OBJECT. These can be RDF::Redland::Node, RDF::Redland::URI or perl URI objects.

add_typed_literal_statement SUBJECT PREDICATE STRING [XML_LANGUAGE [DATATYPE]]

Add a new statement to the model containing a typed literal string object STRING with (optional) XML language (xml:lang attribute) XML_LANGUAGE and (optional) datatype URI DATATYPE. XML_LANGUAGE or DATATYPE can either or both be set to undef.

In the first case, it appears that you'd need to create a RDF::Redland::Node to represent the literal string that is the nickname. There is a constructor that take a string argument In the second case, which is probably easier, you can just use the string directly.

Edit

Now that you've posted your code, the particular issue becomes clearer, though you still haven't mentioned what's actually going wrong.

my $st = RDF::Redland::Node->new_xml_literal("<foaf:nick>content</foaf:content>");
$self->{Model}->add_typed_literal_statement($st);

First, if I understand what you're trying to do, the XML content that you've used isn't well formed, as the opening and closing tags don't match (nick is not content). More problematically, this isn't how RDF works. RDF is a graph-oriented representation, where the basic concept is a statement of the form

subject predicate object

and an RDF graph, or model, is a set of these. Viewing each statement as a directed labelled edge from subject to object, we obtain a graph. RDF models can be serialized in a number of formats, one of which is RDF/XML, which is also the one that you're working with. However, as I've pointed out in this answer, the point of an RDF API is to let you work with the statements of an RDF graph, and to not be concerned with the particular serialization of the graph.

It sounds like you want to add a statement

$something foaf:nick "content"

to the graph, for some subject which you haven't identified. According to the documentation that I linked to and quoted above, add_typed_literal_statement takes three arguments, not one, and the third argument should be a string. Your second line should look something like:

$self->{Model}->add_typed_literal_statement($subject,$foafNick,"content");

where $subject and $foafNick are URI Nodes. E.g., you might do this (untested):

$subject = new RDF::Redland::URINode("https://stackoverflow.com/users/400371");
$foafNick = new RDF::Redland::URINode("http://xmlns.com/foaf/0.1/nick");
$self->{Model}->add_typed_literal_statement($subject,$foafNick,"Nikita");

to add the triple

<https://stackoverflow.com/users/400371> foaf:nick "Nikita"

to the model.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top