2009-05-19 25 views
2

Delphi 2009 XML數據綁定向導無法處理包含complexContent聲明(無效指針操作)的簡單XSD。我可以在Delphi XML綁定向導中使用xsd:complexContent嗎?

這是一個錯誤還是知道限制?

實施例:

<?xml version="1.0" encoding="utf-8"?> 
<xsd:schema targetNamespace="http://example.org/" 
    xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
    elementFormDefault="qualified"> 

    <xsd:complexType name="TestType"> 
    <xsd:complexContent> 
     <xsd:restriction base="xsd:anyType"> 
     <xsd:attribute name="Name" type="xsd:string"/> 
     </xsd:restriction> 
    </xsd:complexContent> 
    </xsd:complexType> 

</xsd:schema> 

編輯:其它實例中工作得很好,所以它看起來像複雜內容定義的一部分導致錯誤。工作示例:

<xsd:complexType name="pc-Typ"> 
    <xsd:sequence> 
    <xsd:element name="name"  type="xsd:string"/> 
    </xsd:sequence> 
    <xsd:attribute name="id" type="xsd:integer"/> 
</xsd:complexType> 

<xsd:complexType name="myPC-Typ"> 
    <xsd:complexContent> 
    <xsd:extension base="pc-Typ"> 
     <xsd:sequence> 
     <xsd:element name="ram" type="xsd:integer"/> 
     </xsd:sequence> 
    </xsd:extension> 
    </xsd:complexContent> 
</xsd:complexType> 

回答

2

我可以在Delphi XML綁定向導中使用xsd:complexContent嗎?

是的,可以使用xsd:complexContent

我知道德爾福有它的缺陷,但我不會責怪德爾福這個模式。 XSD是一種豐富的模式語言,Delphi的OO類也是如此。兩個世界的部分重疊,但有些部分不會。XML數據綁定是將XML模式轉換爲OO類結構的一種行爲,因此該模式必須足夠具體以表示爲類。

在此示例中,您所說的TestType與任何類型匹配,只要它具有名爲Namestring屬性即可。 XML驗證器可能適用於這種定義,但很難將其定義爲單繼承模型,因爲foo:Animalfoo:Plantfoo:Mineral可能都具有Name屬性。

我定義了一個名爲TestBaseType的空complexType,並且生成的類完全正確。

<?xml version="1.0" encoding="utf-8"?> 
<xsd:schema targetNamespace="http://example.org/" 
    xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
    elementFormDefault="qualified"> 

    <xsd:complexType name="TestBaseType"> 
    <xsd:sequence> 
    </xsd:sequence> 
    </xsd:complexType> 

    <xsd:complexType name="TestType"> 
    <xsd:complexContent> 
     <xsd:restriction base="TestBaseType"> 
     <xsd:attribute name="Name" type="xsd:string"/> 
     </xsd:restriction> 
    </xsd:complexContent> 
    </xsd:complexType> 
</xsd:schema> 

這產生以下代碼:

unit test; 

interface 

uses xmldom, XMLDoc, XMLIntf; 

type 

{ Forward Decls } 

    IXMLTestBaseType = interface; 
    IXMLTestType = interface; 

{ IXMLTestBaseType } 

    IXMLTestBaseType = interface(IXMLNode) 
    ['{0FBC1D84-DA5E-4315-83A9-B5FFE9528969}'] 
    end; 

{ IXMLTestType } 

    IXMLTestType = interface(IXMLTestBaseType) 
    ['{12E35067-516F-4457-8C62-4131CA60D706}'] 
    { Property Accessors } 
    function Get_Name: WideString; 
    procedure Set_Name(Value: WideString); 
    { Methods & Properties } 
    property Name: WideString read Get_Name write Set_Name; 
    end; 

{ Forward Decls } 

    TXMLTestBaseType = class; 
    TXMLTestType = class; 

{ TXMLTestBaseType } 

    TXMLTestBaseType = class(TXMLNode, IXMLTestBaseType) 
    protected 
    { IXMLTestBaseType } 
    end; 

{ TXMLTestType } 

    TXMLTestType = class(TXMLTestBaseType, IXMLTestType) 
    protected 
    { IXMLTestType } 
    function Get_Name: WideString; 
    procedure Set_Name(Value: WideString); 
    end; 

implementation 

{ TXMLTestBaseType } 

{ TXMLTestType } 

function TXMLTestType.Get_Name: WideString; 
begin 
    Result := AttributeNodes['Name'].Text; 
end; 

procedure TXMLTestType.Set_Name(Value: WideString); 
begin 
    SetAttribute('Name', Value); 
end; 

end. 
0

確認您的第一個示例在xmllint中解析得很好。

我很驚訝地看到xsd:NCName,因爲我傾向於認爲它僅用於內部使用 - 但當然您可以使用它。我在想德菲的實施者可能會做出同樣的假設。嘗試將其更改爲xsd:string,以幫助縮小範圍。

而且 - 當然 - 爲什麼不把它發佈到官方的Dephi論壇? (我確定它存在)他們可能會修復它,或者有一個解決方法。


BTW:它讓你做一個完整的架構更容易測試XSD的 - 可能會更快得到答案的話,因爲別人不需要在標籤中添加。

+1

改變了模式作爲具有相同的結果建議。我也會將它發佈到QualityCentral。 – mjn 2009-05-20 05:21:03

1

我一直在玩D2007,它似乎也有xsd:anyType的問題。不過,不知道解決這個問題的方法。我知道Delphi對XML解析功能始終存在一些小問題。直到D7,它甚至沒有真正符合XML標準。所以認爲這只是Delphi的另一個缺陷。 在我的Delphi應用程序中使用XML時,我只是忽略這個XML嚮導,而只是導入MSXML類型庫,編寫自己的代碼來包裝XML文件。它需要更多時間,但它爲我的代碼解決了很多問題。 對於一個項目,我甚至做了一些不同的事情,創建一個.NET模塊作爲XML文件的包裝器,將此包裝器作爲COM對象導入到我的Delphi項目中,以調用它的所有功能。它更有效,速度也相當快。

我喜歡Delphi,自從它取代Turbo/Borland Pascal以來,它一直是我的首選開發語言,但是現在競爭(例如.NET)已經變得同樣強大。

1

你最好不要使用XML數據綁定向導。我開始使用它,但發現它非常不適合我使用的許多XML。然後,我開始直接使用IXMLDocument和IXMLNode,並且它們更好一些,但仍然不是特別有利於性能和設計的原因。然後我切換到libxml2並從那以後一直使用它。

相關問題