Solved

Error while transforming XSLT to HTML

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

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.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

705 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now