Jena: Only well-formed absolute URIrefs can be included in RDF/XML output: <A>

≡放荡痞女 提交于 2021-01-29 09:22:27

问题


I'm doing a parser from a csv file that builds an RDF. Right now it just adds as property the header of the csv and it's value. When I try to write the output as XML I get this error:

Caused by: org.apache.jena.shared.BadURIException: Only well-formed absolute URIrefs can be included in RDF/XML output: <A> Code: 57/REQUIRED_COMPONENT_MISSING in SCHEME: A component that is required by the scheme is missing.

But when I write it as json I get correct output.

Does anyone know what I have wrong? code:

public List<Model> createRDF(File file) throws Exception { //TODO implement custom Exceptions
    Csv csv = CsvReader.convertFileToCsv(file);
    List<Model> modelList = new ArrayList<>();
    for(int i = 1; i < csv.lines.length; i++) {
        Model model = ModelFactory.createDefaultModel();
        Resource r = model.createResource( "http://provisionalUri.com/" + i);
        addProperties(r, csv, model, i);
        modelList.add(model);
    }


    return modelList;
}

private void addProperties(Resource r, Csv csv, Model model, int i) {
    for(int j = 0; j < csv.lines[i].length; j++) { // if the columns have different length this will cause problems
        Property property = model.createProperty(csv.headers[j]);
        Literal value = model.createLiteral(csv.lines[i][j]);
        model.add(r, property, value);
    }

}

writing:

List<Model> models = service.createRDF(new File("./src/test/java/resources/test/Bienes_declarados_Patrimonio_mundial_de_la_UNESCO_en_España.csv"));
        for(Model model: models){
            RDFDataMgr.write(System.out, model, Lang.RDFXML);
        }

回答1:


RDF/XML has more requirements on URIs than JSON-LD because properties will be written as XML qnames.

model.createProperty(csv.headers[j]); isn't a legal absolute URI unless you took care with the CSV headers.

Two things are needed:

  1. Need to encode "csv.headers[j]" in case it has character that are bad for URIs.
  2. Need a proper URI e.g. add a "http://HOST/" to the front:

model.createProperty("http://example/"+encode(csv.headers[j]));



来源:https://stackoverflow.com/questions/59089081/jena-only-well-formed-absolute-urirefs-can-be-included-in-rdf-xml-output-a

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!