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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

recursive transformation 1

Status
Not open for further replies.

CassidyHunt

IS-IT--Management
Jan 7, 2004
688
0
0
US
I have an xml document that looks like this:

Code:
<data>
     <Menu>
          <name>Home</name>
          <order>1</name>
     </Menu>
     <Menu>
          <name>Explore</name>
          <parent>Home</parent>
          <order>1</name>
     </Menu>
     <Menu>
          <name>Contact</name>
          <parent>Home</parent>
          <order>2</name>
     </Menu>
     <Menu>
          <name>Products</name>
          <order>2</name>
     </Menu>
</data>

Of course it doesn't end that abruptly without finishing. Basically the parent id refers to a name. Anything that has a Parent element uses the value of that parent element to be a child of an element with that name.

I am trying to build an stylesheet that will handle the recursion and get what I need. I am stuck on how to do this. Any help would be greatly appreciated.

Cassidy
 
I have been trying something like this. I can't seem to get it to work back through the entire document each time.

If anyone has any clue that might help me let me know.

Code:
<?xml version='1.0'?>
<xsl:stylesheet version="1.0" xmlns:xsl="[URL unfurl="true"]http://www.w3.org/1999/XSL/Transform">[/URL]
<xsl:output method="html"/>
<xsl:template match="/">
	<xsl:for-each select="NewDataSet/Menu">
		<xsl:if test="count(parent) = 0">
			<xsl:variable name="sparent"><xsl:value-of select="name"/></xsl:variable>
			<xsl:value-of select="name"/><br />
			<xsl:call-template name="getChildren">
				<xsl:with-param name="mparent" select="$sparent" />
			</xsl:call-template>
		</xsl:if>
	</xsl:for-each>
</xsl:template>

<xsl:template name="getChildren" match="/">
	<xsl:param name="mparent" />
	<xsl:for-each select="NewDataSet/Menu">
		<xsl:if test="count(parent) > 0">
			<xsl:if test="parent = $mparent">
				<xsl:value-of select="name"/><br />
			</xsl:if>
		</xsl:if>
	</xsl:for-each>
</xsl:template>
</xsl:stylesheet>

Thanks
 
This works but I don't think it would go multiple levels deep.

Code:
<?xml version='1.0'?>
<xsl:stylesheet version="1.0" xmlns:xsl="[URL unfurl="true"]http://www.w3.org/1999/XSL/Transform">[/URL]
<xsl:output method="html"/>
<xsl:template match="/">
	<xsl:for-each select="NewDataSet/Menu">
		<xsl:if test="count(parent) = 0">
			<xsl:variable name="sparent"><xsl:value-of select="name"/></xsl:variable>
			<xsl:value-of select="name"/><ul>
			<xsl:call-template name="getChildren">
				<xsl:with-param name="mparent"><xsl:value-of select="$sparent" /></xsl:with-param>
			</xsl:call-template>
			</ul>
		</xsl:if>
	</xsl:for-each>
</xsl:template>

<xsl:template name="getChildren" match="//NewDataSet">
	<xsl:param name="mparent" />
	<xsl:for-each select="//Menu">
		<xsl:if test="count(parent) > 0">
			<xsl:if test="parent = $mparent">
				<li><xsl:value-of select="name"/></li>
			</xsl:if>
		</xsl:if>
	</xsl:for-each>
</xsl:template>
</xsl:stylesheet>

Let me know if this is they way this should be done or if there is a better way.

Thanks
 
This is the kind of solution you would want:

<?xml version='1.0'?>
<xsl:stylesheet version="1.0" xmlns:xsl="
<xsl:eek:utput method="html"/>

<xsl:template match="/">
<xsl:apply-templates />
</xsl:template>

<xsl:template match="//NewDataSet/Menu[not(parent)]">
<xsl:value-of select="name" />
<ul>
<xsl:apply-templates select="//NewDataSet/Menu[parent]">
<xsl:with-param name="sparent" select="name" />
</xsl:apply-templates>
</ul>
</xsl:template>

<xsl:template match="//NewDataSet/Menu[parent]">
<xsl:param name="sparent" />
<xsl:if test="parent=$sparent">
<li><xsl:value-of select="name" /></li>
</xsl:if>
</xsl:template>

</xsl:stylesheet>
 
I like your solution much more but I was wondering why I get the order number showing up on the form?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top