|
[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message] Re: Generating a tree
Hi Marcus,
This kind of problem can be a fun challenge. If you don't have a lot of XSLT experience (and sometimes even if you do), the hard part of a problem like this is knowing which are the hard bits, and which not. In this case, it's the node selection logic that is the hard part: how do you traverse the tree including just the nodes you want? The easy part is rendering those nodes as you select them. (Indenting can readily be achieved by iterating over ancestors of a node and putting out whitespace for each ancestor.) So how to select the nodes? One clue can be gotten by keeping in mind that by default the XSLT processor goes top-down, and in fact since this is how you want your rendition to appear, this is the right way to go. This means translating your logic specifying which nodes you want to include to a top-down model. As it happens, this is pretty straightforward: starting at the document element (not the root node)
A. process all its children
with each child encountered:
if it is an ancestor-or-self of your target node
mark with 'minus', and
if it's an ancestor, return to A
if it's the target node, just write the children
if not
if it has children, mark with 'plus'
in any case, write it outThis reduces the logic to a recursive process that is easily enough achieved using XSLT's processing model. The only trick is in that test: when processing a node, how do you know it's an ancestor of your target node (or the target node itself)? Assume you have bound your target node to a variable: <xsl:variable name="target" select="//*[@id='somethingorother']"/> In this case, a current node "." is among the ancestor elements of the target, or the target itself, if it passes the following test: count($target/ancestor-or-self::*) = count(. | $target/ancestor-or-self::*) (If you want to improve efficiency a mite you can bind the left side to a variable, since it's always the same.) Likewise, the current node "." is the target when count(. | $target) = 1 (XSLT 1.0 idiom for testing node identity.) I hope that's enough to get you moving. Hint: don't traverse up from the target node. Instead, traverse down from the document element, testing as you go. The tests will determine whether to descend any particular branch of the tree. Another hint: modes are your friends. Ask again if I'm being too sketchy here. And good luck, Wendell At 04:16 PM 5/4/2004, you wrote: Hi gurus ====================================================================== Wendell Piez mailto:wapiez@xxxxxxxxxxxxxxxx Mulberry Technologies, Inc. http://www.mulberrytech.com 17 West Jefferson Street Direct Phone: 301/315-9635 Suite 207 Phone: 301/315-9631 Rockville, MD 20850 Fax: 301/315-8285 ---------------------------------------------------------------------- Mulberry Technologies: A Consultancy Specializing in SGML and XML ======================================================================
|
PURCHASE STYLUS STUDIO ONLINE TODAY!Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced! Download The World's Best XML IDE!Accelerate XML development with our award-winning XML IDE - Download a free trial today! Subscribe in XML format
|

Cart








