[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: Re: Re: Re: Converting specific child elements int

Subject: Re: 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
xpath select child nodes
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

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.