How can I extract an XPath for various nodes of an RDF document. Is it possible?

What I need is if I give the atomic value, I should get the XPath for it.

<?xml version="1.0"?>

    <rdf:Description rdf:about="http://www.recshop.fake/cd/Empire Burlesque">
        <cd:artist>Bob Dylan</cd:artist>


If I give 'USA', it should return the XPath for USA.

asked 25 Jan '13, 04:25

vipincpy's gravatar image

accept rate: 0%

edited 25 Jan '13, 13:01

Signified's gravatar image

Signified ♦

Please be more specific and describe what you have tried or think could work.

(25 Jan '13, 05:52) Tomasz Plusk... Tomasz%20Pluskiewicz's gravatar image

(The OP edited to add an example. I edited to clean up the text.)

(25 Jan '13, 13:02) Signified ♦ Signified's gravatar image

XPath is a language for path specification in XML, so it's not clear to me what you mean with "I should get the XPath". Do you mean that you want an XPath expression that finds a particular element in this particular XML file (for your example the expression would be /rdf:RDF/rdf:Description[cd:country='USA']/cd:country), or are you looking for something that can do XPath processing, or do you mean something else?

(28 Jan '13, 02:21) Jeen Broekstra ♦ Jeen%20Broekstra's gravatar image

jeen Broekstra. iam not trying to extract 'USA' from the xml file. what iam trying to extract is "/rdf:RDF/rdf:Description[cd:country='USA']/cd:country)" if i give "USA". Hope u r now clear with the query. my input will be USA and the output should be "/rdf:RDF/rdf:Description[cd:country='USA']/cd:country)"

(28 Jan '13, 04:31) vipincpy vipincpy's gravatar image

Consider parsing the document and not using XPath. The same data (triples) can be written different ways in RDF/XML so any Xpath solution is not guaranteed to work on all data. Work at the data model level - SPARQL may help.

permanent link

answered 25 Jan '13, 06:01

AndyS's gravatar image

AndyS ♦
accept rate: 33%

Are you sure @vipincpy meant XPath in RDF/XML documents and not XPath on actual RDF graphs?

(25 Jan '13, 06:13) Tomasz Plusk... Tomasz%20Pluskiewicz's gravatar image

Not really, but there is sometimes an expectation that arbitrary RDF/XML can be processed as XML; it need addition syntax constraints to be practical (there was an RDF/XML parser in XSLT ... to show it is possible only). As your first comment recognizes the question is not clear (it overlapped with my answer).

(25 Jan '13, 06:25) AndyS ♦ AndyS's gravatar image

That's unreadable. Please edit the original question and add, well formatted, the example.

(25 Jan '13, 06:38) AndyS ♦ AndyS's gravatar image

I gather from your clarification that you are looking for a way to generate an XPath expression given a string and an RDF/XML document as input, where the expression should match the XML node that contains the given string value.

First of all, this is not really a Semantic Web problem, but more a general XML processing problem (the fact that your XML document contains RDF seems immaterial).

Having said that, what you probably need to do is process your document using something like an XML DOM parser to find the matching node and then reconstruct the XPath expression from there. Alternatively, use an XPath processor to find a match using a very generic XPath expression (e.g. something like //*[text()='USA'], and use the returned node to reconstruct the full path.

In Java, you should be able to do this with any JAXP implementation, using javax.xml.xpath.XPathFactory. Something like this (untested):

javax.xml.xpath.XPathFactory factory = ... ;
javax.xml.xpath.XPathExpression expr 
                  = factory.newXPath().compile("//*[text()='USA']");

and then evaluate on your document to a DOM node to able to reconstruct the path, something like this (untested):

InputStream input = ....; // your XML file
org.w3c.dom.Node node = (Node)expr.evaluate(input, XPathConstants.NODE);

Once you have the DOM node, you should be able to reconstruct the path using getParentNode() etc.

I'm not making any claims for efficiency or full correctness here, but this should get you started. For further help, I suggest you look up some tutorials on how to work with XML, DOM and XPath in Java, for example here is a good starting point.

permanent link

answered 28 Jan '13, 17:47

Jeen%20Broekstra's gravatar image

Jeen Broekstra ♦
accept rate: 37%

Thanks a lot...jean..this should help me...

(29 Jan '13, 00:06) vipincpy vipincpy's gravatar image

Hi there;

Try to read rdf document with a tool.(jena is highly recommended). This link shows how to read RDF data. Then you can listStatements and filter results, or create a sparql query to get data.

Good luck


permanent link

answered 25 Jan '13, 14:43

%C3%87a%C4%9Fda%C5%9F%20Ol's gravatar image

Çağdaş Ol
accept rate: 0%

Cagdas, but how to get the xpath using apache-jena. my intention is not to paarse a rdf file. but to find the Xpath.Thanks in advance

(28 Jan '13, 01:32) vipincpy vipincpy's gravatar image
Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here



Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text]( "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Question tags:


question asked: 25 Jan '13, 04:25

question was seen: 2,271 times

last updated: 29 Jan '13, 06:37