“extend” an xml schema

北慕城南 提交于 2020-01-04 06:01:16

问题


I'm trying to "extend" an xml schema (nhibernate here, for example), to add my own entities inside of it. I'm stuck to the point where validation chokes on the "exm:foo" (and exm:foobar) entity, as the "base" schema doesn't allow it. How can I manage to do that, without changing the base schema ?

Sample :

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="Test" namespace="Test.DataAccess.Entities" xmlns:exm="urn:extend-mappings">
  <class name="Post" table="POSTS" xmlns="urn:nhibernate-mapping-2.2" >
    <exm:foo bar="baz" />

    <property name="Body" type="String" column="BODY">
      <exm:foobar />
    </property>

    [...]

  </class>
</hibernate-mapping>

回答1:


Ideally, a schema would allow extensions in selected places, by means of xs:any declarations. Unfortunately, the nhibernate schema does not.

So you will have to write your own schema, and import the existing schema. In such an approach, you could derive new schema types from the existing base schema types. Unfortunately, the element class of nhibernate is defined using an anonymous type which you cannot extend. So you would have to define your own class element and copy nhibernate's content model, extending it where desired.

As a consequence, applications processing the base schema probably will not be able to process your extended schema, so you will also have to rewrite all tools.




回答2:


You could use the <meta> tags to put additional information to the NHibernate mapping files. This is a rarely used and poorly documented feature.

Documentation (for Hibernate java code generation, but it could be used for anything else)

Mapping:

<class name="Post" table="POSTS" xmlns="urn:nhibernate-mapping-2.2" >
  <meta attribut="bar">baz</meta>

  <property name="Body" type="String" column="BODY">
    <meta attribute="property-bar">property-baz</meta>
  </property>

  <!-- ... -->
</class>

You can read the meta tags from the configuration

foreach (PersistentClass persistentClass in Configuration.ClassMappings())
{
  MetaAttributes attribute = persistentClass.GetMetaAttribute("bar");
  // ...
  foreach(Property property in persistentClass.PropertyIterator())
  {
    MetaAttributes propertyAttribute = property.GetMetaAttribute("property-bar");
    // ...
  }
}


来源:https://stackoverflow.com/questions/1585959/extend-an-xml-schema

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!