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

QName arguments: also allow strings #1661

Open
ChristianGruen opened this issue Dec 18, 2024 · 6 comments
Open

QName arguments: also allow strings #1661

ChristianGruen opened this issue Dec 18, 2024 · 6 comments
Labels
Enhancement A change or improvement to an existing feature XQFO An issue related to Functions and Operators

Comments

@ChristianGruen
Copy link
Contributor

In #747, a syntax for QName literals was proposed (Q"prefix:local"). Concerns were raised that the new syntax could be mixed up with the existing Q{URI}local syntax, and that too many syntax variants are confusing.

In attribute and element constructors, it is already possible to supply prefix:local-name and Q{uri}local strings for names:

element { 'xml:name' } {},
element { 'Q{uri}name' } {}

With option declarations, it is possible to write:

declare option output:cdata-section-elements 'xml';
<xml>text</xml>

…and it is counterintuitive that serialize($xml, { 'cdata-section-elements': 'xml' }) is not legal.

I wonder whether we cannot simply allow both QNames and strings for arguments (and arguments in options) in the existing standard functions. There are fewer cases than I had suspected:

Function Argument
fn:error $code
fn:format-number $options: format-name (can also be xs:NCName)
fn:deep-equal $options: unordered-elements
fn:serialize $options: cdata-section-elements, suppress-indentation
fn:function-lookup $name
fn:load-xquery-module $options: variables, vendor-options
fn:transform $options: initial-function, initial-mode, initial-template, ...
fn:elements-to-maps $options: layouts
fn:schema-type $name

For some options, like the method option of fn:serialize, we already allow both strings and QNames.

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

We could additionally introduce mutual coercion of strings and QNames:

  • xs:stringxs:QName: Accept "local"/"Q{uri}local", raise error for "prefix:local" (unless the prefix is statically known?)
  • xs:QNamexs:string: Rewrite to "Q{uri}local"

@michaelhkay
Copy link
Contributor

Note also in XSLT, fn:system-property, fn:element-available, fn:function-available, fn:key etc traditionally required a lexical QName (as a string) and more recently have allowed an xs:QName.

Despite this, I have some hesitation in increasing the number of places where the static namespace context has to be retained at execution time.

@michaelhkay
Copy link
Contributor

I'm starting to come around to extending the coercion rules so that if the required type is xs:QName, a string that can be converted to a QName by calling fn:parse-QName should be accepted.

I have reservations about doing this in dynamic function calls, however, where it isn't statically known what the required type will be; we don't want to have to retain the static namespace for a dynamic function call just in case.

@ChristianGruen
Copy link
Contributor Author

I have reservations about doing this in dynamic function calls, however, where it isn't statically known what the required type will be; we don't want to have to retain the static namespace for a dynamic function call just in case.

To understand this correctly:

  • Would this mean that xs:QName('xml:a'), xs:QName#1('xml:a') and error('xml:a') could be evaluated, but error#1('xml:a') could not?
  • Aren’t components of the static context already preserved in other existing dynamic function calls, such as resolve-uri#1('a')?

@michaelhkay
Copy link
Contributor

Aren’t components of the static context already preserved in other existing dynamic function calls

I think that in general, we know statically which parts of the static context are needed by an expression, and we only need to retain those parts when compiling the expression. Retaining the static namespace context for every dynamic function call, just in case it turns out to have QNames in the function signature, would be very expensive. It's possible to make economies by noting that the static context of an expression is in many cases the same as its parent expression, but that kind of thing makes expression rewrites such as loop-lifting more complex.

@ChristianGruen
Copy link
Contributor Author

Retaining the static namespace context for every dynamic function call, just in case it turns out to have QNames in the function signature, would be very expensive.

I could imagine that the incurred costs are implementation-specific, but maybe I should first implement it before judging it (if I remember correctly, we simply keep a reference to an existing static context in dynamic functions).

In any case, most function calls are certainly static, so it would already be a good improvement to support those.

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 XQFO An issue related to Functions and Operators
Projects
None yet
Development

No branches or pull requests

2 participants