1

Users are inputting quite complex XPaths to query the database. They will only use one fixed namespace for the queries. Currently users have to enter expressions like:

//ns:tag1/ns:tag2 | //ns:tag3/ns:*[not(ns:tag2)]

But this syntax gets overly complicated very quickly, and takes longer to enter. Also it is quite error prone, for users who aren't overly familiar with xpath. Users would ideally input the XPath without namespaces --

//tag1/tag2 | //tag3/*[not(tag2)]

So much easier for them! But not for me. How can I deal with this kind of expression? I do know the namespace that needs to be inserted. Is there any way to automatically insert the ns in the appropriate places in the Xpath expression? I am using Python lxml. Or can I set a default namespace?

Note that *[local-name() = 'entry'] is not possible here!

edit

In python I am calling

currentNode.xpath(query, {'ns':'http://myaddress.com/userns'})

where currentNode is an etree.Element.

2
  • 1
    To what function in lxml are you passing the users' queries? Commented Aug 21, 2014 at 5:05
  • good point, I am sending them to currentNode.xpath(query, {'ns':'http://myaddress.com/userns'}) Commented Aug 21, 2014 at 5:23

1 Answer 1

1

XPath 2.0 solves this by allowing you to specify a default namespace which is applied to all unprefixed element names in the query. Unfortunately, XPath 2.0 isn't readily available to Python users.

An alternative is to transform the document before your users query it, so that it doesn't use any namespaces.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.