XSL transforms

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?Utf-8?B?Sm9hY2hpbQ==?=

    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.Do cumentElement.C reateNavigator( );
    XslCompiledTran sform xslt = new XslCompiledTran sform();
    xslt.Load(xsl_d ocument_locatio n);
    xslt.Transform( nav, null, output_stream);

    My XSL code contains some portions of code like this:

    <xsl:element name="img">
    <xsl:attribut e name="class">dr awing</xsl:attribute>
    <xsl:attribut e name="width">10 0%</xsl:attribute>
    <xsl:attribut e name="src">
    <xsl:value-of select="Folder" />
    <xsl:text disable-output-escaping="yes">/</xsl:text>
    <xsl:value-of select="Filenam e" />
    </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?
  • Arto Viitanen

    #2
    Re: XSL transforms

    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.Do cumentElement.C reateNavigator( );
    XslCompiledTran sform xslt = new XslCompiledTran sform();
    xslt.Load(xsl_d ocument_locatio n);
    xslt.Transform( nav, null, output_stream);
    >
    My XSL code contains some portions of code like this:
    >
    <xsl:element name="img">
    <xsl:attribut e name="class">dr awing</xsl:attribute>
    <xsl:attribut e name="width">10 0%</xsl:attribute>
    <xsl:attribut e name="src">
    <xsl:value-of select="Folder" />
    <xsl:text disable-output-escaping="yes">/</xsl:text>
    <xsl:value-of select="Filenam e" />
    </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"><x sl:text
    disable-output-escaping="yes">/</xsl:text></xsl:variable>
    <xsl:element name="img">
    <xsl:attribut e name="class">dr awing</xsl:attribute>
    <xsl:attribut e name="width">10 0%</xsl:attribute>
    <xsl:attribut e name="src">
    <xsl:value-of select="Folder" />
    <xsl:value-of select="$slash" />
    <xsl:value-of select="Filenam e" />
    </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

    Comment

    • Pavel Minaev

      #3
      Re: XSL transforms

      On Jul 7, 11:41 pm, Joachim <Joac...@discus sions.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.Do cumentElement.C reateNavigator( );
                  XslCompiledTran sform xslt = new XslCompiledTran sform();
                  xslt.Load(xsl_d ocument_locatio n);
                  xslt.Transform( nav, null, output_stream);
      >
      My XSL code contains some portions of code like this:
      >
              <xsl:element name="img">
                <xsl:attribut e name="class">dr awing</xsl:attribute>
                <xsl:attribut e name="width">10 0%</xsl:attribute>
                <xsl:attribut e name="src">
                  <xsl:value-of select="Folder" />
                  <xsl:text disable-output-escaping="yes">/</xsl:text>
                  <xsl:value-of select="Filenam e" />
                </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.

      Comment

      • Marc Gravell

        #4
        Re: XSL transforms

        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

        Comment

        • Marc Gravell

          #5
          Re: XSL transforms

          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

          Comment

          • GArlington

            #6
            Re: XSL transforms

            On Jul 7, 8:41 pm, Joachim <Joac...@discus sions.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.Do cumentElement.C reateNavigator( );
            XslCompiledTran sform xslt = new XslCompiledTran sform();
            xslt.Load(xsl_d ocument_locatio n);
            xslt.Transform( nav, null, output_stream);
            >
            My XSL code contains some portions of code like this:
            >
            <xsl:element name="img">
            <xsl:attribut e name="class">dr awing</xsl:attribute>
            <xsl:attribut e name="width">10 0%</xsl:attribute>
            <xsl:attribut e name="src">
            <xsl:value-of select="Folder" />
            <xsl:text disable-output-escaping="yes">/</xsl:text>
            <xsl:value-of select="Filenam e" />
            </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:attribut e name="src"IS a complex type...
            This may be the cause of the error...

            Comment

            • Marc Gravell

              #7
              Re: XSL transforms

              xsl:attribute is defined by xslt itself; so as long as xsl points to the
              right namespace this shouldn't be an issue.

              Marc


              Comment

              • Pavel Minaev

                #8
                Re: XSL transforms

                On Jul 8, 10:26 am, Marc Gravell <marc.grav...@g mail.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
                XslCompiledTran sform follows the spec here.

                It would be pretty tricky to write an attribute after a child element
                to an XmlWriter, anyway...

                Comment

                • Pavel Minaev

                  #9
                  Re: XSL transforms

                  On Jul 8, 3:07 pm, GArlington <garling...@tis cali.co.ukwrote :
                  AFAIR you need to define complex types BEFORE you use them.
                  In your case <xsl:attribut e name="src"IS a complex type...
                  This may be the cause of the error...
                  <xsl:attributei s 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).

                  Comment

                  • Marc Gravell

                    #10
                    Re: XSL transforms

                    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


                    Comment

                    • Marc Gravell

                      #11
                      Re: XSL transforms

                      <xsl:attributei s 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:complexTy pe name="attribute " mixed="true">
                      <xs:choice minOccurs="0" maxOccurs="unbo unded">
                      <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 "instructio ns" and "attribute-set" groups via:
                      <xs:element name="attribute " type="attribute "/>


                      Comment

                      • Marc Gravell

                        #12
                        Re: XSL transforms

                        "schema for xsd"

                        I meant schema for xsl... too many TLAs!


                        Comment

                        Working...