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 XML mining rules for this example can easily be enhanced to produce DSV output that can be interactively queried using existing XTRAN rules.

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:




Process Flowchart

Here is a flowchart for this process, in which the elements are color coded:

process flowchart

XML input:  sample.xml

<tag1><tag2/><tag3 att1="att1v" att2="att2v">
This is a line of text, &amp; 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, &amp; 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>



Output:

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)
&amp;  2
    sample.xml(2)
    sample.xml(5)