Page 1 of 1

Using ⎕XML to generate "fragments"

Posted: Tue May 21, 2013 12:12 pm
by Morten|Dyalog
I recently had the opportunity to use ⎕XML to generate XML (rather than read it), and may have experienced first hand an issue that I believe I have heard others discuss in the past - that ⎕XML does not provide a convenient mechanism for easily assembling fragments of XML into a whole.

For example, if I have a sub-function to create the leaves of my XML, which returns something along the lines of:

<person><name>Jill</name></person></person><name>Jack</name></person>

... And then I have a top-level function that needs to insert this into an "envelope" along the lines of:

<database>
<title>My friends</title>
[persons go here]
</database>

... It is a little awkward to do the last step. I was wondering whether it would make sense to extend ⎕XML so that pre-formatted XML can be inserted, so I could construct the above as follows:

xml←1 3⍴0 'database'
xml⍪← 1 'title' 'My friends'
xml⍪← ¯2 '' '<person><name>Jill</name>.....</person>'

In this case, the negative level number is the flag which marks the row as containing pre-build XML, but that is just an idea.

We would be interested to hear from anyone who has used ⎕XML to construct non-trivial output files: Would this be helpful, or do you have other ideas for features that would make it easier to construct large XML files?

Re: Using ⎕XML to generate "fragments"

Posted: Thu May 23, 2013 3:10 pm
by StephenTaylor
This is a common requirement in handling XML documents. I've done this in both PHP and JavaScript. Both provide methods for appending nodes to document trees. Also for selecting nodes or lists of nodes, usually using XPath notation.

I would see a choice between three approaches.

1. work on ⎕XML only
2. provide simple tools as a class similar to PHP's SimpleXML
3. go all out, perhaps using JavaScript Node object as the model

Re: Using ⎕XML to generate "fragments"

Posted: Fri May 24, 2013 7:56 pm
by paulmansour
Morten,

I experienced this when I first started working with ⎕XML.

To solve the problem I generate suitable XML matrices, rather than actual XML text, and then, when I have a complete XML matrix, use ⎕XML on it. This requires passing the appropriate depth value to any function that generates an XML matrix.

In retrospect it does not seem like a big deal, but at the time I remember being annoyed that I had to track the depth. It is probably pretty off-putting for a newbie.

I think it is worth exploring a better way.

Paul