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

Functions to manage namespace usage #1045

Open
apb2006 opened this issue Feb 26, 2024 · 5 comments
Open

Functions to manage namespace usage #1045

apb2006 opened this issue Feb 26, 2024 · 5 comments
Labels
Enhancement A change or improvement to an existing feature PRG-hard Categorized as "hard" at the Prague f2f, 2024 PRG-required Categorized as "required for 4.0" at the Prague f2f, 2024 XQFO An issue related to Functions and Operators

Comments

@apb2006
Copy link

apb2006 commented Feb 26, 2024

Prior to saving XML generated in XQuery I often tweak the namespace usage.
This makes the XML lighter and clearer for the casual reader and is sometimes mandated by users and systems.
I think providing builtin solutions for these cases would ease these tasks.

Common cases are:

  1. Remove unused prefixes
    Example: the function presented at https://stackoverflow.com/questions/23002655/xquery-how-to-remove-unused-namespace-in-xml-node

  2. Make a namespace the default wherever it is used.
    Example: functx:change-element-ns-deep($nodes,$targetns,"")
    See http://www.xqueryfunctions.com/xq/functx_change-element-ns-deep.html

  3. Remove the use of all/some namespaces
    Example: BaseX https://docs.basex.org/wiki/Utility_Module#util:strip-namespaces

A somewhat related issue #266

@ChristianGruen ChristianGruen added XQFO An issue related to Functions and Operators Enhancement A change or improvement to an existing feature labels Feb 26, 2024
@michaelhkay
Copy link
Contributor

I would suggest a function rename-nodes($node, $map) that copies a subtree, renames the element (and perhaps attribute) nodes according to rules in the map, and then drops unused namespace nodes.

An example of a rule might be map{"Q{http://something/ns}*" : "Q{}*"} which renames all elements in namespace http://something/ns to be in no namespace. The basic idea is that rules can match either the namespace URI or the local name or both (with a precedence system similar to xsl:strip-space) and the target name can retain either the namespace URI or the local name or both or neither.

This is primarily about namespace URIs, not prefixes, on the assumption that prefixes don't actually matter.

@apb2006
Copy link
Author

apb2006 commented Feb 27, 2024

The goal of case 2 above is to replace use of a prefix with an explicit xmlns= where required.
Could this be done like map{"Q{http://docbook.org/ns/docbook}*" : ""}

let $book:=
<d:book xmlns:d="http://docbook.org/ns/docbook">
        <d:info>
             <title xmlns="http://docbook.org/ns/docbook">Another fine book</title>
        </d:info>
</d:book>

return rename-nodes($book, map{"Q{http://docbook.org/ns/docbook}*" : ""})
<book xmlns="http://docbook.org/ns/docbook">
  <info>
    <title>Another fine book</title>
  </info>
</book>

@cedporter
Copy link
Contributor

rename-nodes() definitely sounds useful, but it would not take the place of strip-namespaces() described in the link in #3 in BaseX. That function, when passed an empty string, will remove all namespaces. With rename-nodes(), you'd have to explicitly specify all namespaces in the doc to produce the same behavior.

@ndw
Copy link
Contributor

ndw commented Jun 4, 2024

This is related to #266

@ndw ndw added PRG-hard Categorized as "hard" at the Prague f2f, 2024 PRG-required Categorized as "required for 4.0" at the Prague f2f, 2024 labels Jun 4, 2024
@ndw
Copy link
Contributor

ndw commented Jun 4, 2024

The CG at the F2F meeting in Prague conclude that several users have asked for this, and it isn't straight-forward in XQuery, so we should do something to address it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Enhancement A change or improvement to an existing feature PRG-hard Categorized as "hard" at the Prague f2f, 2024 PRG-required Categorized as "required for 4.0" at the Prague f2f, 2024 XQFO An issue related to Functions and Operators
Projects
None yet
Development

No branches or pull requests

5 participants