3

I'm getting the following error when trying to do a transform:

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

Below is the function I'm using to do the transform

Public Function Transform(ByVal doc As XmlNode, ByVal stylesheet As XmlDocument) As String
    Dim trans As XslCompiledTransform = New XslCompiledTransform()
    trans.Load(stylesheet)
    Dim settings As XmlWriterSettings = New XmlWriterSettings()
    settings.OmitXmlDeclaration = False
    settings.ConformanceLevel = ConformanceLevel.Fragment
    settings.CloseOutput = False

    Dim writer As System.IO.StringWriter = New System.IO.StringWriter()
    trans.Transform(doc, XmlWriter.Create(writer, settings))

    Return writer.ToString()
End Function

Below is the offending code in my xsl

<xsl:template name="Calendar">
    <xsl:variable name="dateRef"><xsl:value-of select="@dateRef"/></xsl:variable>
        <table border="0" cellpadding="0" cellspacing="0">
            <tr>
                <td style="padding-top: 10px">
                        <span style="position:absolute; display:none" fieldName="{//Form/@name}.{Name}" initialDate="{@initialDate}" futureBound="{@futureBound}" pastBound="{@pastBound}">
                            <xsl:attribute name="ID">date<xsl:value-of select="$dateRef"/></xsl:attribute>
                        </span>
                        <!-- When I comment out the line below my page loads, but the intended content doesn't -->
                        <xsl:attribute name="ID">date<xsl:value-of select="$dateRef"/></xsl:attribute>
                        <xsl:call-template name="calendarContents"/>
                </td>
            </tr>
        </table>
</xsl:template>
kjhughes
  • 106,133
  • 27
  • 181
  • 240
CBC_NS
  • 1,961
  • 4
  • 27
  • 47

1 Answers1

5

Problem: As the error indicates, a "text, comment, pi, or sub-element node" may not intervene between xsl:attribute and the element receiving the attribute.

Solution: Move the xsl:attribute statement up to be immediately beneath the td element to which you wish to add the attribute.

kjhughes
  • 106,133
  • 27
  • 181
  • 240