0

我已經搜索和搜索,但找不到任何東西,甚至遠程類似於答案。從dtd或xsd甚至xml文檔本身的NHibernate映射

我有一個XML文檔類型(我的文檔類型,沒有NHibernate相關)的DTD(xml-dtd),我想將給定文檔類型的所有文檔存儲到關係模型中。不,我不想將XML文件本身作爲某種varchar或XML字段或某種類型存儲到數據庫中,這會破壞目的 - 我希望將其分解爲其元素和屬性,並將其存儲爲THAT,作爲一個適當的關係模型。而XML支持。

我可以在Visual Studio 2015中從給定的DTD創建XSD,然後使用該XSD創建反映給定XSD(以及擴展原始DTD)的C#類。文件解析,一切都OK。

現在的問題是如何通過NHibernate將這些XML文檔存儲到RDBMS中,而無需(大量)手工編碼,從而所有關係都以這種方式存在?通過使用自動映射功能必須有一個簡單的方法,但是原來的DTD(一些IDREF的東西等關係的東西)存在一些「侷限性」,我想將它們「轉換」爲關係並且具有適當的與其他類的關係,而不是將「code」存儲爲字符串類型的值。

所以基本上,我需要一個NHibernate的& & XML & & DTD & & XSD大師在同一時間,以闡明如何可以很容易地取得了一些光。至少在過去的10 - 15年裏,我確信這種事情與hibernate和nhibernate是「正常的」(我從未嘗試過,似乎是我第一次需要將XML文檔存儲在數據庫中)到他們的組成部分而不是整體)。

如果這樣的事情是不可能的,那麼是否還有NHibernate的「XML文檔驅動程序」這樣的事情,以便它不必進入RDBMS,而是保留爲文件系統上的XML文檔? (例如,SGML/XML專家在那裏查看IDREF和NMTOKEN,他們爲什麼只是「字符串」,而不是他們應該去的地方的適當關係,即另一個類或用法或變體或其他什麼?): 最終結果是:

