从元模型中消除可选字段

Eliminate optional fields from meta model

我创建了一个元模型来存储技术度量,以便在我使用 JAXB 解析它们之后在 Java 中管理它们。

我想支持标量(角度、长度、温度等)和矢量(如方向)。

作为奖励,我还想支持矩阵(可能用于旋转)。下面是一个示例模型。

<measure name="X">
   <properties>
      <property name="description" value="length"/>
      <property name="unit" value="mm"/>
      <property name="tolerance" value="1"/>
   </properties>

   <scalar>150.157</scalar>
   <vector/>
   <matrix/>
</measure>

我不喜欢当前的方法,我为三种可能的值类型设置了三个可选条目。

是否有更好、统一的方法而不使用也易于解析的可选字段?

其中一种方法是使用替换组。您声明了一些 ValueType 的全局元素,例如 value,并在需要的地方引用它。

然后添加进一步的类型(如 ScalarTypeVectorTypeMatrixType),它们扩展 ValueType 并声明全局元素 scalarvectormatrixsubstitutionGroup="tns:value"。这意味着这些元素可以替代 value.

在您的 MeasureType 中,您只需包含 <xs:element ref="value"/>,这将允许 scalarvectormatrix 出现。

XJC 可以编译这种模式,而 JAXB 可以很好地处理替换组。您ll get aJAXBElement`-typed 属性.

这是一个使用这种方法的example of schema

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema targetNamespace="http://www.opengis.net/ogc"
   xmlns:ogc="http://www.opengis.net/ogc"
   xmlns:gml="http://www.opengis.net/gml"
   xmlns:xsd="http://www.w3.org/2001/XMLSchema"
   elementFormDefault="qualified"
   version="1.1.3">
   <!-- 
      filter is an OGC Standard.
      Copyright (c) 2002,2003,2004,2010 Open Geospatial Consortium.
      To obtain additional rights of use, visit http://www.opengeospatial.org/legal/ .

      Updated: 2012-07-21
   -->
   <xsd:element name="Add" type="ogc:BinaryOperatorType"
      substitutionGroup="ogc:expression"/>
   <xsd:element name="Sub" type="ogc:BinaryOperatorType"
      substitutionGroup="ogc:expression"/>
   <xsd:element name="Mul" type="ogc:BinaryOperatorType"
      substitutionGroup="ogc:expression"/>
   <xsd:element name="Div" type="ogc:BinaryOperatorType"
      substitutionGroup="ogc:expression"/>
   <xsd:element name="PropertyName" type="ogc:PropertyNameType"
      substitutionGroup="ogc:expression"/>
   <xsd:element name="Function" type="ogc:FunctionType"
      substitutionGroup="ogc:expression"/>
   <xsd:element name="Literal" type="ogc:LiteralType"
      substitutionGroup="ogc:expression"/>
   <xsd:element name="expression" type="ogc:ExpressionType" abstract="true"/>
   <!-- <xsd:complexType name="ExpressionType" abstract="true" mixed="true"/>
     -->
   <xsd:complexType name="ExpressionType" abstract="true"/>
   <xsd:complexType name="BinaryOperatorType">
      <xsd:complexContent>
         <xsd:extension base="ogc:ExpressionType">
            <xsd:sequence>
               <xsd:element ref="ogc:expression" minOccurs="2" maxOccurs="2"/>
            </xsd:sequence>
         </xsd:extension>
      </xsd:complexContent>
   </xsd:complexType>
   <xsd:complexType name="FunctionType">
      <xsd:complexContent>
         <xsd:extension base="ogc:ExpressionType">
            <xsd:sequence>
               <xsd:element ref="ogc:expression" minOccurs="0"
                  maxOccurs="unbounded"/>
            </xsd:sequence>
            <xsd:attribute name="name" type="xsd:string" use="required"/>
         </xsd:extension>
      </xsd:complexContent>
   </xsd:complexType>
   <xsd:complexType name="LiteralType">
      <xsd:complexContent mixed="true">
         <xsd:extension base="ogc:ExpressionType">
            <xsd:sequence>
               <xsd:any minOccurs="0"/>
            </xsd:sequence>
         </xsd:extension>
      </xsd:complexContent>
   </xsd:complexType>
   <xsd:complexType name="PropertyNameType">
      <xsd:complexContent mixed="true">
         <xsd:extension base="ogc:ExpressionType"/>
      </xsd:complexContent>
   </xsd:complexType>
</xsd:schema>