UNDER CONSTRUCTION

netXML

This Wiki is about functions to help create, edit and modify XML variables and files. They are using a combination of Dyalog ⎕XML and .Net.

Element

Element is used to build a ⎕XML element. Typical use are like this:

      'name' Element 'content'
      'name' ('attribute1' 'value1') Element 'content'
      'name' (('attribute1' 'value1')('attribute2' 'value2')) Element 'content'

The return value is an array with 1 row and 4 columns as per the ⎕XML format. Here is a typical example:

 book  ← 'author' Element 'Gambardella, Matthew'
 book⍪ ← 'title'  Element 'XML Developer''s Guide'
 book⍪ ← 'genre'  Element 'Computer'
 book⍪ ← 'price'  Element '44.95'

      ⎕XML book
<author>Gambardella, Matthew</author>                                                                                  
<title>XML Developer's Guide</title>                                                                                   
<genre>Computer</genre>                                                                                                
<price>44.95</price>

Comment

Comment is used to build a comment as per the ⎕XML format.

      book ← (Comment 'This is a good book') ⍪ book

      ⎕XML book
<!-- This is a good book -->
<author>Gambardella, Matthew</author>
<title>XML Developer's Guide</title>
<genre>Computer</genre>
<price>44.95</price>

AddParent

AddParent is used to encapsulate a children with its parent. The left argument is the parent's name with optionally attributes and the right argument is a ⎕XML array. Here is a typical example:

      book ← 'book'('id' 'bk101') AddParent book

      ⎕XML book
<book id="bk101">
  <!-- This is a good book -->
  <author>Gambardella, Matthew</author>
  <title>XML Developer's Guide</title>
  <genre>Computer</genre>
  <price>44.95</price>
</book> 

XPath

There are no XML query or validation features included with ⎕XML. The function XPath is using .Net and a XmlDocument to query the XML. First if we generate the following XML:

⍝ Example taken from: https://msdn.microsoft.com/en-us/library/ms762271%28v=VS.85%29.aspx

 books ← 0 4⍴''

 books⍪ ← Comment 'This is a comment'

 book  ← 'author' Element 'Gambardella, Matthew'
 book⍪ ← 'title'  Element 'XML Developer''s Guide'
 book⍪ ← 'genre'  Element 'Computer'
 book⍪ ← 'price'  Element' 44.95'
 book⍪ ← 'publish_date' Element '2000-10-01'
 book⍪ ← 'description'  Element 'An in-depth look at creating applications with XML.'
 book  ← 'book'('id' 'bk101') AddParent book
 books⍪ ← book

 book  ← 'author' Element 'Ralls, Kim'
 book⍪ ← 'title'  Element' Midnight Rain'
 book⍪ ← 'genre'  Element 'Fantasy'
 book⍪ ← 'price'  Element '5.95'
 book⍪ ← 'publish_date' Element '2000-12-16'
 book⍪ ← 'description'  Element 'A former architect battles corporate zombies, an evil sorceress, and her own childhood to become queen of the world.'
 book  ← 'book'('id' 'bk102') AddParent book
 books⍪ ← book

 book  ← 'author' Element 'Corets, Eva'
 book⍪ ← 'title'  Element 'Maeve Ascendant'
 book⍪ ← 'genre'  Element 'Fantasy'
 book⍪ ← 'price'  Element '5.95'
 book⍪ ← 'publish_date' Element '2000-11-17'
 book⍪ ← 'description'  Element 'After the collapse of a nanotechnology society in England, the young survivors lay the foundation for a new society.'
 book  ← 'book'('id' 'bk103') AddParent book
 books⍪ ← book

 book  ← 'author' Element 'Corets, Eva'
 book⍪ ← 'title'  Element 'Oberon''s Legacy'
 book⍪ ← 'genre'  Element 'Fantasy'
 book⍪ ← 'price'  Element '5.95'
 book⍪ ← 'publish_date' Element '2001-03-10'
 book⍪ ← 'description'  Element 'In post-apocalypse England, the mysterious agent known only as Oberon helps to create a new life for the inhabitants of London. Sequel to Maeve Ascendant.'
 book  ← 'book'('id' 'bk104') AddParent book
 books⍪ ← book

 catalog ← 'catalog' AddParent books

      ⎕XML catalog
<catalog>
  <!-- This is a comment -->
  <book id="bk101">
    <author>Gambardella, Matthew</author>
    <title>XML Developer's Guide</title>
    <genre>Computer</genre>
    <price>44.95</price>
    <publish_date>2000-10-01</publish_date>
    <description>An in-depth look at creating applications with XML.</description>
  </book>
  <book id="bk102">
    <author>Ralls, Kim</author>
    <title>Midnight Rain</title>
    <genre>Fantasy</genre>
    <price>5.95</price>
    <publish_date>2000-12-16</publish_date>
    <description>A former architect battles corporate zombies, an evil sorceress, and her own childhood to become queen of the world.</description>
  </book>
  <book id="bk103">
    <author>Corets, Eva</author>
    <title>Maeve Ascendant</title>
    <genre>Fantasy</genre>
    <price>5.95</price>
    <publish_date>2000-11-17</publish_date>
    <description>After the collapse of a nanotechnology society in England, the young survivors lay the foundation for a new society.</description>
  </book>
  <book id="bk104">
    <author>Corets, Eva</author>
    <title>Oberon's Legacy</title>
    <genre>Fantasy</genre>
    <price>5.95</price>
    <publish_date>2001-03-10</publish_date>
    <description>In post-apocalypse England, the mysterious agent known
    only as Oberon helps to create a new life for the inhabitants of London. Sequel to Maeve Ascendant.</description>
  </book>
</catalog>

To be able to use XPath we need a .Net XmlDocument by doing:

      doc ← AplToXmlDoc catalog
      doc
System.Xml.XmlDocument 

Then we can use the function XPath with an XPath expression to obtain a collection of .Net nodes. If a node is modified from that collection, the parent XmlDocument will be modified also accordingly (Example for XPath can be found at: https://msdn.microsoft.com/en-us/library/ms256086%28v=vs.110%29.aspx). Here is some examples:

⍝ To Modify the price of the book that has the title "XML Developer's Guide" to 54.95
  node ← doc XPath '//book[title="XML Developer''s Guide"]/price'
  node[0].InnerText ← '54.95'

⍝ To Remove the node with the title "Midnight Rain"
  node ← doc XPath '//book[title="Midnight Rain"]'
  {}doc.DocumentElement.RemoveChild node[0]

The XmlDocument is reverted back to the ⎕XML format by doing:

      catalog ← XmlDocToApl doc