Namespace prefix not declared error after extracting a node in OWL/XML file with Java & xPath

StackOverflow https://stackoverflow.com/questions/22170071

  •  02-06-2023
  •  | 
  •  

Question

Initially I have this file.

<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
    <owl:Class />
    <owl:Class />
    <owl:ObjectProperty />
    <Situation:Situation rdf:about"http://localhost/rdf#situa0">
        <Situation:composedBy />
    </Situation:Situation>
</rdf:RDF>

My goal is to extract the node Situation and its content using xPath "RDF/Situation" ...

<Situation:Situation rdf:about"http://localhost/rdf#situa0">
    <Situation:composedBy />
</Situation:Situation>

I found a good example to work with in Java How to extract a complete XML block.

I changed names of tags to my own since I use namespaces and predefined tags.

Here's my code

 public static void main(String... args) throws Exception {
        String xml = "<rdf:RDF xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\"><owl:Class /><owl:Class /><owl:ObjectProperty /><Situation:Situation rdf:about=\"http://localhost/rdf#situa0\" ><Situation:composedBy /></Situation:Situation></rdf:RDF>";
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        Document doc = dbf.newDocumentBuilder().parse(
                new InputSource(new StringReader(xml)));

        XPath xPath = XPathFactory.newInstance().newXPath();
        Node result = (Node) xPath.evaluate("RDF/Situation", doc, XPathConstants.NODE);

        System.out.println(nodeToString(result));
    }

    private static String nodeToString(Node node) throws TransformerException {
        StringWriter buf = new StringWriter();
        Transformer xform = TransformerFactory.newInstance().newTransformer();
        xform.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        xform.transform(new DOMSource(node), new StreamResult(buf));
        return (buf.toString());
    }

My goal is 90% achieved but I have a problem, the Situation tag has an attribute about with a prefix rdf (the code works if I remove the prefix, and even if I added rdf xmlns in the root element)

<Situation:Situation rdf:about="http://localhost/rdf#situa0">

I got this error

ERROR: 'The namespace prefix' rdf 'has not been declared.' Exception in thread "main" javax.xml.transform.TransformerException: java.lang.RuntimeException: Namespace prefix 'rdf' has not been declared. com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform at (Unknown Source) com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform at (Unknown Source)

I added dbf.setNamespaceAware(true) as @ Ian Roberts mentioned, so I got other errors asking for owl & Situation namespaces, after adding it in the root tag, I got nothing in output and without errors. What is the problem ?? The problem was that the variable result, this time is null, so there's a problem with the xPath query..

I tried to see in another place the result of the query and it worked fine in an online xPath tester.

enter image description here

So what is the problem ??

Is there any other way to do like this job.???

thx :)

Was it helpful?

Solution

Is there any other way to do like this job?

Yes, there are other,more appropriate, ways to do this job.

It's typically not a great idea to try to process RDF documents using XML tools, since the same RDF graph can often be represented a number of different ways in RDF/XML. This is discussed in more detail in my answer to How to access OWL documents using XPath in Java?, but we can see the issue pretty quickly here. After adding some additional namespace declarations your data looks like this:

<rdf:RDF
    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
    xmlns:Situation="https://stackoverflow.com/q/22170071/1281433/"
    xmlns:owl="http://www.w3.org/2002/07/owl#">
  <owl:Class/>
  <owl:Class/>
  <owl:ObjectProperty/>
  <Situation:Situation rdf:about="http://localhost/rdf#situa0">
    <Situation:composedBy></Situation:composedBy>
  </Situation:Situation>
</rdf:RDF>

The same RDF graph can be serialized like this, too:

<rdf:RDF
    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
    xmlns:Situation="https://stackoverflow.com/q/22170071/1281433/"
    xmlns:owl="http://www.w3.org/2002/07/owl#" > 
  <rdf:Description rdf:nodeID="A0">
    <rdf:type rdf:resource="http://www.w3.org/2002/07/owl#Class"/>
  </rdf:Description>
  <rdf:Description rdf:about="http://localhost/rdf#situa0">
    <rdf:type rdf:resource="https://stackoverflow.com/q/22170071/1281433/Situation"/>
    <Situation:composedBy></Situation:composedBy>
  </rdf:Description>
  <rdf:Description rdf:nodeID="A1">
    <rdf:type rdf:resource="http://www.w3.org/2002/07/owl#ObjectProperty"/>
  </rdf:Description>
  <rdf:Description rdf:nodeID="A2">
    <rdf:type rdf:resource="http://www.w3.org/2002/07/owl#Class"/>
  </rdf:Description>