我期望的參考是「類」與「代碼」(或ID)G117本身(如public virtual Class Reference { get; set; }

實際結果:只是一個字符串「代碼」與價值「G117 「如在

[System.Xml.Serialization.XmlAttributeAttribute(DataType="NMTOKEN")] 
    public string code { 
     get { 
      return this.codeField; 
     } 
     set { 
      this.codeField = value; 
     } 
    } 

XML文件:

<Class code="G117" kind="process"> 
    <SuperClass code="G"/> 
    <Rubric id="13-223" kind="preferred"> 
     <Label xml:lang="en">A preferred label</Label> 
    </Rubric> 
    <Rubric id="13-224" kind="shortTitle"> 
     <Label xml:lang="en">Short title</Label> 
    </Rubric> 
    <Rubric id="13-225" kind="exclusion"> 
     <Label xml:lang="en">There is some exclusion text with a reference here <Reference>G12</Reference></Label> 
    </Rubric> 
    <Rubric id="13-226" kind="criteria"> 
     <Label xml:lang="en">Some criteria text goes here</Label> 
    </Rubric> 
</Class> 

<Rubric id="56-327" kind="exclusion"> 
    <Label xml:lang="en">This is some thext that might refer someplace <Reference>G117</Reference>; and another piece of text that refers to another place <Reference>BF9</Reference>; Another text describing something and there might be a reference from this piece of text somewhere else too <Reference>AB7</Reference></Label> 
</Rubric> 

DTD:

<!ELEMENT Class (Meta*,SuperClass*,SubClass*,ModifiedBy*,ExcludeModifier*,Rubric*,History*)> 
<!ATTLIST Class code NMTOKEN #REQUIRED kind IDREF #REQUIRED usage IDREF #IMPLIED variants IDREFS #IMPLIED> 
<!ELEMENT Rubric (Label+,History*)> 
<!ATTLIST Rubric id ID #IMPLIED kind IDREF #REQUIRED usage IDREF #IMPLIED> 
<!ELEMENT Reference (#PCDATA)> 
<!ATTLIST Reference classCode CDATA #IMPLIED authority NMTOKEN #IMPLIED uid NMTOKEN #IMPLIED code NMTOKEN #IMPLIED usage IDREF #IMPLIED variants IDREFS #IMPLIED> 

生成的XSD:

<xs:element name="Class"> 
    <xs:complexType> 
     <xs:sequence> 
     <xs:element minOccurs="0" maxOccurs="unbounded" ref="Meta" /> 
     <xs:element minOccurs="0" maxOccurs="unbounded" ref="SuperClass" /> 
     <xs:element minOccurs="0" maxOccurs="unbounded" ref="SubClass" /> 
     <xs:element minOccurs="0" maxOccurs="unbounded" ref="ModifiedBy" /> 
     <xs:element minOccurs="0" maxOccurs="unbounded" ref="ExcludeModifier" /> 
     <xs:element minOccurs="0" maxOccurs="unbounded" ref="Rubric" /> 
     <xs:element minOccurs="0" maxOccurs="unbounded" ref="History" /> 
     </xs:sequence> 
     <xs:attribute name="code" type="xs:NMTOKEN" use="required" /> 
     <xs:attribute name="kind" type="xs:IDREF" use="required" /> 
     <xs:attribute name="usage" type="xs:IDREF" /> 
     <xs:attribute name="variants" type="xs:IDREFS" /> 
    </xs:complexType> 
    </xs:element> 
    <xs:element name="Rubric"> 
    <xs:complexType> 
     <xs:sequence> 
     <xs:element minOccurs="1" maxOccurs="unbounded" ref="Label" /> 
     <xs:element minOccurs="0" maxOccurs="unbounded" ref="History" /> 
     </xs:sequence> 
     <xs:attribute name="id" type="xs:ID" /> 
     <xs:attribute name="kind" type="xs:IDREF" use="required" /> 
     <xs:attribute name="usage" type="xs:IDREF" /> 
    </xs:complexType> 
    </xs:element> 
    <xs:element name="Reference"> 
    <xs:complexType> 
     <xs:simpleContent> 
     <xs:extension base="xs:string"> 
      <xs:attribute name="classCode" type="xs:string" /> 
      <xs:attribute name="authority" type="xs:NMTOKEN" /> 
      <xs:attribute name="uid" type="xs:NMTOKEN" /> 
      <xs:attribute name="code" type="xs:NMTOKEN" /> 
      <xs:attribute name="usage" type="xs:IDREF" /> 
      <xs:attribute name="variants" type="xs:IDREFS" /> 
     </xs:extension> 
     </xs:simpleContent> 
    </xs:complexType> 
    </xs:element> 

生成的C#類

/// <remarks/> 
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.6.1055.0")] 
[System.SerializableAttribute()] 
[System.Diagnostics.DebuggerStepThroughAttribute()] 
[System.ComponentModel.DesignerCategoryAttribute("code")] 
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true, Namespace="http://tempuri.org/MyStuff")] 
[System.Xml.Serialization.XmlRootAttribute(Namespace="http://tempuri.org/MyStuff", IsNullable=false)] 
public partial class Class { 

    private Meta[] metaField; 

    private SuperClass[] superClassField; 

    private SubClass[] subClassField; 

    private ModifiedBy[] modifiedByField; 

    private ExcludeModifier[] excludeModifierField; 

    private Rubric[] rubricField; 

    private History[] historyField; 

    private string codeField; 

    private string kindField; 

    private string usageField; 

    private string variantsField; 

    /// <remarks/> 
    [System.Xml.Serialization.XmlElementAttribute("Meta")] 
    public Meta[] Meta { 
     get { 
      return this.metaField; 
     } 
     set { 
      this.metaField = value; 
     } 
    } 

    /// <remarks/> 
    [System.Xml.Serialization.XmlElementAttribute("SuperClass")] 
    public SuperClass[] SuperClass { 
     get { 
      return this.superClassField; 
     } 
     set { 
      this.superClassField = value; 
     } 
    } 

