XSLT转换正在抛出错误

VJA*_*JAI 5 xslt

我的xml如下,

<?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>
Run Code Online (Sandbox Code Playgroud)

我正在使用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>
Run Code Online (Sandbox Code Playgroud)

我收到错误"状态启动时令牌文本将导致无效的XML文档.如果要编写XML片段,请确保将ConformanceLevel设置设置为ConformanceLevel.Fragment或ConformanceLevel.Auto." 我明白问题在于xml中的i:nil属性.虽然我在XSLT中包含了它们的命名空间,但我仍然收到错误.

Dim*_*hev 14

我收到错误"状态启动时令牌文本将导致无效的XML文档.如果要编写XML片段,请确保将ConformanceLevel设置设置为ConformanceLevel.Fragment或ConformanceLevel.Auto." 我明白问题在于xml中的i:nil属性.虽然我在XSLT中包含了它们的命名空间,但我仍然收到错误.

不会.问题是结果不是格式良好的XML文档,因此XmlWriter参与生成结果树到文本的最终序列化会引发此异常.

实际上,在您的结果中,您有两个Root元素,但没有一个元素具有父元素.

您需要生成格式良好的文档,或更改to 或的ConformanceLevel设置.XmlWriterConformanceLevel.FragmentConformanceLevel.Auto

要创建格式良好的输出,只需添加:

<xsl:template match="/">
 <top>
   <xsl:apply-templates/>
 </top>
</xsl:template>
Run Code Online (Sandbox Code Playgroud)