1

我试图弄清楚如何在使用 Jena 编写 RDF/XML 时解除对 propertyAttr 的阻止。正如文档所说,“默认情况下,规则 propertyAttr 被阻止。” 我已经尝试过了,但是我无法在 RDF/XML-ABBREV 模式下阻止它。

RDFWriter w = m.getWriter("RDF/XML-ABBREV");
w.setProperty("tab", "2");
w.setProperty("blockRules",  "propertyAttr");
w.write(m, System.out, "");

这是我想要的输出,eg:value="v"设置为属性:

<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
         xmlns:eg="http://example.org/"
         xml:base="http://example.org/dir/file">
  <rdf:Description rdf:ID="frag" eg:value="v" />
</rdf:RDF>

这是我的真实输出:

<rdf:RDF
    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
    xmlns:j.0="http://example.org/">
  <rdf:Description rdf:about="http://example.org/dir/filefrag">
    <j.0:value>v</j.0:value>
  </rdf:Description>
</rdf:RDF>
4

1 回答 1

2

您发布的代码,

w.setProperty("blockRules",  "propertyAttr");

不会取消阻止"propertyAttr"规则。相反,它会阻止它。从文档

blockRules: Resource 列表或 String 是来自http://www.w3.org/TR/rdf-syntax-grammar的片段 ID 的逗号分隔列表,指示将不会使用的语法规则。可以被阻止的规则是:……</p>

  • propertyAttr (RDFSyntax.propertyAttr)

所以你的代码实际上是在阻止作者使用propertyAttr规则。

以下代码在执行后打印您的模型

writer.setProperty( "blockRules", "" );

并产生输出(这是您想要的输出):

  <rdf:Description rdf:about="http://example.org/dir/file#frag"
     j.0:value="v"/>

这是我使用的完整代码。实际上,它会打印两次模型。第一次"blockRules"设置为"propertyAttr",第二次设置为""

import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.rdf.model.Property;
import com.hp.hpl.jena.rdf.model.RDFWriter;
import com.hp.hpl.jena.rdf.model.Resource;

public class PropertyWriter {
    public static void main(String[] args) {
        Model model = ModelFactory.createDefaultModel();
        Resource frag = model.createResource( "http://example.org/dir/file#frag" );
        Property value = model.createProperty( "http://example.org/value" );
        frag.addProperty( value, "v" );
        
        RDFWriter writer = model.getWriter( "RDF/XML-ABBREV" );
        
        writer.setProperty( "blockRules", "propertyAttr" );
        writer.write( model, System.out, null );
        
        writer.setProperty( "blockRules",  "" );
        writer.write( model, System.out, null );
    }
}

这是输出(模型打印两次,第二次是您想要的格式):

<rdf:RDF
    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
    xmlns:j.0="http://example.org/">
  <rdf:Description rdf:about="http://example.org/dir/file#frag">
    <j.0:value>v</j.0:value>
  </rdf:Description>
</rdf:RDF>
<rdf:RDF
    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
    xmlns:j.0="http://example.org/">
  <rdf:Description rdf:about="http://example.org/dir/file#frag"
     j.0:value="v"/>
</rdf:RDF>
于 2013-06-07T13:46:17.740 回答