Solved

Error while transforming XSLT to HTML

Posted on 2008-10-17
1
1,219 Views
Last Modified: 2013-11-18
My application reads XML and transfors it to HTML using XSLT.  During the XSL transformation I get error.

Error Description - Attribute and namespace nodes cannot be added to the parent element after a text, comment, pi, or sub-element node has already been added.

Here is my XSLT 
********************************************************************
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
      <xsl:output method="xml" indent="yes"/>

      <xsl:template match="/">
            <xsl:apply-templates select="response"/>
      </xsl:template>

      <xsl:template match="response">
            <xsl:copy>
                  <xsl:copy-of select="@*"/>
                  <goals>
                        <xsl:apply-templates select="goals/goal[@achieved='yes']" mode="passed"/>
                        <xsl:apply-templates select="goals/goal[@achieved='no']" mode="failed"/>
                  </goals>
            </xsl:copy>
      </xsl:template>

      <xsl:template match="goal" mode="passed">
            <xsl:copy>
                  <xsl:copy-of select="@*"/>
                  <xsl:copy-of select="conditions"/>
                  <xsl:copy-of select="adjustments"/>
                  <products>
                        <xsl:apply-templates select="products/product">
                              <xsl:sort select="pricing/calcs/@adjusted_rate" order="descending" data-type="number"/>
                        </xsl:apply-templates>
                  </products>
            </xsl:copy>
      </xsl:template>

      <xsl:template match="goal" mode="failed">
            <xsl:copy>
                  <xsl:copy-of select="@*"/>
                  <xsl:copy-of select="returns"/>
            </xsl:copy>
      </xsl:template>

      <xsl:template match="product">
            <xsl:variable name="par_rate" select="@par_rate"/>
            <xsl:copy>
                  <xsl:copy-of select="@*"/>

                  <xsl:choose>
                        <xsl:when test="pricing[@rate=$par_rate]">
          <xsl:copy-of select="pricing[@rate=$par_rate]"/>
                              <xsl:attribute name="pricing-id"><xsl:value-of select="generate-id()"/></xsl:attribute>          
                        </xsl:when>
                        <xsl:when test="count(pricing[not(@*) and out_of_range]) = count(pricing)">
                              <pricing>
                                    <xsl:copy-of select="pricing[1]/calcs"/>
                                    <xsl:attribute name="pricing-id"><xsl:value-of select="generate-id()"/></xsl:attribute>
                                    <out_of_range/>
                              </pricing>
                        </xsl:when>
                        <xsl:when test="count(pricing[@rate=$par_rate]) = 0">
                              <xsl:for-each select="pricing[not(out_of_range)]">
                                    <xsl:copy>
                                          <xsl:copy-of select="@price"/>
                                          <xsl:attribute name="pricing-id"><xsl:value-of select="generate-id()"/></xsl:attribute>
                                          <xsl:copy-of select="calcs"/>
                                          <no_par/>
                                    </xsl:copy>
                              </xsl:for-each>
                        </xsl:when>
                  </xsl:choose>
            </xsl:copy>
      </xsl:template>
</xsl:stylesheet>

***********************************************************************
In this XSLT, if I replace section-1 below with section-2, then it doesnt give any error but it does not display attribute as pricing-id in the Transformed XSLT. Could you suggest anything to fix this issue?

Section-1
=========
<xsl:when test="pricing[@rate=$par_rate]">
          <xsl:copy-of select="pricing[@rate=$par_rate]"/>
                              <xsl:attribute name="pricing-id"><xsl:value-of select="generate-id()"/></xsl:attribute>          
                        </xsl:when>
Section-2
===========
<xsl:when test="pricing[@rate=$par_rate]">
<xsl:attribute name="pricing-id"><xsl:value-of select="generate-id()"/></xsl:attribute>  
      <xsl:copy-of select="pricing[@rate=$par_rate]"/>                                
</xsl:when>

0
Comment
Question by:unifi
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 60

Accepted Solution

by:
Geert Bormans earned 500 total points
ID: 22745263
your error is here I assume

                    <pricing>
                        <xsl:copy-of select="pricing[1]/calcs"/>
                        <xsl:attribute name="pricing-id"><xsl:value-of select="generate-id()"/></xsl:attribute>
                        <out_of_range/>
                    </pricing>

you need to switch the lines where you create the attribute and create the child node
attributes need to come first

                   <pricing>
                        <xsl:attribute name="pricing-id"><xsl:value-of select="generate-id()"/></xsl:attribute>
                        <xsl:copy-of select="pricing[1]/calcs"/>
                        <out_of_range/>
                    </pricing>

0

Featured Post

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

What is Node.js? Node.js is a server side scripting language much like PHP or ASP but is used to implement the complete package of HTTP webserver and application framework. The difference is that Node.js’s execution engine is asynchronous and event…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…
Suggested Courses

617 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question