    /// <remarks/> 
    [System.Xml.Serialization.XmlElementAttribute("SubClass")] 
    public SubClass[] SubClass { 
     get { 
      return this.subClassField; 
     } 
     set { 
      this.subClassField = value; 
     } 
    } 

    /// <remarks/> 
    [System.Xml.Serialization.XmlElementAttribute("ModifiedBy")] 
    public ModifiedBy[] ModifiedBy { 
     get { 
      return this.modifiedByField; 
     } 
     set { 
      this.modifiedByField = value; 
     } 
    } 

    /// <remarks/> 
    [System.Xml.Serialization.XmlElementAttribute("ExcludeModifier")] 
    public ExcludeModifier[] ExcludeModifier { 
     get { 
      return this.excludeModifierField; 
     } 
     set { 
      this.excludeModifierField = value; 
     } 
    } 

    /// <remarks/> 
    [System.Xml.Serialization.XmlElementAttribute("Rubric")] 
    public Rubric[] Rubric { 
     get { 
      return this.rubricField; 
     } 
     set { 
      this.rubricField = value; 
     } 
    } 

    /// <remarks/> 
    [System.Xml.Serialization.XmlElementAttribute("History")] 
    public History[] History { 
     get { 
      return this.historyField; 
     } 
     set { 
      this.historyField = value; 
     } 
    } 

    /// <remarks/> 
    [System.Xml.Serialization.XmlAttributeAttribute(DataType="NMTOKEN")] 
    public string code { 
     get { 
      return this.codeField; 
     } 
     set { 
      this.codeField = value; 
     } 
    } 

    /// <remarks/> 
    [System.Xml.Serialization.XmlAttributeAttribute(DataType="IDREF")] 
    public string kind { 
     get { 
      return this.kindField; 
     } 
     set { 
      this.kindField = value; 
     } 
    } 

    /// <remarks/> 
    [System.Xml.Serialization.XmlAttributeAttribute(DataType="IDREF")] 
    public string usage { 
     get { 
      return this.usageField; 
     } 
     set { 
      this.usageField = value; 
     } 
    } 

    /// <remarks/> 
    [System.Xml.Serialization.XmlAttributeAttribute(DataType="IDREFS")] 
    public string variants { 
     get { 
      return this.variantsField; 
     } 
     set { 
      this.variantsField = value; 
     } 
    } 
} 

/// <remarks/> 
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.6.1055.0")] 
[System.SerializableAttribute()] 
[System.Diagnostics.DebuggerStepThroughAttribute()] 
[System.ComponentModel.DesignerCategoryAttribute("code")] 
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true, Namespace="http://tempuri.org/MyStuff")] 
[System.Xml.Serialization.XmlRootAttribute(Namespace="http://tempuri.org/MyStuff", IsNullable=false)] 
public partial class Rubric { 

    private Label[] labelField; 

    private History[] historyField; 

    private string idField; 

    private string kindField; 

    private string usageField; 

    /// <remarks/> 
    [System.Xml.Serialization.XmlElementAttribute("Label")] 
    public Label[] Label { 
     get { 
      return this.labelField; 
     } 
     set { 
      this.labelField = value; 
     } 
    } 

    /// <remarks/> 
    [System.Xml.Serialization.XmlElementAttribute("History")] 
    public History[] History { 
     get { 
      return this.historyField; 
     } 
     set { 
      this.historyField = value; 
     } 
    } 

    /// <remarks/> 
    [System.Xml.Serialization.XmlAttributeAttribute(DataType="ID")] 
    public string id { 
     get { 
      return this.idField; 
     } 
     set { 
      this.idField = value; 
     } 
    } 

    /// <remarks/> 
    [System.Xml.Serialization.XmlAttributeAttribute(DataType="IDREF")] 
    public string kind { 
     get { 
      return this.kindField; 
     } 
     set { 
      this.kindField = value; 
     } 
    } 

    /// <remarks/> 
    [System.Xml.Serialization.XmlAttributeAttribute(DataType="IDREF")] 
    public string usage { 
     get { 
      return this.usageField; 
     } 
     set { 
      this.usageField = value; 
     } 
    } 
} 

