Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error when defaultnamespace #36

Closed
alexandrev opened this issue Feb 15, 2016 · 1 comment
Closed

Error when defaultnamespace #36

alexandrev opened this issue Feb 15, 2016 · 1 comment

Comments

@alexandrev
Copy link

I'm using your library as a base from my xpath-tool, that you can watch here: https://github.com/alexandrev/xpath-tool but I'm facing an issue when you have a default namespace. I'm adding the namespace using the useNamespace function but with no prefix ("") as it has been stored on the nsMap from xmldom when it parses the document. So then when I write the xpath expression I mean to use default namespace but it doesn't seem that is working. A real example:

XML Document

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Project xmlns="http://schemas.microsoft.com/project">
    <SaveVersion>9</SaveVersion>
    <Name>DndTesting</Name>
    <Title>DndTesting</Title>
    <Manager></Manager>
    <ScheduleFromStart>1</ScheduleFromStart>
    <StartDate>2016-01-15T08:00:00</StartDate>
    <FinishDate>2016-10-21T17:00:00</FinishDate>
    <FYStartDate>1</FYStartDate>
    <CriticalSlackLimit>0</CriticalSlackLimit>
....

The namespace I add using usenamespace functions it is hte following JSON:
{"": "http://schemas.microsoft.com/project"}

But after all that set up the xpath expression //Project doesn't return anything!

Please, take a look and if you need more help or info from my side, let me know!

@JLRishe
Copy link
Collaborator

JLRishe commented Feb 25, 2016

Unlike XML, XPath 1.0 does not have a concept of default namespaces:

A QName in the node test is expanded into an expanded-name using the namespace declarations from the expression context. This is the same way expansion is done for element type names in start and end-tags except that the default namespace declared with xmlns is not used: if the QName does not have a prefix, then the namespace URI is null (this is the same way attribute names are expanded). It is an error if the QName has a prefix for which there is no namespace declaration in the expression context.

The behavior you are seeing is expected and it is the same behavior you would see in any XPath 1.0 compliant engine:

http://stackoverflow.com/questions/29412083/getting-all-entry-elements-within-a-namespace-from-xml-using-xpath
http://stackoverflow.com/questions/23203421/javax-xml-xpath-is-not-extracted-from-xml-with-namespaces

I'm not sure what to advise you as far as your tool goes, but even if this library allowed using default namespaces, that would just mean that your tool would be allowing your users to test incorrect XPaths.
Perhaps as a way around this, you could have your tool auto-choose a namespace prefix for default namespaces and provide your user with a list of available prefixes?

I noticed on your README page that your tool is planned to support XPath 2.0. XPath 2.0 does have a concept of default namespaces, but this is not an XPath 2.0 library. Perhaps you might be better served using an XPath 2.0 library? I know that there are a few out there:

https://github.com/ilinsky/xpath.js
https://github.com/ilinsky/jquery-xpath

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants