Sorting elements by the value of @n

Oxygen general issues.
m_prm
Posts: 3
Joined: Tue Oct 19, 2021 5:53 pm

Sorting elements by the value of @n

Post by m_prm »

We are trying to develop an action in Java to sort elements by the value of their attribute n. Is there a simple way to do this?
The xml structure is as follows. Here the <msItemStruct> elements have a n attribute, but they are not sorted correctly. We would like to find a simple way to sort the elements according to the value of n.

Code: Select all

<msItemStruct xml:id="handschriftID-msItemStruct-medieval">
    <msItemStruct n="2">
        <title xml:lang="lat">Decretum</title>
    </msItemStruct>
    <msItemStruct n="3">
        <title xml:lang="lat">Brief Isidors von Sevilla an Bischof Massona von Mérida</title>
    </msItemStruct>
    <msItemStruct n="1">
        <title xml:lang="lat">Ordo de celebrando concilio</title>
    </msItemStruct>
</msItemStruct>
Thanks in advance.
Michela
Radu
Posts: 9059
Joined: Fri Jul 09, 2004 5:18 pm

Re: Sorting elements by the value of @n

Post by Radu »

Hi Michela,

If you want to avoid using Java code:

If you are editing the XML document in the Author visual editing mode, you can customize in the Oxygen Preferences->"Document Type Association" page the framework you are using to edit the XML content, add a custom Author action based on an XSLT operation:
https://www.oxygenxml.com/doc/ug-editor ... yoperation

And the XSLT operation could invoke a script like this:

Code: Select all

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
	xmlns:xs="http://www.w3.org/2001/XMLSchema"
	exclude-result-prefixes="xs"
	version="2.0">
	<xsl:template match="node() | @*">
		<xsl:copy>
			<xsl:apply-templates select="node() | @*"/>
		</xsl:copy>
	</xsl:template>
	<xsl:template match="/*:msItemStruct">
		<xsl:copy>
			<xsl:apply-templates select="@*"/>
			<xsl:for-each select="*:msItemStruct">
				<xsl:sort select="xs:integer(@n)"/>
				<xsl:apply-templates select="."/>
			</xsl:for-each>
		</xsl:copy>
	</xsl:template>
</xsl:stylesheet>
.

If you are editing the XML content in the Text editing mode or want to fix all places where the problem might appear in the XML content you could create a custom XML refactoring script based on XSLT to fix all such places in the entire XML document:

https://www.oxygenxml.com/doc/ug-editor ... tools.html

Regards,
Radu
Radu Coravu
<oXygen/> XML Editor
http://www.oxygenxml.com
Post Reply