This is the mail archive of the xsl-list@mulberrytech.com mailing list .


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Forming a Tree


Hi,
I am trying to form a tree on a raw XML, my input XML looks like -

<?xml version="1.0"?>
<Root>
<row BookId="1" BookTitle="abc1" TopicId="1" ParentTopicId="1" TopicTitle="Topic1"/>
<row BookId="1" BookTitle="abc1" TopicId="2" ParentTopicId="1" TopicTitle="Topic11"/>
<row BookId="1" BookTitle="abc1" TopicId="4" ParentTopicId="4" TopicTitle="Topic12"/>
<row BookId="1" BookTitle="abc1" TopicId="3" ParentTopicId="2" TopicTitle="Topic21"/>

<row BookId="1" TopicId="1" SiteId="1" SortOrder="0" SiteTitle="Site1" />
<row BookId="1" TopicId="2" SiteId="2" SortOrder="0" SiteTitle="Site2" />
<row BookId="1" TopicId="3" SiteId="3" SortOrder="0" SiteTitle="Site3" />
<row BookId="1" TopicId="4" SiteId="4" SortOrder="1" SiteTitle="Site4" />
<row BookId="1" TopicId="4" SiteId="1" SortOrder="0" SiteTitle="Site1" />
</Root>

and my XSL is -

<?xml version='1.0'?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>

<xsl:template match="Root">
<TreeNodes>
<xsl:apply-templates select="row[@BookTitle and not(preceding::row/@BookId=@BookId)]" />
</TreeNodes>
</xsl:template>

<xsl:template match="row">
<TreeNode>
<xsl:variable name="node1" select="."/>
<xsl:attribute name="BookId"><xsl:value-of select="@BookId"/></xsl:attribute>
<xsl:attribute name="Text"><xsl:value-of select="@BookTitle"/></xsl:attribute>

<!-- Topic node -->
<xsl:for-each select="//row[@ParentTopicId=@TopicId and @BookId=$node1/@BookId and @TopicTitle]">
<TreeNode>
<xsl:variable name="node2" select="."/>
<xsl:attribute name="TopicId"><xsl:value-of select="@TopicId"/></xsl:attribute>
<xsl:attribute name="ParentTopicId"><xsl:value-of select="@ParentTopicId"/></xsl:attribute>
<xsl:attribute name="Text"><xsl:value-of select="@TopicTitle"/></xsl:attribute>

<!-- Sub-Topic1 -->
<xsl:for-each select="//row[@ParentTopicId=$node2/@TopicId and @ParentTopicId!=@TopicId and @BookId=$node1/@BookId and @TopicTitle]">
<TreeNode>
<xsl:variable name="node3" select="."/>
<xsl:attribute name="TopicId"><xsl:value-of select="@TopicId"/></xsl:attribute>
<xsl:attribute name="ParentTopicId"><xsl:value-of select="@ParentTopicId"/></xsl:attribute>
<xsl:attribute name="Text"><xsl:value-of select="@TopicTitle"/></xsl:attribute>

<!-- Sub-Topic2 -->
<xsl:for-each select="//row[@ParentTopicId=$node3/@TopicId and @ParentTopicId!=@TopicId and @BookId=$node1/@BookId and @TopicTitle]">
<TreeNode>
<xsl:variable name="node4" select="."/>
<xsl:attribute name="TopicId"><xsl:value-of select="@TopicId"/></xsl:attribute>
<xsl:attribute name="ParentTopicId"><xsl:value-of select="@ParentTopicId"/></xsl:attribute>
<xsl:attribute name="Text"><xsl:value-of select="@TopicTitle"/></xsl:attribute>

<!-- Site -->
<xsl:for-each select="//row[@BookId=$node4/@BookId and @TopicId=$node4/@TopicId and @SiteId]">
<xsl:sort select="@SortOrder" data-type="number"/>
<TreeNode>
<xsl:call-template name="SiteDetails">
<xsl:with-param name="site" select="."/>
</xsl:call-template>
</TreeNode>
</xsl:for-each>
</TreeNode>
</xsl:for-each>

<!-- Site -->
<xsl:for-each select="//row[@BookId=$node3/@BookId and @TopicId=$node3/@TopicId and @SiteId]">
<xsl:sort select="@SortOrder" data-type="number"/>
<TreeNode>
<xsl:call-template name="SiteDetails">
<xsl:with-param name="site" select="."/>
</xsl:call-template>
</TreeNode>
</xsl:for-each>
</TreeNode>
</xsl:for-each>

<!-- Site -->
<xsl:for-each select="//row[@BookId=$node2/@BookId and @TopicId=$node2/@TopicId and @SiteId]">
<xsl:sort select="@SortOrder" data-type="number"/>
<TreeNode>
<xsl:call-template name="SiteDetails">
<xsl:with-param name="site" select="."/>
</xsl:call-template>
</TreeNode>
</xsl:for-each>
</TreeNode>
</xsl:for-each>
</TreeNode>
</xsl:template>

<xsl:template name="SiteDetails">
<xsl:param name="site"/>
<xsl:attribute name="TopicId"><xsl:value-of select="$site/@TopicId"/></xsl:attribute>
<xsl:attribute name="SiteId"><xsl:value-of select="$site/@SiteId"/></xsl:attribute>
<xsl:attribute name="Text"><xsl:value-of select="$site/@SiteTitle"/></xsl:attribute>
</xsl:template>

</xsl:stylesheet>

I am applying transformation using .NET classes.
Can the above XSL optimized to gain performance ?
Can I avoid '//row' to achieve the same?

Thanks in advance.
Venkatesh
_________________________________________________________
There is always a better job for you at Monsterindia.com.
Go now http://monsterindia.rediff.com/jobs


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


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]