Altova Mailing List Archives


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

From: Arulraj <p_arulraj@--------->
To:
Date: 11/14/2005 11:54:00 AM
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

Disclaimer

These Archives are provided for informational purposes only and have been generated directly from the Altova mailing list archive system and are comprised of the lists set forth on www.altova.com/list/index.html. Therefore, Altova does not warrant or guarantee the accuracy, reliability, completeness, usefulness, non-infringement of intellectual property rights, or quality of any content on the Altova Mailing List Archive(s), regardless of who originates that content. You expressly understand and agree that you bear all risks associated with using or relying on that content. Altova will not be liable or responsible in any way for any content posted including, but not limited to, any errors or omissions in content, or for any losses or damage of any kind incurred as a result of the use of or reliance on any content. This disclaimer and limitation on liability is in addition to the disclaimers and limitations contained in the Website Terms of Use and elsewhere on the site.