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