Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Error while transforming XSLT to HTML

Posted on 2008-10-17
1
Medium Priority
?
1,226 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
1 Comment
 
LVL 60

Accepted Solution

by:
Geert Bormans earned 2000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Preface This is the third article about the EE Collaborative Login Project. A Better Website Login System (http://www.experts-exchange.com/A_2902.html) introduces the Login System and shows how to implement a login page. The EE Collaborative Logi…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).
Suggested Courses

916 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