|XML in TCC-RT|
TCC provides the ability to open, parse and close XML documents through the use of a subset of the XPath language. The syntax of the language is explained more clearly at the W3C site:
We support a limited subset of the language explained below. Any functions of the language beyond what are listed below are unsupported, but may work.
The most common use of XML in TCC is to parse an XML formatted data file to extract elements for further processing in TCC.
There are four TCC functions that provide XML support:
1.@XMLOPEN - open an XML file for use by @XMLXPATH and/or @XMLNODES. The syntax is:
Example: set a=%@XMLOPEN[bookstore.xml]
2.@XMLCLOSE - close an XML file previously opened by @XMLOPEN. The syntax is:
Example: set a=%@XMLCLOSE
3.@XMLNODES - return the number of nodes (children) for the specified path in an XML file. The syntax is:
Example: set a=%@XMLNODES["bookstore.xml",/bookstore] - returns the number of books in the bookstore file (see example file below)
If you don't specify a filename (which *must* be in double quotes), @XMLNODES will use the XML file previously opened by @XMLOPEN.
4.@XMLXPATH - XML XPath query. (See the XML XPath docs for details on XPath syntax.) The syntax is:
Example: echo %@XMLXPATH[/bookstore/book] - lists all the sub-element values for the first book (see example file below)
If you don't specify a filename (which *must* be in double quotes), @XMLXPATH will use the XML file previously opened by @XMLOPEN.
To return an attribute, preface the attribute name with an @.
Typical Use of The XML Functions
All discussions in this section refer to the following XML data file.
<?xml version="1.0" encoding="ISO-8859-1"?>
<title lang="jap">Harry Potter</title>
<title lang="eng">Learning XML</title>
<title lang="ger">Day Watch</title>
<title lang="eng">Winston Churchill: An Autobiography</title>
Each tag in this file is referred to as a node (e.g., bookstore, book, title). The identifiers within a tag are often referred to as an attribute (e.g., lang).
Typically you will use the four commands in the following order:
1.Open the file with @XMLOPEN
2.Get the number of child nodes (records) to process with @XMLNODES
3.Set up a loop from one to the number of records to process the records with @XMLXPATH
4.Close the file with @XMLCLOSE
Below is a simple example of batch processing (without error handling J):
DO i = 1 to %b
SET Title= %@XMLXPATH[/bookstore/book[%i]/title]
SET Price= %@XMLXPATH[/bookstore/book[%i]/price]
ECHO %Title ` costs only ` %Price
•Opens the bookstore data file,
•Evaluates the number of books in the bookstore as 4(since the only child of bookstore is books) ,
•Gets the title and price for each book
•Prints each title and price out
•Closes the data file.
XPath offers a large number of processing options in addition to the ones above. We have not tested their syntax. If you wish to test additional functionality and report back to us, we will add that information to our documentation.