[xsl] Code Structure Question

Subject: [xsl] Code Structure Question
From: "Michael A. Thompson" <mathomp@xxxxxxxxxxxxxx>
Date: Tue, 18 Feb 2003 14:56:48 -0500
If I have to restructure xml in a non-linear way by jumping around conditionally in the tags to extract the information in the order that it is needed, what is the best way structure my code?

Example, This is what I have been working on (the XML input and what the xml output should look like are at the end of the email:
====================================
XSL FILE:
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"; version="1.0">


	<xsl:output indent="yes" method="xml" />
	
<!-- process the AAA tag -->
	<xsl:template match="AAA">
        		<ADAD>
            		<xsl:apply-templates select="DDD"/>
        		</ADAD>
	</xsl:template>
	
	<!-- AAA tag -->
    	<xsl:template match="DDD">
        		<WWW value="{./@title}">
			<!-- get COMM data -->
            		<xsl:apply-templates select="COMM"/>

			<!-- process all BBB tags to produce a list -->
            		<xsl:call-template name="doBBB"/>

			<!-- now process each entity in the list -->
            		<xsl:call-template name="doBody"/>

        		</WWW>
    	</xsl:template>
    	
    	<xsl:template match="COMM">
    		<ZZZ>
        			<XXX>
        				<xsl:value-of select="."/>
        			</XXX>
    		</ZZZ>
    		
    	</xsl:template>
    	
    	
    	<xsl:template name="doBBB">
    		<YYY>
    			<xsl:for-each select="BBB">
    				<xsl:element name="UUU" use-attribute-sets="id-class"/>
    			</xsl:for-each>
    		</YYY>
    	</xsl:template>
    	
    	 <!--Attribute lists -->
    	<xsl:attribute-set name="id-class">
        		<xsl:attribute name="id">
            		<xsl:value-of select="./@ident"/>
        		</xsl:attribute>
        		<xsl:attribute name="class">
			<!-- store type -->
            		<xsl:variable name="type" select="./III/HHH"/>
			<!-- test type -->
            		<xsl:if test="starts-with($type, 'M')">
				<!-- if true output text -->
                			<xsl:text>C_A_M</xsl:text>
            		</xsl:if>
        		</xsl:attribute>
    	</xsl:attribute-set>
    	
<!-- process body, problem is that I need to still process BBB data -->
    	<xsl:template name="doBody">
    		<xsl:variable name="type" select="../III/HHH"/>
		<!-- this will determine how we should process the list elements -->
        		<xsl:if test="starts-with($type, 'M')">
			<!-- here, if true run this routine -->
            		<!-- <xsl:call-template name="doIT"/> -->
            		<xsl:text>Found M</xsl:text>
        		</xsl:if>
		<!-- Other conditions will continue here -->
    	</xsl:template>

</xsl:stylesheet>
=========================================
XML file:
<?xml version="1.0" encoding="UTF-8"?>
<AAA>
	<DDD ident="db_1" title="Temporary">
		<COMM>This is a Test</COMM>
		<BBB ident="x_1" title="title 1 text here">
			<CCC>
				<EEE>
					<FFF>text here 1</FFF>
				</EEE>
				<JJJ ident="x_1">
					<LLL>
						<MMM ident="x_1_A1">
							<EEE>
								<FFF>Sub text A1 here</FFF>
							</EEE>
						</MMM>
						<MMM ident="x_1_A2">
							<EEE>
								<FFF>Sub Text A2 here</FFF>
							</EEE>
						</MMM>
					</LLL>
				</JJJ>
			</CCC>
			<III>
				<HHH>M text here</HHH>
				<GGG title="M C">
					<STS>
						<AFA>x_1_A1</AFA>
					</STS>
				</GGG>
				<GGG title="M I C">
					<STS>
						<AFA>x_1_A2</AFA>
					</STS>
				</GGG>
			</III>
			<NNN ident="x_1_C">
				<EEE>
					<FFF>C text here</FFF>
				</EEE>
			</NNN>
			<NNN ident="x_1_IC">
				<EEE>
					<FFF>I C text here</FFF>
				</EEE>
			</NNN>
		</BBB>
		<BBB ident="x_2" title="title 2 here">
			<CCC>
				<EEE>
					<FFF>text 2 here</FFF>
				</EEE>
				<JJJ ident="x_2">
					<LLL>
						<MMM ident="x_2_A1">
							<EEE>
								<FFF>sub text A1 here</FFF>
							</EEE>
						</MMM>
						<MMM ident="X_2_A2">
							<EEE>
								<FFF>Sub Text A2 here</FFF>
							</EEE>
						</MMM>
						<MMM ident="x_2_A3">
							<EEE>
								<FFF>Sub Text A3 here</FFF>
							</EEE>
						</MMM>
						<MMM ident="x_2_A4">
							<EEE>
								<FFF>SubText A4 here</FFF>
							</EEE>
						</MMM>
						<MMM ident="x_2_A5">
							<EEE>
								<FFF>SubText A5 here</FFF>
							</EEE>
						</MMM>
					</LLL>
				</JJJ>
			</CCC>
			<III>
				<HHH>M text here</HHH>
				<GGG title="M I C">
					<STS>
						<AFA>x_2_A1</AFA>
					</STS>
				</GGG>
				<GGG title="M C">
					<STS>
						<AFA>x_2_A2</AFA>
					</STS>
				</GGG>
				<GGG title="M I C">
					<STS>
						<AFA>x_2_A3</AFA>
					</STS>
				</GGG>
				<GGG title="M I C">
					<STS>
						<AFA>x_2_A4</AFA>
					</STS>
				</GGG>
				<GGG title="M C">
					<STS>
						<AFA>x_2_A5</AFA>
					</STS>
				</GGG>
			</III>
			<NNN ident="x_2_C">
				<EEE>
					<FFF>C text here</FFF>
				</EEE>
			</NNN>
			<NNN ident="x_2_C">
				<EEE>
					<FFF>C text here</FFF>
				</EEE>
			</NNN>
			<NNN ident="x_2_IC">
				<EEE>
					<FFF>Text here</FFF>
				</EEE>
			</NNN>
		</BBB>
	</DDD>
</AAA>

=========================================
Output format xml file:
<?xml version="1.0" encoding="UTF-8"?>
<ADAD>
	<WWW value="Temporary"/>
	<ZZZ>
		<XXX>title 1 text here</XXX>
	</ZZZ>
	<YYY>
		<UUU class="C_A_M" id="x_1"/>
		<UUU class="C_A_M" id="x_2"/>
	</YYY>
	<C_A_M id="x_1">
		<SSS>
			<XXX>text here 1</XXX>
			<TTT>
				<VVV value="true"/>
				<OOO value="true"/>
			</TTT>
		</SSS>
		<QQQ id="x_1_A1" position="1">
			<XXX>Sub text A1 here</XXX>
		</QQQ>
		<QQQ id="x_1_A2" position="2">
			<XXX>Sub Text A2 here</XXX>
		</QQQ>
		<RRR>
			<VVV>C text here</VVV>
			<ABAB>I C text here</ABAB>
			<ACAC id="x_1_A1"/>
		</RRR>
	</C_A_M>
	<C_A_M>
		<SSS>
			<XXX>text 2 here</XXX>
			<TTT>
				<VVV value="true"/>
				<OOO value="true"/>
			</TTT>
		</SSS>
		<QQQ id="x_2_A1" position="1">
			<XXX>Sub text A1 here</XXX>
		</QQQ>
		<QQQ id="x_2_A2" position="2">
			<XXX>Sub Text A2 here</XXX>
		</QQQ>
		<QQQ id="x_2_A3" position="3">
			<XXX>Sub text A3 here</XXX>
		</QQQ>
		<QQQ id="x_2_A4" position="4">
			<XXX>Sub Text A4 here</XXX>
		</QQQ>
		<QQQ id="x_2_A5" position="5">
			<XXX>Sub Text A4 here</XXX>
		</QQQ>
		<RRR>
			<VVV>C text here C text here</VVV>
			<ABAB>I C text here</ABAB>
			<ACAC answer_id="x_2_A1"/>
		</RRR>
	</C_A_M>
</ADAD>


XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list



Current Thread