Re: [xsl] Converting Flat XML to Hierarchical structure using xsl.

Subject: Re: [xsl] Converting Flat XML to Hierarchical structure using xsl.
From: Arulraj <p_arulraj@xxxxxxxxx>
Date: Mon, 14 Nov 2005 03:53:45 -0800 (PST)
Hello Ragulf,

Given XML content:
-------------------
<?xml version="1.0" encoding="UTF-8"?>
<office>
	<ordered-list style-name="L1">
		<list-item>
			<h style-name="P1" level="1">Application:Die
application</h>
		</list-item>
	</ordered-list>
	<ordered-list style-name="L2">
		<list-item>
			<ordered-list>
				<list-item>
					<h style-name="P3"
level="2"><s/>Processgroup:abc</h>
				</list-item>
			</ordered-list>
		</list-item>
	</ordered-list>
	<ordered-list style-name="L3">
		<list-item>
			<ordered-list>
				<list-item>
					<ordered-list>
						<list-item>
							<h style-name="P4"
level="3"><s/>Process:FSRenta</h>
						</list-item>
					</ordered-list>
				</list-item>
			</ordered-list>
		</list-item>
	</ordered-list>
	<ordered-list style-name="L3"
continue-numbering="true">
		<list-item>
			<ordered-list continue-numbering="true">
				<list-item>
					<ordered-list continue-numbering="true">
						<list-item>
							<ordered-list continue-numbering="true">
								<list-item>
									<h style-name="P5"
level="4"><s/>PanelName:AnalysisLayout<s/></h>
									<p style-name="P6">TitelIcon:xxxxx</p>
									<p style-name="P6">TitelWWWWWWW</p>
									<p style-name="P6">Titeldata<tab-stop/>
									</p>
								</list-item>
							</ordered-list>
						</list-item>
					</ordered-list>
				</list-item>
			</ordered-list>
		</list-item>
	</ordered-list>
	<ordered-list style-name="L3"
continue-numbering="true">
		<list-item>
			<ordered-list continue-numbering="true">
				<list-item>
					<ordered-list continue-numbering="true">
						<list-item>
							<ordered-list continue-numbering="true">
								<list-item>
									<h style-name="P5"
level="4"><s/>PanelName:GeneralLayout<s/></h>
									<p style-name="P6">TitelIcon:xxxxx</p>
									<p style-name="P6">TitelWWWWWWW</p>
									<p style-name="P6">Titeldata<tab-stop/>
									</p>
								</list-item>
							</ordered-list>
						</list-item>
					</ordered-list>
				</list-item>
			</ordered-list>
		</list-item>
	</ordered-list>
	<ordered-list style-name="L2">
		<list-item>
			<ordered-list>
				<list-item>
					<h style-name="P3"
level="2"><s/>Processgroup:xxx</h>
				</list-item>
			</ordered-list>
		</list-item>
	</ordered-list>
	<ordered-list style-name="L3">
		<list-item>
			<ordered-list>
				<list-item>
					<ordered-list>
						<list-item>
							<h style-name="P4"
level="3"><s/>Process:TestApplication</h>
						</list-item>
					</ordered-list>
				</list-item>
			</ordered-list>
		</list-item>
	</ordered-list>

</office>

I have created the following XSL:
---------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
<xsl:output method="xml"/>
<xsl:template match="office">
<layout>
    <xsl:apply-templates
select="ordered-list[1]/list-item">
    	<xsl:with-param
name="strIndexTmp">1</xsl:with-param>
    </xsl:apply-templates>
</layout>				
</xsl:template>

<xsl:template match="list-item">
    	<xsl:param name="strIndexTmp"/>

	<!-- Get the element name and assign in this variable
xx -->
	<xsl:variable name="xx">
		<xsl:apply-templates select="current()"
mode="getElementName"/>
	</xsl:variable>

	<!-- Create the element in inner level -->
	<xsl:if test="$xx != '' ">
		<xsl:choose>
			<xsl:when test="contains($xx,':')">
				<xsl:variable name="ElementName"
select="substring-before($xx,':')"/>
				<xsl:variable name="attributeName"
select="substring-after($xx,':')"/>
						
						<xsl:element name="{$ElementName}">
							<xsl:attribute name="name"><xsl:value-of
select="$attributeName"/></xsl:attribute>
							<xsl:apply-templates
select="/office/ordered-list[$strIndexTmp+1]/list-item">
							    	<xsl:with-param name="strIndexTmp"
select="$strIndexTmp+1"/>
							</xsl:apply-templates>
						</xsl:element>
				
			</xsl:when>
			<xsl:otherwise>
					<xsl:element name="{$xx}">
						<xsl:apply-templates
select="/office/ordered-list[$strIndexTmp+1]/list-item">
						    	<xsl:with-param name="strIndexTmp"
select="$strIndexTmp+1"/>
						</xsl:apply-templates>
					</xsl:element>
			</xsl:otherwise>
		</xsl:choose>


	</xsl:if>
</xsl:template>

<!-- Template to get the value of element h -->
<xsl:template match="list-item" mode="getElementName">
	<xsl:if test="h">
		<xsl:value-of select="h"/>
	</xsl:if>
	<xsl:if test= "not(h)">
	    <xsl:apply-templates
select="ordered-list/list-item"
mode="getElementName"/>
	</xsl:if>
</xsl:template>
</xsl:stylesheet>

I get the following Output:
<layout>
    <Application name="Die application">
        <Processgroup name="abc">
            <Process name="FSRenta">
                <PanelName name="AnalysisLayout">
                    <PanelName name="GeneralLayout">
                        <Processgroup name="xxx">
                            <Process
name="TestApplication"/>
                        </Processgroup>
                    </PanelName>
                </PanelName>
            </Process>
        </Processgroup>
    </Application>
</layout>


But the expected output is:
<layout>
    <Application name="Die application">
        <Processgroup name="abc">
            <Process name="FSRenta">
                <PanelName name="AnalysisLayout">
                </PanelName>
                <PanelName name="GeneralLayout">
                </PanelName>
            </Process>
        </Processgroup>
        <Processgroup name="xxx">
               <Process name="TestApplication"/>
        </Processgroup>
    </Application>
</layout>

Any help..

Thanks
Regards,
Raj

--- Arulraj <p_arulraj@xxxxxxxxx> wrote:

> thanks for the links and suggestion..
> I will do the changes to solve that problem..
> 
> Thanks
> Regards,
> Raj
> 
> 
> --- Ragulf Pickaxe <ragulf.pickaxe@xxxxxxxxx> wrote:
> 
> > Hi again,
> > 
> > > I want to get the output in Hierarchical order
> > using
> > > the text().
> > 
> > Here, I suggest you Google "flat to hierarchical"
> > +XSL.
> > You can also try David Pawson's site:
> > http://dpawson.co.uk/xsl/sect2/flatfile.html
> > 
> > Your solution works on a hierarchical structure,
> but
> > alas, not a flat one.
> > Some following or following-sibling axis to use.
> > 
> > (From looking at your XML, the problem seems to be
> a
> > little more
> > complicated - I refer to your "continue-numbering"
> > attribute).
> > 
> > Ragards,
> > Ragulf Pickaxe :-)
> > 
> > 
> 
> 
> 
> 		
> __________________________________ 
> Yahoo! FareChase: Search multiple travel sites in
> one click.
> http://farechase.yahoo.com
> 
> 



	
		
__________________________________ 
Yahoo! Mail - PC Magazine Editors' Choice 2005 
http://mail.yahoo.com

Current Thread