我经常通过转换遗留系统的专有数据模型来创建 XSD 模式。这效果很好。但是,遗留系统只允许我指定参数的非常基本的属性,例如数据类型(int
、string
等)。
我想通过一种允许我添加元数据的机制来增强 XSL 转换,以便为转换提供更多详细信息。我想到了类似 Java 属性符号之类的东西来将属性分配给 XPath。
想象以下示例:
遗留系统数据模型 (实际上很整洁,但最适合演示目的)
<datamodel>
<customer>
<firstName type="string"/>
<lastName type="string"/>
<age type="int">
<customer>
</datamodel>
元数据
customer/firstName/@nillable=false
customer/lastName/@nillable=false
customer/age/@nillable=true
customer/firstName/@minOccurs=1
customer/firstName/@maxOccurs=1
customer/lastName/@minOccurs=1
customer/lastName/@maxOccurs=1
生成的 XSD 架构
...
<xs:complexType name="customerType">
<xs:sequence>
<xs:element name="firstName" type="xs:string" nillable="false" minOccurs="1" maxOccurs="1"/>
<xs:element name="lastName" type="xs:string" nillable="false" minOccurs="1" maxOccurs="1"/>
<xs:element name="age" type="xs:int" nillable="true"/>
</xs:sequence>
</xs:complexType>
...
你对那个怎么想的?有没有办法将元数据包含到 XSL 样式表中?
最佳答案
最好的解决方案是通过添加缺失的元数据来修改遗留数据。
修改后的“数据模型”词汇表的一个实例可能是这样的:
<datamodel xmlns:nm="my:new.meta">
<customer>
<firstName type="string"
nm:nillable="false"
nm:minOccurs="1"
nm:maxOccurs="1"
/>
<lastName type="string"
nm:nillable="false"
nm:minOccurs="1"
nm:maxOccurs="1"
/>
<age type="int" nm:nillable="true"/>
</customer>
</datamodel>
将新属性放在单独的命名空间中是一种很好的方法,可以轻松地将它们与已经支持的属性区分开来。通常不建议在命名空间中使用属性,因此如果要避免这种情况,可以使用子元素(属于新命名空间)而不是属性。使新属性属于不同的命名空间可能会导致旧模式验证不拒绝它们。
如果由于某些原因无法修改遗留数据,我建议不要在 XSLT 样式表本身中包含新属性(这是完全可能的,例如将其定义为全局
<xsl:variable>
的内容) ,而是将这些新属性作为单独的 XML 文件或作为一组一个或多个 XML 文件提供。在 XSLT 转换期间,可以使用 XSLT document() 函数动态访问任何 XML 文件。具有新属性的 XML 文件实例可能如下所示:
<newProperties xmlns:nm="my:new.meta">
<customer>
<firstName nm:nillable="false"
nm:minOccurs="1"
nm:maxOccurs="1"
/>
<lastName nm:nillable="false"
nm:minOccurs="1"
nm:maxOccurs="1"
/>
<age nm:nillable="true"/>
</customer>
</newProperties>
希望这有帮助。
干杯,
迪米特·诺瓦切夫
关于xslt - 使用 XSLT 的元数据驱动生成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/280542/