xslt - 使用 XSLT 的元数据驱动生成

标签 xslt xpath xsd metadata datamodel

我经常通过转换遗留系统的专有数据模型来创建 XSD 模式。这效果很好。但是,遗留系统只允许我指定参数的非常基本的属性,例如数据类型(intstring 等)。

我想通过一种允许我添加元数据的机制来增强 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/

相关文章:

c# - 如何构建自定义 AngleSharp 元素并将 HTML 部分插入/转换到元素中

Xpath 获取没有完整空子节点的节点

java - 意外元素(uri :"",本地 :"")。预期元素是(无)

.net - 使用 .NET 项目文件和架构?

performance - if/else条件和模板匹配条件之间的区别

c# - 在 .NET 6 中使用 xsltc.exe 生成的程序集(XSLT 样式表)

Python Selenium 按列名称复制表列

html - 根据一个或多个列值查找或更改同一表行上的项目

java - JAXB 解码错误 : UnmarshalException- Unexpected Element - Expected elements are (none) - @XmlRootElement NOT missing

xslt - exsl :node-set treat as the root node? 是什么