How to access OWL documents using XPath in Java?

I have an OWL document as an XML file. I want to extract elements from this document. My code works for simple XML documents, but it does not work with OWL XML documents.

I really wanted to get this element: /rdf:RDF/owl:Ontology/rdfs:labelfor which I did this:

 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();

I also tried to extract only the element rdfs:labelthis way:

 XPathExpression xPathExpression = xPath.compile("//rdfs:label");        
 NodeList nodes = (NodeList) xPathExpression.evaluate(xmlDocument, XPathConstants.NODESET);

But this nodelist is empty.

Please let me know where I am going wrong. I am using the Java XPath API.

+6
source share
3 answers

since xpath does not know the namespace you are using. try using:

"/*[local-name()='RDF']/*[local-name()='Ontology']/*[local-name()='label']/text()"

( , )

+2

RDF ( OWL) XPath

, @. RDF XML (, , RDF OWL), : RDF XML-. , , rdfs:label owl:Ontology, ? , .

OWL API, , Protégé. , .

<rdf:RDF xmlns="http://www.example.com/labelledOnt#"
     xml:base="http://www.example.com/labelledOnt"
     xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
     xmlns:owl="http://www.w3.org/2002/07/owl#"
     xmlns:xsd="http://www.w3.org/2001/XMLSchema#"
     xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
    <owl:Ontology rdf:about="http://www.example.com/labelledOnt">
        <rdfs:label>Here is a label on the Ontology.</rdfs:label>
    </owl:Ontology>
</rdf:RDF>

RDF, , RDF/XML. RDF , , OWL. owl:Ontology XML, XPath .

<rdf:RDF
    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
    xmlns:owl="http://www.w3.org/2002/07/owl#"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema#"
    xmlns="http://www.example.com/labelledOnt#"
    xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#" > 
  <rdf:Description rdf:about="http://www.example.com/labelledOnt">
    <rdf:type rdf:resource="http://www.w3.org/2002/07/owl#Ontology"/>
    <rdfs:label>Here is a label on the Ontology.</rdfs:label>
  </rdf:Description>
</rdf:RDF>

RDF RDF/XML, XML.

RDF SPARQL

, RDF XPath, ? RDF SPARQL. RDF , SPARQL , .

, , . 3- [subject,predicate,object]. .

  • , owl:Ontology. " " rdf:type, [?something,rdf:type,owl:Ontology].
  • , ( ) rdfs:label . [?something,rdfs:label,?label].

SPARQL, , .

PREFIX owl: <http://www.w3.org/2002/07/owl#>                                                                                                                                                   
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>                                                                                                                                           

SELECT ?label WHERE {                                                                                                                                                                          
  ?ontology a owl:Ontology ;                                                                                                                                                                   
            rdfs:label ?label .                                                                                                                                                                
}

( , rdf:type , SPARQL a . s p1 o1; p2 o2 . s p1 o1 . s p2 o2 ..)

SPARQL , . , . , , Jena arq, .

$ arq  --data labelledOnt.owl --query getLabel.sparql
--------------------------------------
| label                              |
======================================
| "Here is a label on the Ontology." |
--------------------------------------
+14

, javax.xml.namespace.NamespaceContext . , fooobar.com/questions/884706/..., , .

+1

All Articles