Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XSLT transforming is throwing error

Tags:

xslt

I've xml as below,

<?xml version="1.0" encoding="utf-16" ?> 
 <AllResidentAndUnitInfo xmlns:i="http://www.w3.org/2001/XMLSchema-instance" 
 i:type="ResidentsByUnitInfo" xmlns="http://schemas.datacontract.org/2004/07/FSRSchema">
    <BillingAddresses>
      <BillingAddress>
        <billing_address1>Some address</billing_address1> 
        <billing_address2 /> 
        <billing_city>Gilbert</billing_city> 
        <billing_country i:nil="true"/> 
        <billing_dtmmodified>2010-12-08T01:37:41+05:30</billing_dtmmodified> 
        <billing_state>AZ</billing_state> 
        <billing_zipcode>23233</billing_zipcode>            
      </BillingAddress>
      <BillingAddress>
       <ResidentsByUnitInfoPropertyUnitBillingAddress>
        <billing_address1>Some address</billing_address1> 
        <billing_address2 /> 
        <billing_city>Gilbert</billing_city> 
        <billing_country i:nil="true"/> 
        <billing_dtmmodified>2010-12-08T01:37:41+05:30</billing_dtmmodified> 
        <billing_state>AZ</billing_state> 
        <billing_zipcode>23233</billing_zipcode> 
       </ResidentsByUnitInfoPropertyUnitBillingAddress>
      </BillingAddress>
      ....

</AllResidentAndUnitInfo>

I'm transforming into another xml format in C# using the XslCompiledTransform,

<?xml version='1.0' ?>
<xsl:stylesheet xmlns:xsl='http://www.w3.org/1999/XSL/Transform'
   xmlns:msxsl='urn:schemas-microsoft-com:xslt'
   xmlns:i='http://www.w3.org/2001/XMLSchema-instance' exclude-result-prefixes='msxsl  
   i' version='1.0'>
<xsl:output method='xml' indent='yes'/>
<xsl:template match='/AllResidentAndUnitInfo/BillingAddresses/BillingAddress'>
    <Root>
      <Address1>..</Address2>   
              ...
    </Root>
</xsl:template>
  </xsl:stylesheet>

I'm getting the error "Token Text in state Start 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." I understood the problem is with the i:nil attributes in the xml. Eventhough I included the namespace of them in XSLT still i'm getting the error.

like image 346
VJAI Avatar asked Oct 03 '11 11:10

VJAI


People also ask

Does anyone use XSLT anymore?

XSLT is very widely used. As far as we can judge from metrics like the number of StackOverflow questions, it is in the top 30 programming languages, which probably makes it the top data-model-specific programming language after SQL. But XSLT isn't widely used client-side, that is, in the browser.

What is Number () in XSLT?

Specifies the format pattern. Here are some of the characters used in the formatting pattern: 0 (Digit)

Can XSLT transform XML to CSV?

This post shows you how to convert a simple XML file to CSV using XSLT. The following XSL Style Sheet (compatible with XSLT 1.0) can be used to transform the XML into CSV. It is quite generic and can easily be configured to handle different xml elements by changing the list of fields defined ar the beginning.


1 Answers

I'm getting the error "Token Text in state Start 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." I understood the problem is with the i:nil attributes in the xml. Eventhough I included the namespace of them in XSLT still i'm getting the error.

No. The problem is that the result isn't a well-formed XML document and thus the XmlWriter, involved in producing the final serialization of the result tree to text, raises this exception.

Really, in your result you have two Root elements and none of them has a parent element.

You need to produce a well-formed document, or change the ConformanceLevel setting for the XmlWriter to ConformanceLevel.Fragment or ConformanceLevel.Auto.

To create a wellformed output, just add:

<xsl:template match="/">
 <top>
   <xsl:apply-templates/>
 </top>
</xsl:template>
like image 197
Dimitre Novatchev Avatar answered Sep 30 '22 19:09

Dimitre Novatchev