Wikis

XMLOrientedProgramming

Instead of relying on functions as application building blocks as many other languages do, X# replaces those functions with four basic node operations (append, remove, update and execute) on an XML document that represents the actual information. Let’s say we want to create a multi-threaded application that shuttles data to and from SQL databases, email repositories, and Web services:

To insert a record into an SQL database, an XML node representing the table in the database is instantiated and another XML node representing the record is appended to it to perform the insertion.

<xsp:append-child target="document('xdbc:mysql://192.168.1.27:3306/ncc1701')/crew">
  <row>
    <id>100</id>
    <name>James T. Kirk</name>
    <occupation>Captain</occupation>
    <age>30</age>
    <email>jkirk@ncc1701.org</email>
  </row>
</xsp:append-child>

To send an e-mail, an XML node representing the SMTP server is instantiated and another XML node representing the e-mail is appended to it to send the e-mail.

<xsp:append-child target="document('smtp://smtp.gmail.com')">
  <mail importance="high" subject="Test message"
    from="spock@vulcan.org" to="jkirk@ncc1701.org">
  Hi James,

    This is my first X# plain text message.

  -- Spock
  </mail>
</xsp:append-child>

To create a file in a directory, an XML node representing the target directory is instantiated and another XML node representing the file is appended to it to create the file.

<xsp:append-child target="document('file:///home/jkirk/documents')">
  <file name="proposal.txt">
    This is the content of the text file...
  </file>
</xsp:append-child>

Other operations just as intuitive:

To delete a record from an SQL database, the XML node representing the record is selected and then deleted. Or, to change the value of a record column, the XML attribute representing the column is changed and then updated.

<!-- Create a node representing our DB -->
<xsp:variable name="sql" type="node" select="document('xdbc:mysql://192.168.1.27:3306/ncc1701')"/>
<!-- Delete all crew members with a name ending in 'Borg' -->
<xsp:remove-node select="$sql/crew/*[ends-with(@name, 'Borg')]"/>
<!-- Update the name of a record stored in our DB -->
<xsp:set-attribute target="$sql/crew/*[@id = '100']" name="seen" select="'James Tiberius Kirk'"/>

To delete an e-mail message from an IMAP4 server, the XML node representing the e-mail is selected and then deleted. Or, to change the message status to read, the XML attribute representing the seen flag is changed to “true” and then the node is updated.

<!-- Create a node representing our Inbox -->
<xsp:variable name="inbox" type="node"
  select="document('imap://imap.gmail.com?user=admin&pass=secret')/folder[@name='INBOX']"/>
<!-- Delete all e-mail messages over 1MB in our Inbox -->
<xsp:remove-node select="$inbox/mail[@size > 1048576]"/>
<!-- Change to read all messages with subject 'Newsletter' -->
<xsp:set-attribute target="$inbox/mail[@subject = 'Newsletter']" name="seen" select="'true'"/>

To delete a file from the local file system, the XML node representing the file is selected and then deleted. Or, to rename a file, the XML attribute representing the file name is changed and then the node is updated.

<!-- Create a node representing our 'documents' directory -->
<xsp:variable name="documents" type="node" select="document('file:///home/jkirk/documents')"/>
<!-- Delete file 'photo.jpg' under a sub-directory -->
<xsp:remove-node select="$documents/directory[@name='personal']/file[@name='photo.jpg']"/>
<!-- Rename file titled 'report.xls' to 'sales report.xls' -->
<xsp:set-attribute target="$documents/file[@name='report.xls']"
  name="name" select="'sales report.xls'"/>

As you can see, it doesn’t matter if we are working with database records, e-mail messages or files; X# abstracts the developer completely using just four basic operations. Working with other repositories or types of information is exactly the same; for example threads are created by appending the program code node to a node representing the thread manager:

<xsp:append-child target="/threads">
  <xsp:pi>
    <!-- My code goes here -->
  </xsp:pi>
</xsp:append-child>

Or, we could call a Web service function simply by executing an XML node representing the Web service operation and passing the nodes representing the parameters:

<xsp:with>
  <xsp:context>
    <appid>testSearch</appid>
    <query>xsharp manual</query>
  </xsp:context>
  <xsp:execute
    select="document('rest:[http://search.yahooapis.com/WebSearchService/V1/webSearch?messagetype=x-www-form&httpmethod=get')"/>]
</xsp:with>
0 Attachments 0 Attachments
1368 Views

Average (0 Votes)