2012-04-20 15 views
1

我想做一個XML模式來驗證我的XML,第一次。我無法在XML模式中包含屬性。 (http://www.w3.org/2001/XMLSchema-instance使用)

<?xml version="1.0" encoding="utf-8"?> 
<CrystalReport 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="urn:crystal-reports:schemas:report-detail 
        http://www.businessobjects.com/products/xml/CR2008Schema.xsd" 
    xmlns="urn:crystal-reports:schemas:report-detail" 
> 

    <ReportHeader> 
    <Section SectionNumber="0"> 
     <Text Name="Text9"> 
     ... 

我的XML模式(注意2號線,3號和20):

我的XML(通知 「-instance」 和 「SectionNumber="0"」)開始

<?xml version="1.0" encoding="utf-8"?> 
<xsi:schema id="XMLSchema_varslings1" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns="urn:crystal-reports:schemas:report-detail" 
    elementFormDefault="qualified" 
    targetNamespace="urn:crystal-reports:schemas:report-detail"> 

    <xsi:element name="CrystalReport" type="CrystalReportType"/> 

    <xsi:complexType name="CrystalReportType"> 
     <xsi:sequence maxOccurs="unbounded"> 
      <xsi:element name="ReportHeader" type="ReportHeaderType"/> 
     </xsi:sequence> 
    </xsi:complexType> 

    <xsi:complexType name="ReportHeaderType"> 
     <xsi:sequence> 
      <xsi:element name="Section" type="SectionType"/> 
      <!-- This is line 19....................................... --> 
      <xsi:attribute name="SectionNumber" type="xsi:Integer"/> 
     </xsi:sequence> 
    </xsi:complexType> 

    <xsi:complexType name="SectionType"> 
     <xsi:sequence maxOccurs="unbounded"> 
      <xsi:element name="Text" type="TextType" /> 
     </xsi:sequence> 
    </xsi:complexType> 

    <xsi:complexType name="TextType"> 
     <xsi:sequence maxOccurs="unbounded"> 
      <xsi:element name="TextValue" type="xsi:string" /> 
     </xsi:sequence> 
    </xsi:complexType> 
</xsi:schema> 

我得到這個錯誤,我無法解決: 「The root element of a W3C XML Schema should be <schema> and its namespace should be 'http://www.w3.org/2001/XMLSchema'.

如果我從模式中刪除「-instance」,我擺脫了a bove錯誤,但是我不能使用屬性「<xsi:attribute name="SectionNumber" type="xsi:Integer"/>」的代碼。

我甚至不知道我的真正問題是-instance部分,還是有另一種方法在模式中編寫/包含屬性。我該如何解決這個問題?

回答

1

你混淆了兩個命名空間:XMLSchemaXMLSchema-instance。這兩個服務於不同目的,XMLSchema(通常以前綴xs)用於聲明您的模式。這就是沒有它的原因,你的XSD文件無法正常工作的原因。

命名空間XMLSchema-instance(通常與前綴xsi)當你想使用文檔中的某些XML模式時使用。例如,schemaLocation前綴位於此名稱空間中。

類型integer(小寫i)位於XMLSchema命名空間中,因此您必須按照這種方式使用它。

此外,您有不正確的元素順序。 <xs:attribute>應該在<xs:sequence>之後。

所以,你的模式應該是這樣的:

<xs:schema id="XMLSchema_varslings1" 
    xmlns:xs="http://www.w3.org/2001/XMLSchema" 
    xmlns="urn:crystal-reports:schemas:report-detail" 
    elementFormDefault="qualified" 
    targetNamespace="urn:crystal-reports:schemas:report-detail"> 

    … 

    <xs:complexType name="ReportHeaderType"> 
    <xs:sequence> 
     <xs:element name="Section" type="SectionType"/> 
    </xs:sequence> 
    <xs:attribute name="SectionNumber" type="xs:integer"/> 
    </xs:complexType> 

</xs:schema> 

我也改變了命名空間前綴xs,因爲它是有道理的,但在技術上是沒有必要的。

+0

謝謝,這是一個非常好的答案。這很有道理。你的意思是你把前綴改成'xs'而不是'ns'吧?我會馬上開始測試。 – radbyx 2012-04-20 10:38:33

+0

對,我的意思是'xs',修正了。 – svick 2012-04-20 10:45:03

+0

此外,現在它是有道理的,爲什麼我讀的所有使用'xs'而不是'xsi'就像我做了哈哈:) – radbyx 2012-04-20 10:55:18

0

嗯,我發現這一點:

Link to w3

「這種模式不應該被用作這樣的:XML模式建議禁止在這個命名空間屬性的聲明」

我猜我麻煩的話,因爲我需要改變XML。問題是XML是從我們擁有的舊程序生成的。任何人都可以確認/不確認我找到了解決方案,否則我會appriciate任何人elses的答案?

相關問題