You are viewing a plain text version of this content. The canonical link for it is here.
Posted to j-users@xalan.apache.org by Bennett <be...@glengroup.com> on 2003/10/09 22:30:33 UTC

"vertical slices" of tree

Hello,

Say I have the following tree:



<a>
	<b>
		<c>
			<d/>
		</c>
	</b>
	<e>
		<f>
			<g/>
		</f>
	</e>
</a>


If I'm calling this into the stylesheet via document():

	- How do I get the names of all elements on the same "vertical slice" 
that <b> and <e> are on?
	- or the names of all the elements on the same "vertical slice" as <c> 
and <f> ?
	- or the names of all the elements on the same "vertical slice" as <d> 
and <g>?

Note: the names of the elements aren't static so I can't just specify a 
static path to anything.

Any ideas?
TIA


Re: "vertical slices" of tree

Posted by Johannes Lebek <jo...@gmx.de>.
The following script prints the names of the nodes that belong to these 
"vertical slices":

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
   <xsl:output method="text"/>
   <xsl:template match="/">
     <xsl:for-each select="node()/node()[not(name() = '')]">
       <xsl:text>'</xsl:text>
       <xsl:value-of select="name()"/>
       <xsl:text>'</xsl:text>
       <xsl:text>&#xA;</xsl:text>
     </xsl:for-each>
     <xsl:text>&#xA;</xsl:text>

     <xsl:for-each select="node()/node()/node()[not(name() = '')]">
       <xsl:text>'</xsl:text>
       <xsl:value-of select="name()"/>
       <xsl:text>'</xsl:text>
       <xsl:text>&#xA;</xsl:text>
     </xsl:for-each>
     <xsl:text>&#xA;</xsl:text>

     <xsl:for-each select="node()/node()/node()/node()[not(name() = '')]">
       <xsl:text>'</xsl:text>
       <xsl:value-of select="name()"/>
       <xsl:text>'</xsl:text>
       <xsl:text>&#xA;</xsl:text>
     </xsl:for-each>
   </xsl:template>
</xsl:stylesheet>

The output is:

'b'
'e'

'c'
'f'

'd'
'g'


Please note:
1) Predicate "[not(name() = '')]" is required to skip text nodes.
2) Retrieving names of nodes of a 'certain' depth could be achieved by
    using a recursively called template (parameter: depth)

Regards,

Johannes




Joseph Kesselman wrote:

> 
> 
> 
> "Siblings" was my first reaction too (the sibling axes and/or
> children-of-same-parent). However, if one takes a deeper "slice", nodes at
> the same depth may not actually be siblings. For example, see C and E in:
> 
>       <a>
>             <b>
>                   <C/>
>             </b>
>             <d FOO="bar">
>                   <E/>
>             </d>
>       </a>
> 
> If that's what's intended, I'm not sure what the best XPath expression
> would be. One approach is "nodes with same number of ancestors as this
> one", but you'd need to think about whether the attribute FOO is considered
> part of the same slice as C and E.
> 
> 
> ______________________________________
> Joe Kesselman, IBM Next-Generation Web Technologies: XML, XSL and more.
> "The world changed profoundly and unpredictably the day Tim Berners Lee
> got bitten by a radioactive spider." -- Rafe Culpin, in r.m.filk
> 
> 


Re: "vertical slices" of tree

Posted by Joseph Kesselman <ke...@us.ibm.com>.



"Siblings" was my first reaction too (the sibling axes and/or
children-of-same-parent). However, if one takes a deeper "slice", nodes at
the same depth may not actually be siblings. For example, see C and E in:

      <a>
            <b>
                  <C/>
            </b>
            <d FOO="bar">
                  <E/>
            </d>
      </a>

If that's what's intended, I'm not sure what the best XPath expression
would be. One approach is "nodes with same number of ancestors as this
one", but you'd need to think about whether the attribute FOO is considered
part of the same slice as C and E.


______________________________________
Joe Kesselman, IBM Next-Generation Web Technologies: XML, XSL and more.
"The world changed profoundly and unpredictably the day Tim Berners Lee
got bitten by a radioactive spider." -- Rafe Culpin, in r.m.filk


Re: "vertical slices" of tree

Posted by Richard Cao <ri...@ca.ibm.com>.



Hi,

The "vertical slices" you are referring to are called siblings. This is
more apparent if the tree structure is written as

              a
             /   \
            b   e
           /       \
         c         f
         /           \
       d             g


To retrieve the siblings of a node, you can refer to the documentation on
the various axes [1].
Here is a simple example to print the siblings of <e>. In this case, we
want to use preceding-sibling and following-sibling.

<xsl:template match="e">
        <xsl:for-each select="preceding-sibling::*" |
following-sibling::*">
                <xsl:value-of select="name(.)"/>
        </xsl:for-each>
</xsl:template>


[1] http://www.w3.org/TR/xpath#axes

Thanks,

Richard Cao
XSLT Development, Toronto IBM Lab
(905) 413-2454 T/L 969-2454
richcao@ca.ibm.com



                                                                           
             Bennett                                                       
             <bennett@glengrou                                             
             p.com>                                                     To 
                                       xalan-j-users@xml.apache.org        
             10/09/2003 04:30                                           cc 
             PM                                                            
                                                                   Subject 
                                       "vertical slices" of tree           
                                                                           
                                                                           
                                                                           
                                                                           
                                                                           
                                                                           




Hello,

Say I have the following tree:



<a>
             <b>
                         <c>
                                     <d/>
                         </c>
             </b>
             <e>
                         <f>
                                     <g/>
                         </f>
             </e>
</a>


If I'm calling this into the stylesheet via document():

             - How do I get the names of all elements on the same "vertical
slice"
that <b> and <e> are on?
             - or the names of all the elements on the same "vertical
slice" as <c>
and <f> ?
             - or the names of all the elements on the same "vertical
slice" as <d>
and <g>?

Note: the names of the elements aren't static so I can't just specify a
static path to anything.

Any ideas?
TIA