2011-10-03 46 views
5

我有XML如下,XSLT转换抛出错误

<?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> 

我转变成使用XslCompiledTransform在C#中的另一个XML格式,

<?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> 

我得到的错误“如果您想写一个XML片段,请确保ConformanceLevel设置被设置为ConformanceLevel.Fragment或ConformanceLevel.Auto。“状态开始的令牌文本将导致无效的XML文档。我明白这个问题与xml中的i:nil属性有关。尽管我在XSLT中包含了它们的命名空间,但我仍然遇到了错误。

回答

11

我得到的错误“令牌中的文本状态开始将导致一个无效 XML文档中。确保该ConformanceLevel设置 设置为ConformanceLevel.Fragment或ConformanceLevel.Auto如果你想 写一个XML片段“。我明白这个问题与xml中的i:nil 属性有关。尽管我在 XSLT中包含了它们的名称空间,但我仍然收到错误消息。

号的问题是,该结果不是良好的XML文档因此XmlWriter,参与产生结果树为文本的最后的序列化,提高此异常。

真的,在你的结果中你有两个Root元素并没有一个父元素。

您需要制作良好的文档,或更改ConformanceLevel设置为XmlWriterConformanceLevel.FragmentConformanceLevel.Auto

要创建一个简洁(wellformed)输出,只需添加

<xsl:template match="/"> 
<top> 
    <xsl:apply-templates/> 
</top> 
</xsl:template>