Thanks for your code snippets. If you are using XQuery Update, the best solution is to delete all attributes and reinsert them:

[1]
return (
  delete nodes $w/@*,
  insert nodes ($w/@*, attribute after { $after }) into $p
)

[2]
return (
  delete nodes $p/@*,
  insert nodes ($p/@* except @p, attribute n { $id }) into $p
)

If you know that the attribute already exists, you can simply replace its value:

[2]
  replace value of node $p/@n with $id



On Thu, Feb 10, 2022 at 2:26 PM Jonathan Robie <jonathan.robie@gmail.com> wrote:
Attribute order matters even more when you have bidi and are using typical programming editors.  Here's one problem I am facing - look at the @after attribute, as displayed in some editors:

Sublime doesn't handle bidi, so the element content, which is the word in question, is spelled backwards - but the @after attribute displays correctly precisely because it doesn't handle bidi:

image.png

But editors that typically do support bidi have real problems displaying this properly.  In this example, the content of @after looks like a colon, but it is actually a sof passuq, a Hebrew character, and the character sequences involved display weirdly in many editors.

vi:
image.png

Emacs:
image.png

Adding an attribute that does not contain RTL text solves the display problem in many editors:

image.png

So attribute order matters more than usual in this setting ...

Jonathan

On Thu, Feb 10, 2022 at 6:34 AM Hans-Juergen Rennau <hrennau@yahoo.de> wrote:
[ Just a shout from the cheap seats in the gallery. In my experience, the order of attributes may have considerable esthetic value and may have impact on the acceptance of a program. The order therefore does deserve attention, in spite of its theoretical insignificance. Even more important than the order coming out of a literal constructor may be the order resulting from a dynamic constructor, that is, a sequence of attribute {...} {...}, because the developer can control that order dynamically, and all is well, provided BaseX plays along and retains the order. Yes, reordering is possible with the comma expression, but it would be nice to get away without that extra mile. ]

Am Donnerstag, 10. Februar 2022, 10:34:56 MEZ hat Christian Grün <christian.gruen@gmail.com> Folgendes geschrieben:


Hi Jonathan,


> Is there any way for me to control the order of attributes in BaseX when I insert new attributes?


If you evaluate the following XQuery expression in BaseX …

<a b='b' c='c'/>

… you can be sure that the attributes will be placed in the original
order. Please note that we cannot give a guarantee that this will stay
that way forever, even though it’s unlikely to change.

If you have an existing element, you can reinsert all attributes in
the order you want:

let $a := <a b='b' c='c'/>
return element { node-name($a) } {
  $a/@*, attribute d { 'd' }, $a/node()
}

How do you currently proceed?

Best,
Christian