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

add CompileWithNS #82

Merged
merged 1 commit into from
Feb 15, 2023
Merged

add CompileWithNS #82

merged 1 commit into from
Feb 15, 2023

Conversation

tingstad
Copy link
Contributor

@tingstad tingstad commented Feb 3, 2023

Related to discussion in antchfx/xmlquery#15

This pull-request proposes a function;

func CompileWithNS(expr string, namespaces map[string]string) (*Expr, error)

as an alternative to

func Compile(expr string) (*Expr, error)

This allows for more readable xpath expressions without relying on prefix values. For example:

<tags>
    <a:tag xmlns:a="example.com/tag" />
    <b:tag xmlns:b="example.com/tag" />
</tags>

Using Compile("//a:tag") is not desirable because a: is not guaranteed to not change, and does not match b:. Namespaces in XML says:

Note that the prefix functions only as a placeholder for a namespace name. Applications SHOULD use the namespace name, not the prefix, in constructing names whose scope extends beyond the containing document.

A current workaround is Compile("//*[local-name()='tag' and namespace-uri()='example.com/tag']") which quickly gets messy.

I propose CompileWithNS("//e:tag", map[string]string{"e": "example.com/tag"}) which scales better since namespace bindings can be reused.

@tingstad
Copy link
Contributor Author

@zhengchun What are the chances of this PR, or something similar, making its way to master/release? And, if so, in what time frame? Thanks.

@zhengchun zhengchun merged commit adca7e3 into antchfx:master Feb 15, 2023
@zhengchun
Copy link
Contributor

Sorry for late, merged, thanks.

A few days ago I was thinking if there was a way to return the all namespaceURL with it's prefix name on xmlquery package, let user to write a prefix xpath without namespace map.

@tingstad tingstad deleted the namespaces branch February 15, 2023 10:26
@tingstad
Copy link
Contributor Author

Awesome, @zhengchun! Do you know when the next release will be out?

@zhengchun
Copy link
Contributor

zhengchun commented Feb 15, 2023 via email

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

Successfully merging this pull request may close these issues.

2 participants