xsl-list
[Top] [All Lists]

RE: [xsl] XSL/XPath to generate a list of ancestors?

2008-05-12 10:23:38
I didn't fully read your requirements the first time around, but this
really should be fairly straightforward.

<xsl:template match="*">
  <fullName>
    <xsl:for-each select="ancestor-or-self::*[parent::*]">
      <xsl:value-of select="@name" />
      <xsl:if test="position() != last()">.</xsl:if>
    </xsl:for-each>
  </fullName>
</xsl:template>

~ Scott


-----Original Message-----
From: Scott Trenda [mailto:Scott(_dot_)Trenda(_at_)oati(_dot_)net] 
Sent: Monday, May 12, 2008 11:00 AM
To: xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com
Subject: RE: [xsl] XSL/XPath to generate a list of ancestors?

<xsl:for-each select="ancestor::*">
  <xsl:value-of select="name()" />
</xsl:for-each>

~ Scott


-----Original Message-----
From: Nathan Potter [mailto:ndp(_at_)coas(_dot_)oregonstate(_dot_)edu] 
Sent: Monday, May 12, 2008 10:52 AM
To: xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com
Cc: Nathan Potter
Subject: [xsl] XSL/XPath to generate a list of ancestors?


I need to concatenate the "name" attributes of all of the parents for  
each element. All I could figure out was to use a recursive template.  
Is there a more straightforward way to accomplish this?



XML:

<Dataset name="root">
     <A name="a1">
         <A name="a2">
             <A name="a3" />
         </A>
     </A>
     <B name="b1">
         <B name="b2"/>
     </B>
</Dataset>

Desired output:

<fullName>a1</fullName>
<fullName>a1.a2</fullName>
<fullName>a1.a2.a3</fullName>

<fullName>b1</fullName>
<fullName>b1.b2</fullName>


XSL:

     <xsl:template name="fullName">
         <fullName>
             <xsl:call-template name="fullNameWorker" />
         </fullName >
     </xsl:template>

     <xsl:template match="*" name="fullNameWorker" mode="fullName">
         <xsl:if test=".!=/">
             <xsl:apply-templates select=".." mode="fullName"/>
             <xsl:if test="..!=/">.</xsl:if>
             <xsl:value-of select="@name"/>
         </xsl:if>
     </xsl:template>

============================================================
Nathan Potter                 Oregon State University, COAS
ndp at coas.oregonstate.edu   104 Ocean. Admin. Bldg.
541 737 2293 voice            Corvallis, OR   97331-5503
541 737 2064 fax



--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--


--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--


--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--