Solved

Error while transforming XSLT to HTML

Posted on 2008-10-17
1
1,218 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

Suggested Solutions

I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
Styling your websites can become very complex. Here I'll show how SASS can help you better organize, maintain and reuse your CSS code.
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.

739 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