问题
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