[xsl] Re: Re: Change of Attribute Value

Subject: [xsl] Re: Re: Change of Attribute Value
From: "Miraodb" <miraodb@xxxxxxxxxxx>
Date: Fri, 4 Nov 2005 09:13:39 +0100
Hi ragulf,

Thanks for your prompt reply.
Im' having trouble with your code, it stops at the <xsl:copy-of
select="@*[not(self::@name)]" /> telling me that it's expecting a node test
in xsl:copy-of....

doesn't really makes sense to me.

from what i understand of your solution, it would work fine as long as
there's only three levels. the problem is that there can be as many level as
possible and also that the element node isn't the actual root of the tree.

anyway, i found another solution last night and i was thinking i could post
it here if somebody find it useful. I use recursion to loop into all the
nodes and check the level. if the level is greater than 1 (starting from 0)
it means i'm on the sub-sub-node element and therefore for all sub nodes i
needs to concat the attribute value of the parent node.

here it is:

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
version="1.0">
<xsl:output omit-xml-declaration="yes" method="xml" indent="yes" />

 <xsl:template match="/">
  <xsl:call-template name="port_root" />
 </xsl:template>

 <xsl:template name="port_root">
  <xsl:element name="portfolio">
   <xsl:attribute name="name"><xsl:value-of select="/portfolio/@name"
/></xsl:attribute>
   <xsl:attribute name="description"><xsl:value-of
select="/portfolio/@description" /></xsl:attribute>
   <xsl:copy-of select="/portfolio/*[not(self::node)]" />
   <xsl:call-template name="parentnode">
    <xsl:with-param name="currentnode" select="/portfolio/node" />
    <xsl:with-param name="level" select="0" />
    <xsl:with-param name="prefix" select="" />
   </xsl:call-template>
  </xsl:element>
 </xsl:template>

 <xsl:template name="parentnode">
  <xsl:param name="currentnode" />
  <xsl:param name="level" />
  <xsl:param name="prefix" />

  <xsl:element name="node">
   <xsl:choose>
   <xsl:when test="$prefix=''">
    <xsl:attribute name="name"><xsl:value-of select="$currentnode/@name"
/></xsl:attribute>
   </xsl:when>
   <xsl:otherwise>
    <xsl:attribute name="name"><xsl:value-of
select="concat($prefix,'_',$currentnode/@name)" /></xsl:attribute>
   </xsl:otherwise>
   </xsl:choose>

   <xsl:attribute name="description"><xsl:value-of
select="$currentnode/@description" /></xsl:attribute>
   <xsl:copy-of select="$currentnode/*[not(self::node)]" />
   <xsl:for-each select="$currentnode/node">
    <xsl:choose>
     <xsl:when test="$level = 1">
      <xsl:call-template name="parentnode">
       <xsl:with-param name="prefix" select="$currentnode/@name" />
       <xsl:with-param name="level" select="$level + 1" />
       <xsl:with-param name="currentnode" select="." />
      </xsl:call-template>
     </xsl:when>
     <xsl:when test="$level &gt; 1">
      <xsl:call-template name="parentnode">
       <xsl:with-param name="prefix" select="$prefix" />
       <xsl:with-param name="level" select="$level + 1" />
       <xsl:with-param name="currentnode" select="." />
      </xsl:call-template>
     </xsl:when>
     <xsl:otherwise>
      <xsl:call-template name="parentnode">
       <xsl:with-param name="prefix" select="" />
       <xsl:with-param name="level" select="$level + 1" />
       <xsl:with-param name="currentnode" select="." />
      </xsl:call-template>
     </xsl:otherwise>
    </xsl:choose>
   </xsl:for-each>
  </xsl:element>

 </xsl:template>

</xsl:stylesheet>

Hope this can be a use for someone else.

Cheers and thx for your help.

Fabrice

> Date: Thu, 3 Nov 2005 11:42:03 +0100
> To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
> From: Ragulf Pickaxe <ragulf.pickaxe@xxxxxxxxx>
> Subject: Re: [xsl] Change of Attribute Value
> Message-ID: <9ca1ac670511030242w6878fcc0n2878a50d77d9b63a@xxxxxxxxxxxxxx>
>
> Hi fabrice,
>
> This is just a very quick solution from the top of my head. Not
> tested, and I am a little sure of the copy-of on the remaining
> attributes - the test for not choosing the name. Another one might
> help you out here.
>
> <xsl:template match=3D"node">
>   <xsl:copy>
>     <xsl:attribute
> name=3D"name">concat((ancestor::node)[2]/@name,@name"</xsl:attribute>
>     <xsl:copy-of select=3D"@*[not(self::@name)]"/>
>     <xsl:apply-templates/>
>   </xsl:copy>
> </xsl:template>
>
> <xsl:template match=3D"*[not(self::node)]">
>   <xsl:copy-of select=3D"."/>
> </xsl:template>
>
> Regards,
> Ragulf Pickaxe :-)

Current Thread