2011-10-03 27 views
5

Tôi đã xml như dưới đây,XSLT biến đổi được ném lỗi

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

Tôi đang chuyển sang một định dạng xml trong C# sử dụng 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> 

Tôi nhận được thông báo lỗi " Mã thông báo trong trạng thái Bắt đầu sẽ dẫn đến một tài liệu XML không hợp lệ. Đảm bảo rằng cài đặt ConformanceLevel được đặt thành ConformanceLevel.Fragment hoặc ConformanceLevel.Auto nếu bạn muốn viết một đoạn XML. " Tôi hiểu vấn đề là với các thuộc tính i: nil trong xml. Mặc dù tôi đã bao gồm không gian tên của chúng trong XSLT nhưng tôi vẫn gặp lỗi.

Trả lời

11

Tôi nhận được thông báo lỗi "Mã văn bản trong trạng thái bắt đầu sẽ cho kết quả trong một tài liệu XML hợp lệ . Hãy chắc chắn rằng các thiết lập ConformanceLevel là thiết lập để ConformanceLevel.Fragment hoặc ConformanceLevel.Auto nếu bạn muốn viết một đoạn XML. " Tôi hiểu vấn đề là với các thuộc tính i: nil trong xml. Mặc dù tôi đã bao gồm không gian tên của chúng trong XSLT vẫn đang gặp lỗi.

số Vấn đề là kết quả không phải là một tài liệu XML cũng như hình thành và do đó XmlWriter, tham gia vào việc sản xuất các serialization cuối cùng của cây kết quả văn bản, tăng ngoại lệ này.

Thực tế, trong kết quả của bạn, bạn có hai thành phần Root và không có phần tử nào trong số chúng có phần tử gốc.

Bạn cần phải tạo ra một tài liệu tốt được hình thành, hoặc thay đổi các thiết lập ConformanceLevel cho XmlWriter-ConformanceLevel.Fragment hoặc ConformanceLevel.Auto.

Để tạo ra một sản lượng wellformed, chỉ cần thêm:

<xsl:template match="/"> 
<top> 
    <xsl:apply-templates/> 
</top> 
</xsl:template> 
Các vấn đề liên quan