tags:

views:

1245

answers:

2

I'm seeing this exception message coming from XslCompiledTransform.Transform(), but after handling the exception the XSL transform still appears to have been successful. The full exception message is:

Token Text in state EndRootElement would result in an invalid XML document. Make sure that the ConformanceLevel setting is set to ConformanceLevel.Fragment or ConformanceLevel.Auto if you want to write an XML fragment.

The stylesheet looks like this:

    <xsl:stylesheet version="1.0" xmlns:ext="ext:extensions" xmlns:f="http://schemas.foo.com/FOAMSchema"&gt;
      <xsl:template match="/Root/Documents/PO/DROPSHIP">
        <Transactions>
          <Transaction>
            <f:partnerTransmission>
              <transmission_id>
                <xsl:value-of select="ext:NewGUID()"/>
              </transmission_id>
              <partner_code>
                <xsl:value-of select="/Root/@PartnerCode"/>
              </partner_code>
              <control_nbr>
                <xsl:value-of select="@GS_CNTRL_NUM"/>
              </control_nbr>
              <creationTime>
                <xsl:value-of select="ext:ConvertToStandardDateTime(@DATE,@TIME,'ISO8601Basic')"/>
              </creationTime>
              <direction>I</direction>
              <messageCount>
                <xsl:value-of select="count(ORDERS/ORDER)"/>
              </messageCount>
              <syntax>XML</syntax>
              <format>BARBAZ</format>
              <deliveryMethod>FTP</deliveryMethod>
            </f:partnerTransmission>
          </Transaction>
        </Transactions>
      </xsl:template>
    </xsl:stylesheet>

The generated XML looks like this:

<Transactions xmlns="http://schemas.foo.com/IntegrationProfile" xmlns:ext="ext:extensions">
  <Transaction>
    <f:partnerTransmission xmlns:f="http://schemas.foo.com/FOAMSchema"&gt;
      <transmission_id>a5e0ec76-6c24-426b-9eb5-aef9c45d913f</transmission_id>
      <partner_code>VN000033</partner_code>
      <control_nbr>650</control_nbr>
      <creationTime>9/27/2008 12:51:00 AM</creationTime>
      <direction>I</direction>
      <messageCount>2</messageCount>
      <syntax>XML</syntax>
      <format>BARBAZ</format>
      <deliveryMethod>FTP</deliveryMethod>
    </f:partnerTransmission>
  </Transaction>
</Transactions>

The above is what I get when I catch and ignore the exception.

I haven't been able to find a way to set the ConformanceLevel (the property is read-only), but at the same time I also don't think there should be a problem here anyway.

Does my output constitute an XML fragment? Am I missing something in the stylesheet?

A: 

Your output does constitute a well-formed XML fragment. In other words, it looks okay and so does your XSLT.

It seems the error message tries to tell you the following:

Applying this XSLT produces a document that is invalid according to the DTD or Schema, or whatever I am using to validate the output, and my conformanceLevel tells me complain about invalid output. If you do not care about validity, set my conformanceLevel to something less anal.

Note the important difference between "well formed" (a conformant non-validating xml parser can read it) and "valid" (the structure does not follow the grammar specified in a schema).

Also note that it is impossible in XSLT to produce output that is not well-formed XML.

ddaa
At the moment the output namespaces don't have a DTD or XSD or any other formal schema behind them, so it shouldn't be validating against anything at all. Would it be expecting to?
C. Lawrence Wenham
There's no reason it would have to. Yet it seems your xsl processor is performing validation against SOME schema. Otherwise you would not get an "invalid XML document" error.
ddaa
+1  A: 

The exception is trying to tell you that you have attempted to output text after the close element of the root element. The reason your output looks ok is that the exception prevented the invalid XML from being generated.

The reason is simple: you don't have a transform for the root of the document. Therefore, the default transformations are performed. These will output the text content of all elements as text nodes.

Add

<xsl:template match="/">
    <xsl:apply-templates select="/Root/Documents/PO/DROPSHIP"/>
</xsl:template>
John Saunders