Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Recursive Templates

Status
Not open for further replies.

LeeGath

Programmer
Jun 3, 2002
13
GB
Hello

I am writing a web site and trying to create a generice template for a given set of XML data. My aim is to display the Hierachy of the current page. For Example SectionA > SubSectionC > SubSubSectionE.

My schema is designed so the Sections are recursive(I think) : -

<xs:element name=&quot;Sections&quot;>
<xs:complexType>
<xs:sequence>
<xs:element name=&quot;Section&quot; maxOccurs=&quot;unbounded&quot;/>
</xs:sequence>
</xs:complexType>
</xs:element>

I'm just at a bit of a loss as to how I display this in my XSL stylesheet. If I was using VB or some other language I would recursively call a procedure, and produce a string of the current section compared to where it sits in the hierarchy.

Does anyone know how to achieve this?
 
This is a simple implementation of a recursive template

consider the XML file below

--------------Sections.xml---------------------------

<Sections>
<Section name=&quot;A&quot; caption=&quot;Section A&quot;>
<Section name=&quot;1&quot; caption=&quot;Section 1&quot; />
<Section name=&quot;2&quot; caption=&quot;Section 2&quot; />
<Section name=&quot;3&quot; caption=&quot;Section 3&quot; />
</Section>
<Section name=&quot;B&quot; caption=&quot;Section B&quot;>
<Section name=&quot;1&quot; caption=&quot;Section 1&quot;>
<Section name=&quot;i&quot; caption=&quot;Section i&quot; />
<Section name=&quot;ii&quot; caption=&quot;Section ii&quot; />
</Section>
<Section name=&quot;2&quot; caption=&quot;Section 2&quot; />
<Section name=&quot;3&quot; caption=&quot;Section 3&quot; />
</Section>
<Section name=&quot;C&quot; caption=&quot;Section C&quot;>
<Section name=&quot;1&quot; caption=&quot;Section 1&quot; />
<Section name=&quot;2&quot; caption=&quot;Section 2&quot; />
<Section name=&quot;3&quot; caption=&quot;Section 3&quot; />
</Section>
</Sections>

-----------------------------------------------------
A simple HTML out put can obtained with
the stylesheet below:

--------------Sections.xsl---------------------------

<xsl:stylesheet xmlns:xsl=&quot; version=&quot;1.0&quot;>
<xsl:eek:utput method=&quot;html&quot; version=&quot;4.0&quot; />
<xsl:template match=&quot;/&quot;>
<xsl:apply-templates />
</xsl:template>
<xsl:template match=&quot;Sections&quot;>
<xsl:apply-templates />
</xsl:template>
<xsl:template match=&quot;Section&quot;>
<li>
<xsl:value-of select=&quot;@caption&quot; />
<xsl:if test=&quot;count(Section) > 0&quot;>
<ul>
<xsl:apply-templates />
</ul>
</xsl:if>
</li>
</xsl:template>
</xsl:stylesheet>

----------------------------------------------------
Let me know if u need any futher help
bye!
 
Thanks for that it's definitely helpful but I want to take it a step further.

How do I tract the hierarchy from the current section. For example if the current section is &quot;Section i&quot;. What I want to be able to do is display &quot;Section B > Section 1 > Section i&quot;

How would I do that in such a way that it would work for any number of levels.

Thanks
 
Result,

Thanks for your help whichman. I did your bit of code then used the ancestor-of instruction which allowed me to traverse back up the tree.

Cheers
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top