从OWL本体获取基本名称空间 [英] get base namespace from an OWL ontology
本文介绍了从OWL本体获取基本名称空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否有一种方法可以从OWL本体文件中获取基本名称空间,而无需使用DOM或类似方法,而仅使用Jena的API?例如,从OWL文件中:
Is there a way to get the base namespace from a OWL ontology file, without using DOM or similar, but just using Jena's API? E.g., from an OWL file:
<rdf:RDF
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:protege="http://protege.stanford.edu/plugins/owl/protege#"
xmlns="http://www.owl-ontologies.com/Ontology1254827934.owl#"
xmlns:xsp="http://www.owl-ontologies.com/2005/08/07/xsp.owl#"
xmlns:owl="http://www.w3.org/2002/07/owl#"
xmlns:xsd="http://www.w3.org/2001/XMLSchema#"
xmlns:swrl="http://www.w3.org/2003/11/swrl#"
xmlns:swrlb="http://www.w3.org/2003/11/swrlb#"
xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
xml:base="http://www.owl-ontologies.com/Ontology1254827934.owl">
如何在运行时获取http://www.owl-ontologies.com/Ontology1254827934.owl
?
推荐答案
或者如果您真的想要xml:base而不是空的xmlns:
Or if you really want the xml:base and not the empty xmlns:
final ArrayList<String> baseUriDropHere = new ArrayList<>();
DefaultHandler handler = new DefaultHandler() {
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
if("rdf:RDF".equals(qName)) {
for (int i=0; i<attributes.getLength(); i++) {
if("xml:base".equals(attributes.getQName(i))) {
baseUriDropHere.add(attributes.getValue(i));
return;
}
}
}
}
};
try {
SAXParserFactory factory = SAXParserFactory.newInstance();
factory.setValidating(false);
SAXParser parser = factory.newSAXParser();
parser.parse(FILENAME_HERE, handler);
} catch (ParserConfigurationException e) {
} catch (SAXException e) {
} catch (IOException e) {
}
if(baseUriDropHere.isEmpty()) {
System.out.println("no base uri set");
} else {
System.out.println(baseUriDropHere.get(0));
}
这篇关于从OWL本体获取基本名称空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文