1

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.

4

1 回答 1

0

从 Saxon 9.4 中的命令行运行时,您的查询给出了预期的结果。它在 oXygen 13.1(使用 Saxon 9.3.0.5)中也能正常工作。因此,它必须与您运行它的方式有关。也许您以某种方式弄错了查询场景,因此您没有使用正确的文件?

于 2012-06-04T17:29:45.117 回答