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

XML: What would be the best design for a menu tree?

Status
Not open for further replies.

saul11

Programmer
Jan 16, 2005
7
NL
What would be the best design for a flexible and unlimited menu tree with XML?

Now I have an XML file with only one type of tag (<item>)
The tag holds the name of the item and possible subitems.
I've built it this way so that it would be unlimited in depth, e.g. :
Code:
<?xml version="1.0" encoding="ISO-8859-1"?>
<menu>
. <item>
. . treeHead 1
. . <item>treeSub 1.1</item>
. . <item>
. . . treeSub 1.2
. . . <item>treeSub 1.2.1</item>
. . . <item>treeSub 1.2.2</item>
. . </item>
. . <item>treeSub 1.3</item>
. . <item>treeSub 1.4</item>
. </item>
. <item>
. . treeHead 2
. . <item>
. . . treeSub 2.1
. . . <item>
. . . . treeSub 2.1.1
. . . . <item>
. . . . . treeSub 2.1.1.1
. . . . . <item>treeSub 2.1.1.1.1</item>
. . . . </item>
. . . </item>
. . </item>
. </item>
</menu>

(If you're interested in the menubuilder, see [URL unfurl="true"]http://www.saulmade.nl/contextMenu/[/URL] and its thread on [URL unfurl="true"]http://forum.echoechoplus.com/showthread.php?s=&threadid=7807[/URL])

But now I have problems regenerating the tree out of the XML file.
I want the items on each level to have a differnt fontSize and indent (Like the original tree on [URL unfurl="true"]http://www.saulmade.nl/contextMenu/[/URL])
So can an XSLT file be used for this?
Can an XSLT file generate a fontsize and marginLeft style property depending on the depth of an item ( = XML node) ?
And can those properties be limited to a certain value, e.g can the XSLT file check if the generated fontsize is larger than a specified value or set it to that value when it was smaller. (like if(x < value){x = value})

(And as an extra, can a different image be assigned depending whether or not the item contans subitems?)

So can someone help me with the xslt file or is there another way to do this?

Thanks
 
ideally, every bit of text in an xml file is enclosed by a single set of begin/end tags...

You have text floating in the parent tags...
Code:
<menu>
. <item>
. . [b]treeHead 1[/b]
. . <item>treeSub 1.1</item>
. . <item>
. . . [b]treeSub 1.2[/b]
. . . <item>treeSub 1.2.1</item>
. . . <item>treeSub 1.2.2</item>
. . </item>
. . <item>treeSub 1.3</item>
. . <item>treeSub 1.4</item>
. </item>
Which has a tendancy to cause probmlems in the long run...

I would start by enclosing the text in tags...
Code:
<menu>
. <item>
. . [b]<head>treeHead 1</head>[/b]
. . <item>treeSub 1.1</item>
. . <item>
. . . [b]<head>treeSub 1.2</head>[/b]
. . . <item>treeSub 1.2.1</item>
. . . <item>treeSub 1.2.2</item>
. . </item>
. . <item>treeSub 1.3</item>
. . <item>treeSub 1.4</item>
. </item>

As far as: (like if(x < value){x = value})
I just wrote a Tip on this called Conditional Variables in XSL...
Read over it here: thread426-985480

>>(And as an extra, can a different image be assigned depending whether or not the item contans subitems?)
Yes... you just have to get creative with the <xsl:if> tag ;-)

And for something like this, you might want to look into making a recursive template, using a named template instead of a match template...

Each time you find a node that contains a child node make the template call itself...

Visit My Site
PROGRAMMER: (n) Red-eyed, mumbling mammal capable of conversing with inanimate objects.
 
Your post is very clear and really tackles my posted problems, but I went on trying to embed XML into HTML...
I found out that there is no cross-browser solution for working with xml data islands in HTML!

An xml file (possible including an xslt file) can only be showed in a window or frame, not just somewhere where you'd like it in your HTML.

Not even quirksmode has a cross-browser solution : Quirksmode links to another script ( ) that is the closest to being cross-browser, but now it is IE that causes trouble, so another check is needed to let it run in IE...

With all those side roads needed to come to a cross-browser XML parsing, I choose for transforming the XML to HTML with PHP (without using an XSLT file).
I'll just write my own parser and output the right HTML...
Other methods are available of course:
This page ( ) describes transforming with the use of an XSLT processor which reads in both the XML document and the XSLT stylesheet.

quite a disappointment I think.

(correct me if I'm wrong plz)
 
>>An xml file (possible including an xslt file) can only be showed in a window or frame, not just somewhere where you'd like it in your HTML.

What do you mean by this?

You can have XML data islands on your page, then display them with javascript's (or vbscript's) document.write method...

you can transform it with an XSL by using
xml.transformNode(xsl)
As shown in this example:
(from:
Not sure if that is what you mean, but I hope it helps...

-------

As far as the browser war thing goes, I have made a choice to lean towards IE... It seems to have more functionality the the others I have used, as far as XML stuff goes, and they seem to be leading the way...
Which means everyone else will probably, eventually, have to follow...

That's just my choice/opinion though... I'm just tired of writing 10 different versions for each page on a site (little exageration there, but the point still stands)

For the most part I try to follow the W3 recomendations... which is what the browser developers are supposed to be doing also...

And if IE wants to make my life easier, I let it ;-)

LOL, seriously though, I hope the above tutorial helps...

I wouldn't worry about the Cross browser thing too much, if it doesn't work on one of them, just make a disclaimer and if they want to view your site in full, they can use the browser it was designed for (hopefully a mainstream one... IE, NetScape, etc...)

Visit My Site
PROGRAMMER: (n) Red-eyed, mumbling mammal capable of conversing with inanimate objects.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top