=== netXML ===
This Wiki is about functions to help create, edit and query XML variables and files. They are using a combination of Dyalog ⎕XML and .Net. You should already be familiar with ⎕XML, if not you can reach the online help at http://help.dyalog.com/ and search for 'XML Convert'.
=== Element ===
`Element` is used to build a ⎕XML element. The return value is an array with 1 row and 5 columns as per the ⎕XML format. Typical uses are like this:
{{{
⎕XML 'name' Element 'content'
content
⎕XML 'name' ('attribute1' 'value1') Element 'content'
content
⎕XML 'name' (('attribute1' 'value1')('attribute2' 'value2')) Element 'content'
content
⎕XML '⍝' Element 'This is a comment'
⎕XML '?' Element 'xml version="1.0" encoding="UTF-8"'
⎕XML '!' Element 'DOCTYPE root_element SYSTEM "DTD_location"'
book ← '⍝' Element 'This is a good book'
book⍪ ← 'author' Element 'Gambardella, Matthew'
book⍪ ← 'title' Element 'XML Developer''s Guide'
book⍪ ← 'genre' Element 'Computer'
book⍪ ← 'price' Element 44.95
⎕XML book
Gambardella, Matthew
XML Developer's Guide
Computer
44.95
}}}
=== Elements ===
`Elements` will call the function `Element` repetitively on it's arguments to simplify the coding:
{{{
book ← '⍝' 'author' 'title' 'genre' 'price' Elements 'This is a good book' 'Gambardella, Matthew' 'XML Developer''s Guide' 'Computer' 44.95
⎕XML book
Gambardella, Matthew
XML Developer's Guide
Computer
44.95
}}}
=== 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
Gambardella, Matthew
XML Developer's Guide
Computer
44.95
}}}
=== XPath ===
There are no XML query features included with ⎕XML. We need to use .Net for that. First let's generate the following XML:
{{{
⍝ Example taken from: https://msdn.microsoft.com/en-us/library/ms762271%28v=VS.85%29.aspx
books ← 0 5⍴''
books⍪ ← '⍝' Element 'This is a comment'
book ← 'author' 'title' 'genre' Elements 'Gambardella, Matthew' 'XML Developer''s Guide' 'Computer'
book⍪ ← 'price' 'publish_date' Elements 44.95 '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' 'title' 'genre' Elements 'Ralls, Kim' 'Midnight Rain' 'Fantasy'
book⍪ ← 'price' 'publish_date' Elements 5.95 '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' 'title' 'genre' Elements 'Corets, Eva' 'Maeve Ascendant' 'Fantasy'
book⍪ ← 'price' 'publish_date' Elements 5.95 '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' 'title' 'genre' Elements 'Corets, Eva' 'Oberon''s Legacy' 'Fantasy'
book⍪ ← 'price' 'publish_date' Elements 5.95 '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
Gambardella, Matthew
XML Developer's Guide
Computer
44.95
2000-10-01
An in-depth look at creating applications with XML.
Ralls, Kim
Midnight Rain
Fantasy
5.95
2000-12-16
A former architect battles corporate zombies, an evil sorceress, and her own childhood to become queen of the world.
Corets, Eva
Maeve Ascendant
Fantasy
5.95
2000-11-17
After the collapse of a nanotechnology society in England, the young survivors lay the foundation for a new society.
Corets, Eva
Oberon's Legacy
Fantasy
5.95
2001-03-10
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.
}}}
After that we need to obtain a .Net !XmlDocument by using the function `AplToXmlDoc` on the ⎕XML array:
{{{
doc ← AplToXmlDoc catalog
doc
System.Xml.XmlDocument
}}}
Then we can use the function `XPath` with an XPath expression and a .Net !XmlDocument to obtain a collection of .Net Xml nodes. If a node is modified from that collection, the parent !XmlDocument will be modified also accordingly (Examples for XPath can be found at: https://msdn.microsoft.com/en-us/library/ms256086%28v=vs.110%29.aspx). Here are 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]
⍝ To find the attribute of the first book
nodes ← doc XPath'//book[1]'
nodes[0].Attributes[0].(Name Value)
id bk101
⍝ To find the genre of all the books
nodes ← doc XPath'//book/genre'
(⌷nodes).InnerText
Computer Fantasy Fantasy
⍝ To find how many books has a price of $5.95
nodes ← doc XPath'//book/price[text()="5.95"]'
nodes.Count
2
}}}
The !XmlDocument is reverted back to the ⎕XML array by using `XmlDocToApl`:
{{{
catalog ← XmlDocToApl doc
}}}
=== AppendElement ===
To append some XML elements, prepare a ⎕XML array first:
{{{
book ← 'author' 'title' 'genre' Elements 'Corets, Eva' 'The Sundered Grail' 'Fantasy'
book⍪ ← 'price' 'publish_date' Elements 5.95 '2001-09-10'
book⍪ ← 'description' Element 'The two daughters of Maeve, half-sisters, battle one another for control of England. Sequel to Oberon''s Legacy.'
book ← 'book'('id' 'bk105') AddParent book
}}}
Then you can use the function `AppendElement` with a !XmlDocument:
{{{
doc AppendElement book
}}}
Or you can catenate in APL after adjusting the first column of the ⎕XML array for the proper depth level:
{{{
book[;1]+←1
catalog⍪←book
}}}
=== File Operations ===
You can use the functions `AplToFile` and `FileToApl` to Save and Retrieve a ⎕XML array to a file (see comments of each functions for more details).
{{{
catalog AplToFile 'd:\fileName.xml'
1
catalog ← 2⊃FileToApl 'd:\fileName.xml'
}}}
== How to install netXML in your workspace ==
1. Download [[attachment:netXML.v1.1.txt]]
1. Do a Select all (Ctrl+A) and a copy (Ctrl+C).
1. In your workspace execute `)ed ⍟ netXML`
1. Paste (Ctrl+V) the text into the Dyalog editor
1. Press Escape and ')save' your workspace
Optionally to de-script the namespace you can do:
{{{
#.netXML←{('n' ⎕NS ⍵)⊢n←⎕NS ''}#.netXML
}}}
== Version Information ==
{{{
Version 1.0 August 2015, Pierre Gilbert
Version 1.1 August 2015
Method Comment has been replaced by: '⍝' Element 'CommentContent'
Added: '?' Element 'DeclarationContent'
Added: '!' Element 'DeclarationContent'
}}}
||Original author: ||Pierre Gilbert ||
||Responsible: ||PierreGilbert ||
||Email: || <> ||
----
See also:
[[wpfXmlBindingDemo]] will convert an APL nameless namespace to Xml and vice-versa.[[wpfXamlEditor]] for a XML editor.
[[XsltTransform]] to apply a XSLT transform to a XML file. The workspace '''ws\loaddata.dws''' included with Dyalog has the functions `LoadXML` and `SaveXML` that are giving an example of how to use `⎕SE.Parser` that may be useful in some cases.
----
CategoryDyalog - CategoryDyalogDotNet - CategoryDyalogDotNetUtilities - CategoryDotNet