I have a problem right now that is the result of current restrictions on a server that our team does not control.
We have work to do using the database, but we are forced to use an XML file and parse it using Javascript / jQuery. We don’t even have write access for our scripts (only through our FTP account) ... we don’t like talking about it, but this is what we got.
The problem with these restrictions is that we need to parse a large XML file of about 500 kb in size with 1700-ary records of document name / document number / url.
The number is rather complicated, for example, "31-2b-1029E" mixed with a material of the type "T2315342".
So, I realized that I need to use something called "Natural Sort" (thanks stackoverflow).
Anyway, I tried using this script here:
function naturalSort (a, b) {
var re = /(^-?[0-9]+(\.?[0-9]*)[df]?e?[0-9]?$|^0x[0-9a-f]+$|[0-9]+)/gi,
sre = /(^[ ]*|[ ]*$)/g,
dre = /(^([\w ]+,?[\w ]+)?[\w ]+,?[\w ]+\d+:\d+(:\d+)?[\w ]?|^\d{1,4}[\/\-]\d{1,4}[\/\-]\d{1,4}|^\w+, \w+ \d+, \d{4})/,
hre = /^0x[0-9a-f]+$/i,
ore = /^0/,
x = a.toString().replace(sre, '') || '',
y = b.toString().replace(sre, '') || '',
xN = x.replace(re, '\0$1\0').replace(/\0$/,'').replace(/^\0/,'').split('\0'),
yN = y.replace(re, '\0$1\0').replace(/\0$/,'').replace(/^\0/,'').split('\0'),
xD = parseInt(x.match(hre)) || (xN.length != 1 && x.match(dre) && Date.parse(x)),
yD = parseInt(y.match(hre)) || xD && y.match(dre) && Date.parse(y) || null;
if (yD)
if ( xD < yD ) return -1;
else if ( xD > yD ) return 1;
for(var cLoc=0, numS=Math.max(xN.length, yN.length); cLoc < numS; cLoc++) {
oFxNcL = !(xN[cLoc] || '').match(ore) && parseFloat(xN[cLoc]) || xN[cLoc] || 0;
oFyNcL = !(yN[cLoc] || '').match(ore) && parseFloat(yN[cLoc]) || yN[cLoc] || 0;
if (isNaN(oFxNcL) !== isNaN(oFyNcL)) return (isNaN(oFxNcL)) ? 1 : -1;
else if (typeof oFxNcL !== typeof oFyNcL) {
oFxNcL += '';
oFyNcL += '';
}
if (oFxNcL < oFyNcL) return -1;
if (oFxNcL > oFyNcL) return 1;
}
return 0;
}
And applied using:
var sortedSet = $(data).children("documents").children("document").sort(function(a, b) {
return naturalSort($(a).children('index').text(), $(b).children('index').text());
});
This works fine in our other, smaller XML file, but for the giant 500kb-ish file Safari (v4) just hangs for a few minutes to sort it, and Firefox (the last one) takes about 10 seconds to process (still not good, but at least reasonable).
I also found this other smaller / lighter script called Alphanum :
function alphanum(a, b) {
function chunkify(t) {
var tz = [], x = 0, y = -1, n = 0, i, j;
while (i = (j = t.charAt(x++)).charCodeAt(0)) {
var m = (i == 46 || (i >=48 && i <= 57));
if (m !== n) {
tz[++y] = "";
n = m;
}
tz[y] += j;
}
return tz;
}
var aa = chunkify(a);
var bb = chunkify(b);
for (x = 0; aa[x] && bb[x]; x++) {
if (aa[x] !== bb[x]) {
var c = Number(aa[x]), d = Number(bb[x]);
if (c == aa[x] && d == bb[x]) {
return c - d;
} else return (aa[x] > bb[x]) ? 1 : -1;
}
}
return aa.length - bb.length;
}
This speeds up for Safari, but still blocks the browser for a minute or so.
, , XSL XML, , -, - , JavaScript.
-, : Sarissa, , , 2011-06-22.
, xslt.js
:
- XSL ?
- , XSL ? (URL- ?)
- , ?
- XSL , ?
, .