Differences between revisions 1 and 2
Revision 1 as of 2015-08-20 01:18:04
Size: 6589
Comment:
Revision 2 as of 2015-08-20 12:09:06
Size: 8567
Comment:
Deletions are marked like this. Additions are marked like this.
Line 13: Line 13:
The return value is an array with 1 row and 4 columns as per the ⎕XML format. Here is a typical example: The return value is an array with 1 row and 5 columns as per the ⎕XML format. Here is a typical example:
Line 57: Line 57:
 books ← 0 4⍴''  books ← 0 5⍴''
Line 64: Line 64:
 book⍪ ← 'price' Element' 44.95'  book⍪ ← 'price' Element '44.95'
Line 137: Line 137:
To be able to use `XPath` we need a .Net !XmlDocument by doing: To be able to use `XPath` we need a .Net !XmlDocument by using the function `AplToXmlDoc` on the ⎕XML array:
Line 143: Line 143:
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: Then we can use the function `XPath` with an XPath expression 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 is some examples:
Line 157: Line 157:
Note: You can look at the workspace ws\loaddata.dws included with Dyalog for the functions `LoadXML` and `SaveXML` that are giving an example of how to use `⎕SE.Parser` that may be useful in some cases.
=== AppendElement ===
To append some XML elements, prepare a ⎕XML array first:
{{{
 book ← 'author' Element 'Corets, Eva'
 book⍪ ← 'title' Element 'The Sundered Grail'
 book⍪ ← 'genre' Element 'Fantasy'
 book⍪ ← 'price' Element '5.95'
 book⍪ ← 'publish_date' Element '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:
{{{
      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.
{{{
      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

}}}
||Original author: ||Pierre Gilbert ||
||Responsible: ||PierreGilbert ||
||Email: || <<MailTo(apgil AT SPAMFREE videotron DOT ca)>> ||

----
CategoryDyalog - CategoryDyalogDotNet - CategoryDyalogDotNetUtilities - CategoryDotNet

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 5 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 5⍴''

 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 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 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 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

Note: You can look at the workspace ws\loaddata.dws included with Dyalog for the functions LoadXML and SaveXML that are giving an example of how to use ⎕SE.Parser that may be useful in some cases.

AppendElement

To append some XML elements, prepare a ⎕XML array first:

 book  ← 'author' Element 'Corets, Eva'
 book⍪ ← 'title'  Element 'The Sundered Grail'
 book⍪ ← 'genre'  Element 'Fantasy'
 book⍪ ← 'price'  Element '5.95'
 book⍪ ← 'publish_date' Element '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:

      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.

      catalog AplToFile 'd:\filename.xml'
1
      catalog ← 2⊃FileToApl 'd:\filename.xml'

How to install netXML in your workspace

  1. Download netXML.v1.1.txt

  2. Do a Select all (Ctrl+A) and a copy (Ctrl+C).
  3. In your workspace execute )ed ⍟ netXML

  4. Paste (Ctrl+V) the text into the Dyalog editor
  5. 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

Original author:

Pierre Gilbert

Responsible:

PierreGilbert

Email:

<apgil AT SPAMFREE videotron DOT ca>


CategoryDyalog - CategoryDyalogDotNet - CategoryDyalogDotNetUtilities - CategoryDotNet

netXML (last edited 2015-08-29 22:12:29 by PierreGilbert)