Re: [xsl] copying attributes

Subject: Re: [xsl] copying attributes
From: Edierley Messias <edierley@xxxxxxxxxxx>
Date: Tue, 9 Jan 2001 00:42:43 -0200 (EDT)
Hi Russ,

I think that this .xsl maybe help, it works with any kind of nodes like
<last_year>, <last10year>, <90s>, <80s> or <today> for example:

=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=
<xsl:stylesheet
version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/">
  <z>
    <xsl:attribute name="breakfast">bacon</xsl:attribute>
    <xsl:for-each select="//z[@breakfast='bacon']">
      <xsl:attribute name="{concat(name(parent::*),'_servings')}">
        <xsl:value-of select="@servings"/>
      </xsl:attribute>
    </xsl:for-each>
  </z>
  <z>
    <xsl:attribute name="breakfast">eggs</xsl:attribute>
    <xsl:for-each select="//z[@breakfast='eggs']">
      <xsl:attribute name="{concat(name(parent::*),'_servings')}">
        <xsl:value-of select="@servings"/>
      </xsl:attribute>
    </xsl:for-each>
  </z>
   <z>
    <xsl:attribute name="breakfast">cereal</xsl:attribute>
    <xsl:for-each select="//z[@breakfast='cereal']">
      <xsl:attribute name="{concat(name(parent::*),'_servings')}">
        <xsl:value-of select="@servings"/>
      </xsl:attribute>
    </xsl:for-each>
  </z>
</xsl:template>
</xsl:stylesheet>

=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=

I tested with your .xml file and the results was:
 <?xml version="1.0" encoding="ISO-8859-1"?>
 <z breakfast="bacon" life_to_date_servings="2000" y2000_servings="130"/>
 <z breakfast="eggs" life_to_date_servings="2000" y2000_servings="100" january_servings="1"/>
 <z breakfast="cereal" life_to_date_servings="7000"/>

=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=

I tried to optmize the code to deal with any kind of food, but still not
work 100%, the code and the results are below: 

<xsl:stylesheet
version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
<xsl:variable name='foods'/>
<xsl:output method="xml" indent="yes"/>

<xsl:template match="/">

  <xsl:for-each select="//z[@breakfast]">

    <xsl:variable name='food'>
      <xsl:value-of select="@breakfast" />
    </xsl:variable>
    
    <xsl:if test="not(contains($foods,$food))">
      <xsl:variable name='foods'>
        <xsl:value-of select = "concat($foods,'|',$food)" />
      </xsl:variable>
      
      <z>
        <xsl:attribute name="breakfast"><xsl:value-of
select="$food"/></xsl:attribute>
        <!--<xsl:attribute name="foods"><xsl:value-of
select="$foods"/></xsl:attribute>-->
        <!--<xsl:attribute name="actualnode"><xsl:value-of
select="name(parent::*)"/></xsl:attribute>-->
        <xsl:for-each select="//z[@breakfast=$food]">
          <xsl:attribute name="{concat(name(parent::*),'_servings')}">
            <xsl:value-of select="@servings"/>
          </xsl:attribute>
        </xsl:for-each>
      </z>
    </xsl:if>
  </xsl:for-each>
</xsl:template>

</xsl:stylesheet>

=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=

The results are:
<z breakfast="bacon" life_to_date_servings="2000" y2000_servings="130"/>
<z breakfast="eggs" life_to_date_servings="2000" y2000_servings="100" january_servings="1"/>
<z breakfast="cereal" life_to_date_servings="7000"/>
<z breakfast="bacon" life_to_date_servings="2000" y2000_servings="130"/>
<z breakfast="eggs" life_to_date_servings="2000" y2000_servings="100" january_servings="1"/>
<z breakfast="eggs" life_to_date_servings="2000" y2000_servings="100" january_servings="1"/>

The elements with same breakfast are still repeating, because the
xsl:variable called $foods cannot be updated.

I'm think in the problem, I hope that helps.
Any suggestions how to fix this problem people?

Thanks
_________________________
Edierley Messias
www.dcc.ufmg.br/~edierley
icq 32943484
=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=






>When converting the result of three SQL queries to xml I generate something
>like;
>
><breakfasts>
>
>    <life_to_date>
>        <z breakfast="bacon"     servings="2000"/>
>        <z breakfast="eggs"       servings="2000"/>
>        <z breakfast="cereal"     servings="7000"/>
>    </life_to_date>
>
>    <y2000>
>        <z breakfast="bacon"   servings="130">
>        <z breakfast="eggs"     servings="100">
>    </y2000>
>
>    <january>
>        <z breakfast="eggs"     servings="1">
>    </january>
>
></breakfasts>
>
>
>..you can assume that any 'breakfast' types in the y2000 and january nodes
>will show in the life_to_date node..
>
>I would like to load this xml into a Data Source Object using the following
>format;
>
><z breakfast="bacon"    january_servings=""      y2000_servings="130"
>life_to_date_servings="2000"/>
><z breakfast="eggs"     january_servings="1"     y2000_servings="100"
>life_to_date_servings="2000"/>
><z breakfast="cereal"   january_servings=""       y2000_servings=""
>life_to_date_servings="7000"/>
>
>Can I transform into this format?  If so how?
>
>In trying to create the 'january_servings' node I've tried;
>
><xsl:template match="/breakfasts/life_to_date/z">
>  <z>
>    <xsl:for-each select="@breakfast" >
>    <xsl:attribute name="breakfast"><xsl:value-of
>select="."/></xsl:attribute>
>    <xsl:attribute name="january_servings"><xsl:value-of
>select="/breakfasts/january[@breakfast=.]/@servings"/>
>    </xsl:attribute>
>    </xsl:for-each>
></z>
></xsl:template>
>
>But with no success...could anyone help me out here? I'm trying to say for
>each life_to_date/z node, iterate through the 'breakfast' attributes and
>create new attributes with values that match the value of the corresponding
>'january' node..where the 'breakfast' attibutes match..
>
>Thanks heaps,
>
>Russ
>
>
> XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
>
>


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


Current Thread