/// <remarks/> 
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.6.1055.0")] 
[System.SerializableAttribute()] 
[System.Diagnostics.DebuggerStepThroughAttribute()] 
[System.ComponentModel.DesignerCategoryAttribute("code")] 
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true, Namespace="http://tempuri.org/MyStuff")] 
[System.Xml.Serialization.XmlRootAttribute(Namespace="http://tempuri.org/MyStuff", IsNullable=false)] 
public partial class Reference { 

    private string classCodeField; 

    private string authorityField; 

    private string uidField; 

    private string codeField; 

    private string usageField; 

    private string variantsField; 

    private string valueField; 

    /// <remarks/> 
    [System.Xml.Serialization.XmlAttributeAttribute()] 
    public string classCode { 
     get { 
      return this.classCodeField; 
     } 
     set { 
      this.classCodeField = value; 
     } 
    } 

    /// <remarks/> 
    [System.Xml.Serialization.XmlAttributeAttribute(DataType="NMTOKEN")] 
    public string authority { 
     get { 
      return this.authorityField; 
     } 
     set { 
      this.authorityField = value; 
     } 
    } 

    /// <remarks/> 
    [System.Xml.Serialization.XmlAttributeAttribute(DataType="NMTOKEN")] 
    public string uid { 
     get { 
      return this.uidField; 
     } 
     set { 
      this.uidField = value; 
     } 
    } 

    /// <remarks/> 
    [System.Xml.Serialization.XmlAttributeAttribute(DataType="NMTOKEN")] 
    public string code { 
     get { 
      return this.codeField; 
     } 
     set { 
      this.codeField = value; 
     } 
    } 

    /// <remarks/> 
    [System.Xml.Serialization.XmlAttributeAttribute(DataType="IDREF")] 
    public string usage { 
     get { 
      return this.usageField; 
     } 
     set { 
      this.usageField = value; 
     } 
    } 

    /// <remarks/> 
    [System.Xml.Serialization.XmlAttributeAttribute(DataType="IDREFS")] 
    public string variants { 
     get { 
      return this.variantsField; 
     } 
     set { 
      this.variantsField = value; 
     } 
    } 

    /// <remarks/> 
    [System.Xml.Serialization.XmlTextAttribute()] 
    public string Value { 
     get { 
      return this.valueField; 
     } 
     set { 
      this.valueField = value; 
     } 
    } 
} 

回答

0

那麼,奧斯卡,和其他誰可能對這樣的事情感興趣,它花了我12天,但最終我弄明白了,它的工作原理。它需要幾秒鐘(也許2)來創建映射,數據庫,並用XML中的數據填充它,但它然後運行得非常好。仍然不確定如何將「IDREF」類型映射到真實ID,但這與我在過去幾天的經歷相比,是一個小問題。

0

我很困惑 - 看來你是矛盾的你自己。有一次,你聲明「如何通過NHibernate將這些XML文檔存儲到RDBMS中?「然後稍後問你」是否還有像NHibernate的「XML文檔驅動程序」這樣的東西,以便它不必進入RDBMS,而是作爲文檔系統上的XML文檔?「這看起來像確切與第一個問題相反

在任何情況下--NHibernate是一個用於映射對象模型和可以使用SQL查詢的各種關係數據庫系統的框架如果您發現一個使用XML文件存儲數據和暴露的RDBMS這些文件的SQL外觀,也許你可以爲它編寫一個NHibernate驅動程序,但是NHibernate本身並不包含像定位XML文件的查詢引擎之類的任何東西。至於映射到NHibernate的RDBMS,我懷疑你的最好的辦法是寫一些產生的東西NHibernate的映射配置。通過轉換爲NHibernate的XML配置,或者通過發佈代碼映射或FluentNHibernate。

+0

我忘了說「萬一這樣的事情是不可能的,那麼...」 – pele

+0

編輯並添加「如果這樣的事情是不可能的,那麼」,我希望你贊成編輯? – pele