Altova Mailing List Archives>Archive Index >microsoft.public.xsl Archive Home >Recent entries >Thread Prev - Recurse >Thread Next - Re: Recurse Re: RecurseTo: NULL Date: 6/1/2005 9:36:00 AM Kevin wrote:
> <<PlainText>>
>
> Hello all. I am somewhat new to XSL transforms. I have an xml document that
> represents a sitemap (or, it will... this is just test data). I want to
> display the sitemap in Microsoft TreeView control as mentioned in this
> article (http://aspnet.4guysfromrolla.com/articles/051403-1.aspx).
>
> The raw xml file could have any number of nestings... here is a sample...
>
> <sitemap en="Home" es="Pagina Principal" url="/default.aspx">
> <page en="Personal Finance" es="Finanzas Personales" url="/pf/default.aspx"
> />
> <page en="Community" url="/community/default.aspx">
> <page en="Community Champions" url="/community/champions.aspx">
> <regionalization region="2" en="Community in English!" />
> </page>
> </page>
> <page en="About Us" url="/au/defualt.aspx" />
> <page en="Contact Us" url="/cu/default.aspx">
> <page en="Phone Directory" url="cu/PhoneDirectory.aspx">
> <page en="President" url="/cu/President.aspx">
> <page en="President's Message" url="/cu/President/message.aspx" />
> </page>
> <page en="Vice-President" url="/cu/vicepresident.aspx" />
> <page en="Vice-President Sales" url="/cu/vpsales.aspx" />
> </page>
> </page>
> <page en="Investors" url="in/default.aspx">
> <page en="Annual Reports" url="/in/ar.aspx" />
> </page>
> </sitemap>
>
> This xml file shows four levels deep. Microsoft's treeview control requires
> the input xml file to be in <TREENODES><treenode/></TREENODES> format... so
> the above file SHOULD be transformed to:
>
> Please help me understand how to re-write the stylesheet so that the raw xml
> input file can have any number of nesting possible. Thank you in advance....
>
> Regards,
>
> Kevin
>
>
Kevin,
Try the stylesheet below. Since you are replicating the structure of the
original XML, you only need two templates: one for the root <sitemap>,
and one for each <page> node. As you can see below, the <page> template
recursively applies itself, <xsl:apply-templates select="page" />. If no
child <page> node is found then nothing is generated.
The code to assign the value of the 'en' attribute to text attribute,
text="{@en}" uses the shorthand 'value-of' operator, '{}' (curly
brackets). The longer way to do this is
<treenode>
<xsl:attribute name="text" select="@en" />
<xsl:apply-templates select="page" />
</treenode>
Note that when using this method (the long one) that all attribute tags
must occur immediatly following the opening tag of the element they are
being assigned to, and before the assignment of any child nodes or text.
HTH
Regards,
N. Demos
XSL:
-------------------
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output method="xml" version="1.0" indent="yes"/>
<xsl:template match="/sitemap">
<TREENODES>
<xsl:apply-templates select="page" />
</TREENODES>
</xsl:template>
<xsl:template match="page">
<treenode text="{@en}">
<!-- Recursive Call -->
<xsl:apply-templates select="page" />
</treenode>
</xsl:template>
</xsl:stylesheet>
--
Change "seven" to a digit to email me.
| ||||||
| Company | Legal | Press | Partners | Careers | Sitemap | Contact Us | Altova Blog | Mobile | Full Site | |||
|
