简体   繁体   中英

How to preserve namespace prefixes in a Jena TDB dataset?

I am using Jena TDB (1.1.1) to store a set of named graphs. Everything works fine but whenever I retrieve a named graph from the the dataset, all the namespace prefix information is lost. Is there a way to preserve the namespace prefixes in the original RDF graph.

Following code snippet shows the issue.

@Test
public void testPreserveNsPrefixes(){

    String modelText = "@prefix ro:    <http://purl.org/wf4ever/ro#> ." +
            "@prefix ore:   <http://www.openarchives.org/ore/terms/> ." +
            "@prefix ldp:   <http://www.w3.org/ns/ldp#> ." +
            "<http://example.org/ro> a ore:Aggregation , ro:ResearchObject , ldp:DirectContainer .\n" ;

    // Build the RDF graph
    InputStream stream = new ByteArrayInputStream(modelText.getBytes(StandardCharsets.UTF_8));
    Model model = ModelFactory.createDefaultModel();
    model.read(stream, null, "TURTLE");
    System.out.println("NS prefix count: " + model.getNsPrefixMap().size());

    //Create a dataset
    Dataset dataset = TDBFactory.createDataset("test");

    // Add the RDF graph to the dataset
    dataset.begin(ReadWrite.WRITE) ;
    try {
        dataset.addNamedModel("http://example.org/ro", model);
        dataset.commit() ;
    } finally {
        dataset.end() ;
    }

    //Read the RDF graph again
    dataset.begin(ReadWrite.READ);
    try{
        Model model2 = dataset.getNamedModel("http://example.org/ro");
        model2.write(System.out, "TURTLE");
        System.out.println("NS prefix count: " + model2.getNsPrefixMap().size());
    } finally {
        dataset.end();
    }
}

The output of this is:

    NS prefix count: 3
    <http://example.org/ro>
        a       <http://www.w3.org/ns/ldp#DirectContainer> ,
                <http://purl.org/wf4ever/ro#ResearchObject> ,
                <http://www.openarchives.org/ore/terms/Aggregation> .
    NS prefix count: 0

I think is related to this question though I don't think it is an exact duplicate.

只是为了避免删除注释后答案消失, AndyS 提到 ,这已在Jena用户的邮件列表中进行了讨论 ,并在问题JENA-860中得到解决添加图不添加前缀

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM