Getting the wrong order when using ORDER BY in XQuery using Saxon processor on Oxygen XML Editor

334 Views Asked by At

this is my very first post and I'm seeking for some help because I've tried everything I could without any luck.

So, I'm trying to use some simple XQuery to get some data from an XML file (showed below), but I'm having trouble trying to order the results, as they don't appear as expected.

My input file:

<?xml version="1.0" encoding="UTF-8"?>
<database name="MyDB">
    <constraints>
        <constraint name="ASIGNATURAS_MATRICULADAS_CHK1">
            <type>C</type>
            <table-name>ASIGNATURAS_MATRICULADAS</table-name>
            <condition>CONVOCATORIAS_AGOTADAS &gt;= 0 AND CONVOCATORIAS_AGOTADAS &lt; 6</condition>
        </constraint>
        <constraint name="SYS_C0029035">
            <type>C</type>
            <table-name>ASIGNATURAS_MATRICULADAS</table-name>
            <condition>&quot;CONVOCATORIAS_AGOTADAS&quot; IS NOT NULL</condition>
        </constraint>
        <constraint name="SYS_C0029034">
            <type>C</type>
            <table-name>ASIGNATURAS_MATRICULADAS</table-name>
            <condition>&quot;COD_ASIGNATURA&quot; IS NOT NULL</condition>
        </constraint>
        <constraint name="SYS_C0029033">
            <type>C</type>
            <table-name>ASIGNATURAS_MATRICULADAS</table-name>
            <condition>&quot;NIF_ALUMNO&quot; IS NOT NULL</condition>
        </constraint>
        <constraint name="SYS_C0029031">
            <type>C</type>
            <table-name>ASIGNATURAS</table-name>
            <condition>&quot;CODIGO&quot; IS NOT NULL</condition>
        </constraint>
        <constraint name="SYS_C0029027">
            <type>C</type>
            <table-name>ALUMNOS</table-name>
            <condition>&quot;NIF&quot; IS NOT NULL</condition>
        </constraint>
        <constraint name="SYS_C0029029">
            <type>C</type>
            <table-name>ALUMNOS</table-name>
            <condition>&quot;PRIMER_APELLIDO&quot; IS NOT NULL</condition>
        </constraint>
        <constraint name="SYS_C0029028">
            <type>C</type>
            <table-name>ALUMNOS</table-name>
            <condition>&quot;NOMBRE&quot; IS NOT NULL</condition>
        </constraint>
        <constraint name="ASIGNATURAS_MATRICULADAS_FK1">
            <type>R</type>
            <table-name>ASIGNATURAS_MATRICULADAS</table-name>
            <referenced-constraint-name>ALUMNOS_PK</referenced-constraint-name>
        </constraint>
        <constraint name="ASIGNATURAS_MATRICULADAS_FK2">
            <type>R</type>
            <table-name>ASIGNATURAS_MATRICULADAS</table-name>
            <referenced-constraint-name>ASIGNATURAS_PK</referenced-constraint-name>
        </constraint>
        <constraint name="ALUMNOS_PK">
            <type>P</type>
            <table-name>ALUMNOS</table-name>
            <column-name>NIF</column-name>
        </constraint>
        <constraint name="ASIGNATURAS_PK">
            <type>P</type>
            <table-name>ASIGNATURAS</table-name>
            <column-name>CODIGO</column-name>
        </constraint>
    </constraints>
</database>

What I'm trying to do is something quite simple, get all the constraints with type "C" and show the name of them, sorted alphabetically.

This is the XQuery:

<check-constraints>
{
for $r in doc("DB.xml")//constraint[type = 'C']/@name
order by $r
return <constraint>{data($r)}</constraint>
}
</check-constraints>

The result I'm expecting is as follows:

<?xml version="1.0" encoding="UTF-8"?>
<check-constraints>
   <constraint>ASIGNATURAS_MATRICULADAS_CHK1</constraint>
   <constraint>SYS_C0029027</constraint>
   <constraint>SYS_C0029028</constraint>
   <constraint>SYS_C0029029</constraint>
   <constraint>SYS_C0029031</constraint>
   <constraint>SYS_C0029033</constraint>
   <constraint>SYS_C0029034</constraint>
   <constraint>SYS_C0029035</constraint>
</check-constraints>

But instead, I'm getting this result:

<?xml version="1.0" encoding="UTF-8"?>
<check-constraints>
   <constraint>SYS_C0029031</constraint>
   <constraint>ASIGNATURAS_MATRICULADAS_CHK1</constraint>
   <constraint>SYS_C0029027</constraint>
   <constraint>SYS_C0029028</constraint>
   <constraint>SYS_C0029029</constraint>
   <constraint>SYS_C0029035</constraint>
   <constraint>SYS_C0029034</constraint>
   <constraint>SYS_C0029033</constraint>
</check-constraints>

What am I doing wrong? Why is the "order by" not sorting as it should? If I don't put the "order by" I get the result in the same order as listed in the file, something that makes sense because the nodes are entered as they are found.

Edit: As suggested, apparently it could be due to the XQuery processor. In this case I'm working on Oxygen, which uses the Saxon processor as default. I just tried Zorba and it works there. So now I'm more confused, which is the right output?

Solved: My input file contained random length number of BOM characters on some of the values (by error), and they are invisible, so it made the ordering go off. Taking them away makes it work as expected.

1

There are 1 best solutions below

3
On

Your query gives the expected result when run from the command line in Saxon 9.4. It also works correctly for me in oXygen 13.1 (using Saxon 9.3.0.5). So it must be something to do with the way you are running it. Perhaps you've somehow got the query scenario wrong so you are not using the correct files?