Addons/xml/sax/x2j Examples

From J Wiki
Jump to navigation Jump to search

xml/sax - x2j (XML 2 J) Examples


x2j1.ijs General declarations

Declarative XML to J transformer.
Convert XML document to J structures.

require 'xml/sax/x2j'

x2jclass 'px2j1'

'/' x2jElm (3 : 0)    NB. document root (start/end Document)
  root
:
  root=: 0 0$0        NB. a table
)

'row' x2jElm (3 : 0)
  root=: root,row     NB. append to table
:
  row=: ''            NB. a list
)

'row/item' x2jElm (3 : 0)
                      NB. append boxed pair to list
  row=: row,<('name';name),:'value';value
:
  name=: ''
  value=: ''          NB. default to empty
)

'row/item/name'  x2jChar (3 : 'name=: y')
'row/item/value' x2jChar (3 : 'value=: y')

NB. =========================================================
cocurrent 'base'

TESTX2J1=: 0 : 0
<table>
  <row>
    <item><name>Item 1.1</name><value>12.3</value></item>
    <item><name>Item 1.2</name><value>45.6</value></item>
  </row>
  <row>
    <item><name>Item 2.1</name><value>78.9</value></item>
    <item><name>Item 2.2</name><value>67.8</value></item>
  </row>
</table>
)

0 : 0  NB. Test
process_px2j1_ TESTX2J1
)


   process_px2j1_ TESTX2J1
+----------------+----------------+
|+-----+--------+|+-----+--------+|
||name |Item 1.1|||name |Item 1.2||
|+-----+--------+|+-----+--------+|
||value|12.3    |||value|45.6    ||
|+-----+--------+|+-----+--------+|
+----------------+----------------+
|+-----+--------+|+-----+--------+|
||name |Item 2.1|||name |Item 2.2||
|+-----+--------+|+-----+--------+|
||value|78.9    |||value|67.8    ||
|+-----+--------+|+-----+--------+|
+----------------+----------------+

x2j2.ijs Compact declarations

Declarative XML to J transformer.
Using compact definition.

require 'xml/sax/x2j'

x2jclass 'px2j2'

'Items' x2jDefn
  /              := root              : root=: 0 0$0
  row            := root=: root,row   : row=: ''
  row/item       := row=: row,<('name';name),:'value';value : name=: value=: ''
  row/item/name  := name=: y
  row/item/value := value=: y
)

NB. =========================================================
cocurrent 'base'

TESTX2J2=: 0 : 0
<table>
  <row>
    <item><name>Item 1.1</name><value>12.3</value></item>
    <item><name>Item 1.2</name><value>45.6</value></item>
  </row>
  <row>
    <item><name>Item 2.1</name><value>78.9</value></item>
    <item><name>Item 2.2</name><value>67.8</value></item>
  </row>
</table>
)

0 : 0  NB. Test
process_px2j2_ TESTX2J2
)


   process_px2j2_ TESTX2J2
+----------------+----------------+
|+-----+--------+|+-----+--------+|
||name |Item 1.1|||name |Item 1.2||
|+-----+--------+|+-----+--------+|
||value|12.3    |||value|45.6    ||
|+-----+--------+|+-----+--------+|
+----------------+----------------+
|+-----+--------+|+-----+--------+|
||name |Item 2.1|||name |Item 2.2||
|+-----+--------+|+-----+--------+|
||value|78.9    |||value|67.8    ||
|+-----+--------+|+-----+--------+|
+----------------+----------------+

x2j3.ijs Simple table

Declarative XML to J transformer.
HTML table using compact definition.

require 'xml/sax/x2j'

x2jclass 'px2j3'

'Items' x2jDefn
  /   :=  table             : table=: 0 0$0
  tr  :=  table=: table,row : row=: ''
  td  :=  row=: row,<y
)

NB. =========================================================
cocurrent 'base'

TESTX2J3=: 0 : 0
  <table>
    <tr>
      <td>0 0</td><td>0 1</td><td>0 2</td><td>0 3</td>
    </tr><tr>
      <td>1 0</td><td>1 1</td><td>1 2</td><td>1 3</td>
    </tr><tr>
      <td>2 0</td><td>2 1</td><td>2 2</td><td>2 3</td>
    </tr>
  </table>
)

0 : 0  NB. Test
process_px2j3_ TESTX2J3
)


   process_px2j3_ TESTX2J3
+---+---+---+---+
|0 0|0 1|0 2|0 3|
+---+---+---+---+
|1 0|1 1|1 2|1 3|
+---+---+---+---+
|2 0|2 1|2 2|2 3|
+---+---+---+---+

x2j4.ijs Mixed elements and attributes

Declarative XML to J transformer.
Elements and attributes using compact definition.

require 'xml/sax/x2j'

x2jclass 'px2j4'

'Items' x2jDefn
  /              :=  table                   : table=: ,:'href';'title';'add';'mod';'visit'
  bookmark       :=  table=: table,h;t;a;m;v : 'h a m v'=: atr'href added modified visited'
  bookmark/title :=  t=: y
)

NB. =========================================================
cocurrent 'base'

TESTX2J4=: 0 : 0
<xbel>
  <title>Bookmarks</title>
  <desc>Bookmarks</desc>
  <folder id="rdf:#$FvPhC3" folded="no">
    <bookmark href="http://www.mozilla.com/products/firefox/central.html">
      <title>Getting Started</title>
    </bookmark>
    <bookmark href="http://fxfeeds.mozilla.com/" modified="1209052290">
      <title>Latest Headlines</title>
    </bookmark>
  </folder>
    <bookmark href="http://www.jsoftware.com/forumsearch" added="1159366136" visited="1207757057">
      <title>Forum Search</title>
    </bookmark>
    <bookmark href="http://www.ubuntu.com/" added="1208996707" visited="1209047897">
      <title>Ubuntu Home Page | Ubuntu</title>
    </bookmark>
</xbel>
)

0 : 0  NB. Test
process_px2j4_ TESTX2J4
)


   process_px2j4_ TESTX2J4
+-------------------------------+----------------+----------+----------+----------+
|href                           |title           |add       |mod       |visit     |
+-------------------------------+----------------+----------+----------+----------+
|http://www.mozilla.com/produ...|Getting Started |_1        |_1        |_1        |
+-------------------------------+----------------+----------+----------+----------+
|http://fxfeeds.mozilla.com/    |Latest Headlines|_1        |1209052290|_1        |
+-------------------------------+----------------+----------+----------+----------+
|http://www.jsoftware.com/for...|Forum Search    |1159366136|_1        |1207757057|
+-------------------------------+----------------+----------+----------+----------+
|http://www.ubuntu.com/         |Ubuntu Home Page|1208996707|_1        |1209047897|
+-------------------------------+----------------+----------+----------+----------+