</rdf:RDF>

If you're looking for a Situation:Situation element, you'll find one in the first serialization, but not the second, even though they're the same RDF graph.

You could probably use a SPARQL query to get what you're looking for. The typical implementation of describe queries might do what you want. E.g., the very simple query

describe <http://localhost/rdf#situa0>

produces this result (in RDF/XML):

<rdf:RDF
    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
    xmlns:Situation="https://stackoverflow.com/q/22170071/1281433/"
    xmlns:owl="http://www.w3.org/2002/07/owl#">
  <Situation:Situation rdf:about="http://localhost/rdf#situa0">
    <Situation:composedBy></Situation:composedBy>
  </Situation:Situation>
</rdf:RDF>

Alternatively, you could ask for everything that has the type Situation:Situation:

prefix s: <https://stackoverflow.com/q/22170071/1281433/>
describe ?situation where {
  ?situation a s:Situation .
}
<rdf:RDF
    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
    xmlns:s="https://stackoverflow.com/q/22170071/1281433/"
    xmlns:owl="http://www.w3.org/2002/07/owl#">
  <s:Situation rdf:about="http://localhost/rdf#situa0">
    <s:composedBy></s:composedBy>
  </s:Situation>
</rdf:RDF>

The important point here is to use an appropriate query language for the type of data that you have. You have RDF, which is a graph-based data representation. An RDF graph is a set of triples. Your data is five triples:

_:BX2D6970b66dX3A1448f4e1bcfX3AX2D7ffe <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#Class> .
<http://localhost/rdf#situa0> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <https://stackoverflow.com/q/22170071/1281433/Situation> .
<http://localhost/rdf#situa0> <https://stackoverflow.com/q/22170071/1281433/composedBy> "" .
_:BX2D6970b66dX3A1448f4e1bcfX3AX2D7ffd <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#ObjectProperty> .
_:BX2D6970b66dX3A1448f4e1bcfX3AX2D7fff <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#Class> .

In the Turtle serialization, the graph is:

@prefix owl:   <http://www.w3.org/2002/07/owl#> .
@prefix rdf:   <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix Situation: <https://stackoverflow.com/q/22170071/1281433/> .

[ a       owl:Class ] .

<http://localhost/rdf#situa0>
        a                     Situation:Situation ;
        Situation:composedBy  "" .

[ a       owl:Class ] .

[ a       owl:ObjectProperty ] .

You should use SPARQL (the standard RDF query language) or an RDF-based API for extracting data from RDF documents.

OTHER TIPS

There are several ways you can parse the file without actually having the namespaces in your XML file. You can add them to your root node directly:

rootElement.setAttribute("xmlns:owl", "http://www.w3.org/2002/07/owl");
rootElement.setAttribute("xmlns:Situation", "http://localhost/Situation.owl#");

or you can configure a namespace resolver:

xPath.setNamespaceContext(new NamespaceContext() {
    public String getNamespaceURI(String prefix) {
        if (prefix.equals("rdf")) {
            return "http://www.w3.org/1999/02/22-rdf-syntax-ns#";
        } else if (prefix.equals("owl")) {
            return "http://www.w3.org/2002/07/owl";
        } else if (prefix.equals("Situation")) {
            return "http://localhost/Situation.owl#";
        } else {
            return XMLConstants.NULL_NS_URI;
        }
    }
    public String getPrefix(String namespaceURI) { return null;}
    public Iterator getPrefixes(String namespaceURI) { return null;}
});

You can also use a namespace-independent XPath expression:

xPath.evaluate("/*[local-name()='RDF']/*[local-name()='Situation']", doc, XPathConstants.NODE);

But it seems that you are having errors with the transformer. It's not finding the rdf namespace. That's weird. Perhaps it's not being correctly copied to the result node since it's declared in an attribute and for some reason the parser did not copy it (I'm just guessing). There might be a nicer way to fix that, but you could also explicitly add the namespace prefix to the result node before sending it to the transformer. Cast it to Element and then use addAttribute:

Element result = (Element) xPath.evaluate("/RDF/Situation", doc, XPathConstants.NODE);
result.setAttribute("xmlns:rdf", "http://www.w3.org/1999/02/22-rdf-syntax-ns#");
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top