RE: [xsl] creating XML hierarchy where not existed before??

Subject: RE: [xsl] creating XML hierarchy where not existed before??
From: "Meltsner, Kenneth" <Kenneth.Meltsner@xxxxxx>
Date: Mon, 18 Dec 2000 14:39:21 -0500
One minor mistake with my pseudo-code:

If you're trying to build a hierarchy of multiple layers, you need to move the call-template to the next element of the appropriate level if (and only if) there isn't an element of higher level between the current element and that next element.  That is, if you're looking for group2 elements, you can jump to the next group2 element on the forward-sibling access iff there isn't an intervening group1.

The other way to do this is with keys: for each element, define a key that points to its immediate "parent" -- the element of one level higher right before it.  Then for each element, process the element followed by processing all the elements that have that element as a parent.

<xsl:key name="actparent" match="//p[starts-with(@stylename, 'Level2')]" use="preceding-sibling::p[starts-with(@stylename,'Level1')][1]" /> 
  <xsl:key name="actparent" match="//p[starts-with(@stylename, 'Level3')]" use="preceding-sibling::p[starts-with(@stylename,'Level2')][1]" /> 
  <xsl:key name="actparent" match="//p[starts-with(@stylename, 'Level4')]" use="preceding-sibling::p[starts-with(@stylename,'Level3')][1]" /> 
  <xsl:key name="actparent" match="//p[starts-with(@stylename, 'Level5')]" use="preceding-sibling::p[starts-with(@stylename,'Level4')][1]" /> 
  <xsl:key name="actparent" match="//p[starts-with(@stylename, 'Level6')]" use="preceding-sibling::p[starts-with(@stylename,'Level5')][1]" /> 
  
and from a template elsewhere in the stylesheet (that starts with "group1" element"):

<xsl:for-each select="key('actparent', .)">
  <xsl:apply-templates select="." /> 
  </xsl:for-each>
  
to build a hierarchy of activities (actparent == activity parent) that's 6 levels deep.

Ken

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


Current Thread