Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Error while transforming XSLT to HTML

Posted on 2008-10-17
1
Medium Priority
?
1,224 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 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

AWS Certified Solutions Architect - Associate

This course has been developed to provide you with the requisite knowledge to not only pass the AWS CSA certification exam but also gain the hands-on experience required to become a qualified AWS Solutions architect working in a real-world environment.

Question has a verified solution.

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

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
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…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

722 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