Lab 5. Evaluate an XML design

Below is an XML document that contains data about a 
military mission. It describes the type-of-mission 
(training, actual, simulation) and the weapons used.

<Mission>
    <Type>simulation</Type>
    <WeaponRelease>
        <Munition>CBU-89</Munition>
        <Quantity>20</Quantity>
        <Target>Enemy terrorists</Target>
        <DateTime>2013-05-11T08:00:00</DateTime>
    </WeaponRelease>
</Mission>

Suppose that processing <WeaponRelease> requires knowing the 
type-of-mission. 

Suppose the XSLT processor is positioned at <WeaponRelease>
Will the XSLT code be able to process <WeaponRelease> correctly?

Scroll down for answer ...


































The <Type> element is a preceding sibling of the <WeaponRelease>
element. The XSLT code for <WeaponRelease> cannot access the data
in the <Type> element since that would require the XSLT processor
to "back up", which is not allowed in streaming.

How would you design the XML so that the XSLT code for <WeaponRelease>
would have access to the mission type (training, actual, simulation)?

Scroll down for answer ...




































Put the data about the type-of-mission in an ancestors attribute,
so the XSLT can process WeaponRelease:

<Mission type="simulation">
    <WeaponRelease>
        <Munition>CBU-89</Munition>
        <Quantity>20</Quantity>
        <Target>Enemy terrorists</Target>
        <DateTime>2013-05-11T08:00:00</DateTime>
    </WeaponRelease>
</Mission>

Now the code that processes <WeaponRelease> can use this motionless
expression to access the mission type: ../@type

Here's a streaming XSLT program:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                version="3.0">
    
    <xsl:output method="xml" />
    
    <xsl:mode streamable="yes" />
    
    <xsl:template match="WeaponRelease">
        <Mission>
            <xsl:choose>
                <xsl:when test="../@type eq 'training'">
                    <xsl:text>Training Mission</xsl:text>
                </xsl:when>
                <xsl:when test="../@type eq 'actual'">
                    <xsl:text>Actual Mission</xsl:text>
                </xsl:when>
                <xsl:when test="../@type eq 'simulation'">
                    <xsl:text>Simulation Mission</xsl:text>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:message terminate="yes">Invalid Mission Type</xsl:message>
                </xsl:otherwise>
            </xsl:choose>
        </Mission>
    </xsl:template>
    
</xsl:stylesheet>


What have we learned about designing XML documents so that
they can be stream-processed?

Scroll down for answer ...



































A simple change in XML design can make the difference between a 
streamable XML versus a non-streamable XML.

Put metadata and reference information (stuff that's needed for 
reference throughout document processing) at the start of the 
document rather than the end, or in a separate document. Use 
hierarchic nesting for relationships rather than id/idref style 
pointers (even perhaps if it means holding the data redundantly).

