From: Joachim on
I have a XML file with belonging XSL file. Opening the XML file in IE7 works
just fine. But when using the following code I get the error message:

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

XPathNavigator nav =
xml_document.DocumentElement.CreateNavigator();
XslCompiledTransform xslt = new XslCompiledTransform();
xslt.Load(xsl_document_location);
xslt.Transform(nav, null, output_stream);

My XSL code contains some portions of code like this:

<xsl:element name="img">
<xsl:attribute name="class">drawing</xsl:attribute>
<xsl:attribute name="width">100%</xsl:attribute>
<xsl:attribute name="src">
<xsl:value-of select="Folder" />
<xsl:text disable-output-escaping="yes">/</xsl:text>
<xsl:value-of select="Filename" />
</xsl:attribute>
</xsl:element>

A thread I found
(http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=627581&SiteID=1) says
that attribute nodes should be created before element nodes. This is find
very strange since the attribute is a part of the element, not vice versa.
Anyone?
From: Arto Viitanen on
Joachim kirjoitti:
> I have a XML file with belonging XSL file. Opening the XML file in IE7 works
> just fine. But when using the following code I get the error message:
>
> "Attribute and namespace nodes cannot be added to the parent element after a
> text, comment, pi, or sub-element node has already been added."
>
> XPathNavigator nav =
> xml_document.DocumentElement.CreateNavigator();
> XslCompiledTransform xslt = new XslCompiledTransform();
> xslt.Load(xsl_document_location);
> xslt.Transform(nav, null, output_stream);
>
> My XSL code contains some portions of code like this:
>
> <xsl:element name="img">
> <xsl:attribute name="class">drawing</xsl:attribute>
> <xsl:attribute name="width">100%</xsl:attribute>
> <xsl:attribute name="src">
> <xsl:value-of select="Folder" />
> <xsl:text disable-output-escaping="yes">/</xsl:text>
> <xsl:value-of select="Filename" />
> </xsl:attribute>
> </xsl:element>
>

xsl:attribute creates new attribute. xsl:text appends text to the
element. So xsl:text adds / to the <img> element, and
the end of the src attribute causes the error message.

I'd try

<xsl:variable name="slash"><xsl:text
disable-output-escaping="yes">/</xsl:text></xsl:variable>
<xsl:element name="img">
<xsl:attribute name="class">drawing</xsl:attribute>
<xsl:attribute name="width">100%</xsl:attribute>
<xsl:attribute name="src">
<xsl:value-of select="Folder" />
<xsl:value-of select="$slash" />
<xsl:value-of select="Filename" />
</xsl:attribute>
</xsl:element>

instead.

> A thread I found
> (http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=627581&SiteID=1) says
> that attribute nodes should be created before element nodes. This is find
> very strange since the attribute is a part of the element, not vice versa.

I guess it means, that subelements should be added after the attributes
are added.
> Anyone?

--
Arto Viitanen
From: Pavel Minaev on
On Jul 7, 11:41 pm, Joachim <Joac...(a)discussions.microsoft.com> wrote:

> I have a XML file with belonging XSL file. Opening the XML file in IE7 works
> just fine. But when using the following code I get the error message:
>
> "Attribute and namespace nodes cannot be added to the parent element after a
> text, comment, pi, or sub-element node has already been added."
>
>             XPathNavigator nav =
> xml_document.DocumentElement.CreateNavigator();
>             XslCompiledTransform xslt = new XslCompiledTransform();
>             xslt.Load(xsl_document_location);
>             xslt.Transform(nav, null, output_stream);
>
> My XSL code contains some portions of code like this:
>
>         <xsl:element name="img">
>           <xsl:attribute name="class">drawing</xsl:attribute>
>           <xsl:attribute name="width">100%</xsl:attribute>
>           <xsl:attribute name="src">
>             <xsl:value-of select="Folder" />
>             <xsl:text disable-output-escaping="yes">/</xsl:text>
>             <xsl:value-of select="Filename" />
>           </xsl:attribute>
>         </xsl:element>
>
> A thread I found
> (http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=627581&SiteID=1) says
> that attribute nodes should be created before element nodes. This is find
> very strange since the attribute is a part of the element, not vice versa..

Actually, what this means is that attribute nodes for a given element
must be created before the child element nodes for that element. The
particular piece of code that you've posted doesn't have this problem,
but, apparently, some other one does. To quote the XSLT W3C
Recommendation (http://www.w3.org/TR/xslt#creating-attributes):

"The following are all errors:

- Adding an attribute to an element after children have been added to
it; implementations may either signal the error or ignore the
attribute."

IE XSLT implementation seems to be lax in that regard, probably
because it works with mutable DOM rather than forward-only writers,
and so the restriction doesn't make as much sense there.
From: Marc Gravell on
attributes definitely follow elements in xslt - and I'm not convinced
that the error message relates to that particular element, but make
life simple; try something like:

<img class="drawing" width="100%" src="{Folder}/{Filename}"/>

Marc

From: Marc Gravell on
Note I've posted an example (showing no problems in the above) in the
managed group. But note also the simpler syntax <img ... src="{Folder}/
{Filename}"/> is preferable.

Marc