From: Brian G. <br...@qu...> - 2003-04-20 19:48:40
|
> Take an RSS news feed for example, typically these can contain up to 15 > news items. You may only want to display five of them. As the item nodes > are usually under a channel node, this would be rather awkward to implement > with a generic tree traversal directive. This is a good example, since it would be a common usage of any DOM traversal in WM and is pretty simple. Option 1: preprocess with a servlet and extract what you want. This is a pretty decent option. The servlet traverses the tree and puts an array of RssFeedElement objects, with properties like Link, Description, etc, in the context. Then the template traverses it with #foreach. This option only works when your document is simple, like an RSS feed, which has a relatively flat structure. Option 2: Introduce a context tool that knows how to flatten trees into linear streams of SAX-like events: begin-x begin-y z z end-y end-x Now, traverse it in WM with #foreach. You have to maintain context by yourself, though, and this sucks even worse in WM than it does in ordinary languages. Option 3: Use XPath for selection. Use an XPath select to whittle down the subset of the document you want, and then apply the techniques from Option 1 or 2. This is nice because it eliminates nearly all of the extraneous XML and gives you something suitable for transforming into a linear array. Option 4: Some sort of traversal directive, which is going to end up looking a lot like XSL. Combining it with XPath could be a win. #domtree $dom { #node "content" $c { blah $c } #node "channel" $c { blah blah $c } } |