473,387 Members | 1,592 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

XSL transforms

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/Sho...27581&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?
Jul 7 '08 #1
11 2439
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 <imgelement, 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/Sho...27581&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
Jul 8 '08 #2
On Jul 7, 11:41*pm, Joachim <Joac...@discussions.microsoft.comwrote:
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/Sho...27581&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.
Jul 8 '08 #3
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

Jul 8 '08 #4
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
Jul 8 '08 #5
On Jul 7, 8:41 pm, Joachim <Joac...@discussions.microsoft.comwrote:
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/Sho...27581&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?
AFAIR you need to define complex types BEFORE you use them.
In your case <xsl:attribute name="src"IS a complex type...
This may be the cause of the error...
Jul 8 '08 #6
xsl:attribute is defined by xslt itself; so as long as xsl points to the
right namespace this shouldn't be an issue.

Marc
Jul 8 '08 #7
On Jul 8, 10:26*am, Marc Gravell <marc.grav...@gmail.comwrote:
attributes definitely follow elements in xslt
They do not. I've quoted the XSLT specification which says, in plain
English, that it is not allowed to add any attributes after a child
element has been added. An XSLT processor may still allow it as an
extension, but it is not standard XSLT, and it would seem that
XslCompiledTransform follows the spec here.

It would be pretty tricky to write an attribute after a child element
to an XmlWriter, anyway...
Jul 10 '08 #8
On Jul 8, 3:07*pm, GArlington <garling...@tiscali.co.ukwrote:
AFAIR you need to define complex types BEFORE you use them.
In your case <xsl:attribute name="src"IS a complex type...
This may be the cause of the error...
<xsl:attributeis not a type at all, and @src attribute should have
type xs:anyURI, which is not complex (but it is irrelevant here
anyway, since no type is being referenced in the XSLT snippet posted).
Jul 10 '08 #9
Yes, but if you consider the context of the OP's remark:
<q>
A thread I found [snip]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?
</q>
It seems clear to me that the OP is referring to the *containing* element.
And the attribute declarations definitely follow the *containing* element
declaration. If my phrasing was unfortunate then sorry for confusion; but I
was referring to the OPs remark. Yes, attribute declarations preceed other
*child* content nodes, but that isn't what I was referring to.

Either way, it is *still* simpler to use the inline syntax <img
src="{$foo}"/;-p

Marc
Jul 10 '08 #10
<xsl:attributeis not a type at all

It is if you have a schema for xsd, such as xslt.xsd that ships with VS,
which defines:

<xs:complexType name="attribute" mixed="true">
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:group ref="char-instructions"/>
</xs:choice>
<xs:attribute name="name" type="xs:string" use="required" />
<xs:attribute name="namespace" type="xs:string" />
<xs:attribute ref="xml:space" />
</xs:complexType>

and uses it in the "instructions" and "attribute-set" groups via:
<xs:element name="attribute" type="attribute"/>
Jul 10 '08 #11
"schema for xsd"

I meant schema for xsl... too many TLAs!
Jul 10 '08 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Matthew | last post by:
Hello, For a few days now, I have tried a number of methods that are supposed to provide the a(k) and b(k) coefficients for a Fourier series. These methods are listed at the end of this post (in...
0
by: Paul Graham | last post by:
Hi All, New to Java2D and am trying to do something I know should be relatively simple but isn't turning out to be. I want to draw intersecting rectangles across an arbitrary shape that can...
4
by: Ravi | last post by:
My XML looks something like: <one> .... <two>A</two> <two>B</two> <two>C</two> <two>D</two> .... </one>
3
by: rush | last post by:
I have a DTD that defines new elements "mytextfield" and "mysn", and does it as an extension to XHTML. The idea is that my XML markup is actually valid XHTML according to my DTD. This all works...
2
by: sneill | last post by:
Hello all, In the August 2003 edition of "XML Journal" (see: http://xml.sys-con.com/read/40671.htm) Pietro Michelucci wrote an interesting article entitled "Object-Oriented XSLT". The article...
1
by: rocio | last post by:
I'm trying ti use the example in http://support.microsoft.com/default.aspx?scid=kb;en-us;330587#appliesto but doe s not work. I keep getting the error Public WriteOnly Property XmlResolver()...
6
by: Peter Oliphant | last post by:
I'm working with Graphics transforms (i.e., RotateTranform, TranslateTransform and ScaleTransform ). Are these applied to the WORLD (graphics container) before drawing into that world (graphics...
13
by: Ragnar | last post by:
Hi, 2 issues left with my tidy-work: 1) Tidy transforms a "&amp;" in the source-xml into a "&" in the tidied version. My XML-Importer cannot handle it 2) in a long <title>-string a wrap is...
4
by: sadc1986 | last post by:
How does one introduce multiple worksheets in a excel using Xslt Transforms my code... Please suggest <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"...
0
by: VicJ | last post by:
I'm trying to make a control on which the user can freely drag an image with the mouse and zoom and rotate it with the mouse wheel. I'd like to do this as far as possible using graphics transforms....
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.