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

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

尝试进行转换时出现以下错误:

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

下面是我用来进行转换的函数

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

下面是我的 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>

问题: 如错误所示,"text, comment, pi, or sub-element node" 可能不会介于 xsl:attribute 和接收属性的元素之间。

解决方案:xsl:attribute 语句上移 立即 td 元素下方希望添加属性。