Addons/xml/xslt

From J Wiki
Jump to navigation Jump to search

xml/xslt  - XSL Transform tool

XSL Transformation of a text XML with a text XSLT. Useful for obtaining a flat text for further processing with J.

Windows version requires MSXSL v4+. UNIX requires libxml2/libxslt.

The idea is to avoid reinventing the wheel trying to make XML parser in J, but use world's best XML tools for what it does best and leave flat structures to the world's best rectangular data tool.

Example

Given an XSLT

xsltest=: 0 : 0
<x:stylesheet xmlns:x="http://www.w3.org/1999/XSL/Transform" version="1.0">
        <x:output method="text"/>
    <x:template match="Record">
        <x:for-each select="*">
            <x:value-of select="." /><x:text>|</x:text>
        </x:for-each>
        <x:text>
</x:text>
    </x:template>
    <x:template match="text()" />
</x:stylesheet>
)

and XML input

xmltest=: 0 :0
<THETABLE>
<Record>
  <Item0>Blablabla</Item0>
  <Item1>19990920</Item1>
  <Item2>0</Item2>
  <Item3>0</Item3>
  <Item4>-1.1</Item4>
</Record>
<Record>
  <Item0>QWERTY</Item0>
  <Item1>19990920</Item1>
  <Item2>0</Item2>
  <Item3>0</Item3>
  <Item4>-1.1</Item4>
</Record>
</THETABLE>
)

we apply xslt verb to obtain the results

require 'xml/xslt'

   xsltest xslt xmltest
Blablabla|19990920|0|0|-1.1|
QWERTY|19990920|0|0|-1.1|

   <;._2;._2 toJ xsltest xslt xmltest
+---------+--------+-+-+----+
|Blablabla|19990920|0|0|-1.1|
+---------+--------+-+-+----+
|QWERTY   |19990920|0|0|-1.1|
+---------+--------+-+-+----+


Contributed by Oleg Kobchenko