Re: [xsl] Re: Re: Re: Converting specific child elements into att riutes of parent

Subject: Re: [xsl] Re: Re: Re: Converting specific child elements into att riutes of parent
From: Jeni Tennison <jeni@xxxxxxxxxxxxxxxx>
Date: Mon, 3 Nov 2003 14:18:34 +0000
Hi Ganesh,

> Still i am not able to come out of this loop.  This is really frustrating.
> Now I tried with XalanJ 2.5.0 and XalanJ 2.5.2 with the classpath set to
> these version's jar file using -Xbootclasspath/p(exactly how its described
> in Xalan-J FAQ at:  http://xml.apache.org/xalan-j/faq.html#faq-N100CB &
> http://xml.apache.org/xalan-j/faq.html#faq-N100B4).  Now the earlier error
> gone
>
> file:///C:/working/XML/elmToAtt.xsl; Line #31; Column #5; Cannot add
> attribute id after child nodes or before an element is produced.  Attribute
> will be ignored.
> file:///C:/working/XML/elmToAtt.xsl; Line #31; Column #5; Cannot add
> attribute id after child nodes or before an element is produced.  Attribute
> will be ignored. 
>
> However its producing the required output when I run this within the
> application. Now i need to eliminate this error. Anybody got clue on
> this ?

These are reports of errors in your stylesheet. You are currently
creating an attribute and adding it to an element after you've already
added children to that element, and that's not allowed in XSLT. The
cause is the <xsl:apply-templates> instruction that applies templates
to all the children of the element, when some of the children might
cause the generation of attributes and others the copying of elements.

Try the following stylesheet, which should fix the problem:

<xsl:stylesheet version="1.0"
   xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
   xmlns:my="my:my"
   exclude-result-prefixes="my"
   >

   <xsl:output omit-xml-declaration="yes" indent="yes"/>
   <xsl:strip-space elements="*"/>

   <my:elNames>
      <name>id</name>
   </my:elNames>

   <xsl:variable name="elNames"
      select="document('')/*/my:elNames/name"/>

   <xsl:template match="@* | node()">
      <xsl:copy>
         <xsl:apply-templates select="@* | node()"/>
      </xsl:copy>
   </xsl:template>

   <xsl:template match="*">
      <xsl:choose>
         <xsl:when test="not(name() = $elNames)">
            <xsl:copy>
               <xsl:copy-of select="@*"/>
               <xsl:apply-templates select="*[name() = $elNames]"/>
               <xsl:apply-templates
                 select="node()[not(name() = $elNames)]"/>
            </xsl:copy>
         </xsl:when>
         <xsl:otherwise>
            <xsl:attribute name="{name()}">
               <xsl:value-of select="."/>
            </xsl:attribute>
         </xsl:otherwise>
      </xsl:choose>
   </xsl:template>

   <xsl:template match="value">
      <xsl:value-of select="."/>
   </xsl:template>
</xsl:stylesheet>

Cheers,

Jeni

---
Jeni Tennison
http://www.jenitennison.com/


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


Current Thread