Wie greife ich mit XPath in Java auf OWL-Dokumente zu?
Ich habe ein OWL-Dokument in Form einer XML-Datei. Ich möchte Elemente aus diesem Dokument extrahieren. Mein Code funktioniert für einfache XML-Dokumente, aber nicht für OWL-XML-Dokumente.
Eigentlich habe ich dieses Element gesucht:/rdf:RDF/owl:Ontology/rdfs:label
, für die ich das gemacht habe:
DocumentBuilder builder = builderfactory.newDocumentBuilder();
Document xmlDocument = builder.parse(
new File(XpathMain.class.getResource("person.xml").getFile()));
XPathFactory factory = javax.xml.xpath.XPathFactory.newInstance();
XPath xPath = factory.newXPath();
XPathExpression xPathExpression = xPath.compile("/rdf:RDF/owl:Ontology/rdfs:label/text()");
String nameOfTheBook = xPathExpression.evaluate(xmlDocument,XPathConstants.STRING).toString();
Ich habe auch versucht, nur die zu extrahierenrdfs:label
Element auf diese Weise:
XPathExpression xPathExpression = xPath.compile("//rdfs:label");
NodeList nodes = (NodeList) xPathExpression.evaluate(xmlDocument, XPathConstants.NODESET);
Aber diese Knotenliste ist leer.
Bitte lassen Sie mich wissen, wo ich falsch liege. Ich benutze Java XPath API.