xml.k 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. \d xml
  2. MX:0N-1
  3. ev:#'/|1(2*-2!#:)'\
  4. fm:{(v@g)@&~0<':0&\+\(,/(#'x)#'1 -1)@g:<v:,/x}
  5. fr:{m:2!fd:{y'x}.'x@/:i:+&~=#x
  6. $[&/,//^k:m?\:0;:0N;]
  7. j:*&=/1&/\MX^x[i[;0]]@'k
  8. MX^1 0+x[i[j;1]]@0 1+(fd@j)@k@j}
  9. cl3:{r:fr@(,-1,fm@x[!2]),2_x;$[0N~r;x;x@'(&~</r>\:)'x]}
  10. cl2:{{r:fr@x;$[0N~r;x;x@'(&~</r>\:)'x]}/x}
  11. WS:" \t"
  12. tr:{&/(~`cmt=;(|/^" \n\t"?" ",)')@'+x}
  13. /tag parse
  14. tp:{l:@/1(&0<#:')\1_'(0,@/1(&2!{x'y}[-1+*-/x[2 0]]@)\&|/WS=\:s)_s:-1_x[1]
  15. (tp;nm;loc;len;attrs):(`tag;l[0];x[0];#x[1];{(x;-1_2_y)}@/'(0,/:(1_l)?\:"=")_'1_l)
  16. +(`tp`nm`loc`len`attrs;(tp;nm;loc;len;attrs))}
  17. pt:{c:0,((#c)#0 1)+c:@/1<:\,//x[3 4]-x[0]
  18. (tp;loc;len;cnt):(`txt;x[0]
  19. #x[1]
  20. tr#+(`txt`cmt`cdata@(0,,/((#*)'x[3 4])#'1 2,\:0)@<c
  21. c_x[1]))
  22. +(`tp`loc`len`cnt;(tp;loc;len;cnt))}
  23. ps:{!/+((pt;tp)@>/"<!"=2#x[1])x}
  24. cc:{cl:2+cl@'&'"-]"='x@cl:y[1;4 3]-2
  25. op:op@/:&'("--";"[CDATA["){x~(#x)#y}/:\:x@(op:y[0;0])+\:2+!7
  26. cl2@ev@fm'+(op;cl)}
  27. cl0:{[cms;cds;is]@/1(&2!(+/1(#/|(,0 1),#:)\@/1<:\cms,cds)')\is}
  28. cl:{[ds;qs;cms;cds;sp];(ds;qs):0 2_cl3/,/cl0[cms;cds]@''(ds;qs)
  29. (ds@'&'^(,/'sp)?'ds;,/qs;cl0[cms;cds]''sp)}
  30. lvl0:{(+\(,/(#'x)#'2 -1 0 -2)@g)-(,/(#'x)#'2 0 0 0)@g:<,/x}
  31. nds0:{[xml;sep;dd],/'(nds;(.'dl)@'&'(!'dl:(@/1(=c')\)'(sep))=)@\:/:!#nds:+(c;(c:@/1<:\dd)_xml)}
  32. p1:{ds:&'"<>"=\:x
  33. qs:&'"'\""=\:x;
  34. sp:-1 1+'sp@'/:&''~^ds?'(sp:&'"!?/]-"=\:x)+\:/:-1 1
  35. (ds;qs;sp)}
  36. p2:{(ds;qs;sp):p1[x];:(ds;qs;sp),cc[x;sp]}
  37. parse:{(ds;qs;sp;cms;cds):p2[x]
  38. (ds;qs;sp):cl[ds;qs;cms;cds;sp]
  39. (lvl0[ds,sp[;2]];ps'nds0[x;(qs;cms;cds);,//0 1+/:(ds;sp[;2])])}
  40. \d .