CBC_NS CBC_NS - 1 month ago 9
Vb.net Question

Error: Attribute and namespace nodes cannot be added to the parent element

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()
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">
<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>
<!-- 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"/>


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.