XTRAN Example — Analyze XML Entity References
Scenario — you want to analyze the use of entity references in XML.
XTRAN to the rescue!
XTRAN treats XML as a computer language, in which each tag, line and segment of text, or end tag is a "statement", and each tag attribute is a "statement attribute".
The following example uses an XTRAN rules file comprising 93 non-comment lines of XTRAN's rules language ("meta-code") to analyze all entity references in XML.
The following is an English paraphrase of the XTRAN rules used for this example:
For each XML "statement" If nonmarkup text line or segment For each entity reference in text Tally entity reference occurrence by name Record source file and line of this occurrence Sort entity names For each entity name seen, alphabetically Report entity name with tally For each occurrence of this entity name Report occurrence source file and line
How can such powerful and generalized XML analysis be automated in only 93 lines of rules? Because there is so much capability already available as part of XTRAN's rules language. These rules take advantage of the following functionality:
- Text manipulation
- Text formatting
- Delimited list manipulation
- "Per statement" recursive iterator
- Access to XTRAN's Internal Representation (XIR)
- Meta-variable and meta-function pointers
Here is a flowchart for this process, in which the elements are color coded:
- BLUE for XTRAN versions (runnable programs)
- ORANGE for XTRAN rules (text files)
- RED for
- PURPLE for text data files
XML input: sample.xml
<tag1><tag2/><tag3 att1="att1v" att2="att2v"> This is a line of text, & is followed by a comment.<!-- comment 1 --> This is the second line of text, &003; with no comment. </tag3> <!-- comment 2 --> This is the third line of text, & is preceded by a comment. <tag3>Some text &003; in a "tag3".</tag3> <tag4 attb="attbv1" atta/> This is more nonmarkup &003; text.<tag4 attb="attbv2"/> </tag1>
Running the rules shown above on the XML input shown above generated the following XTRAN analysis output:
XML Entity Reference Usage &003; 3 sample.xml(3) sample.xml(7) sample.xml(9) & 2 sample.xml(2) sample.xml(5)