如何在 XSD 中重用 simpleType 重定义
假设在 definition.xsd 中有一个已定义的简单类型,
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:simpleType name="myType">
<xs:restriction base="xs:string" />
</xs:simpleType>
</xs:schema>
然后该类型在 2 个不同的 xsd 中使用:use1.xsd 和 use2.xsd:
use1.xsd:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:include schemaLocation="definition.xsd"></xs:include>
<xs:complexType name="ComplexType1">
<xs:attribute name="typeInstance1" type="myType" use="required" />
</xs:complexType>
</xs:schema>
use2.xsd:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:include schemaLocation="definition.xsd"></xs:include>
<xs:complexType name="ComplexType2">
<xs:attribute name="typeInstance2" type="myType" use="required" />
</xs:complexType>
</xs:schema>
然后有2个文件pspecialUse1.xsd和preferredUse2.xsd,重新定义之前的两个 xsd:
pspecialUse1.xsd:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:redefine schemaLocation="use1.xsd">
<xs:simpleType name="myType">
<xs:restriction base = "myType">
<xs:enumeration value = "value1"/>
<xs:enumeration value = "value2"/>
</xs:restriction>
</xs:simpleType>
</xs:redefine>
</xs:schema>
pspecialUse2.xsd:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:redefine schemaLocation="use2.xsd">
<xs:simpleType name="myType">
<xs:restriction base = "myType">
<xs:enumeration value = "value1"/>
<xs:enumeration value = "value2"/>
</xs:restriction>
</xs:simpleType>
</xs:redefine>
</xs:schema>
现在的问题:我如何重用 myType 的重新定义(特别是 use1.xsd 和specialUse2.xsd),使其不重复。
理想情况下,它是另一个文件 reused.xsd:
<xs:simpleType name="myType">
<xs:restriction base = "myType">
<xs:enumeration value = "value1"/>
<xs:enumeration value = "value2"/>
</xs:restriction>
</xs:simpleType>
文件 reused.xsd 中的引用用于 pspecialUse1.xsd 和 pspecialUse2 .xsd。
Imagine there is a defined simple type in definition.xsd
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:simpleType name="myType">
<xs:restriction base="xs:string" />
</xs:simpleType>
</xs:schema>
Then the type is used in 2 different xsds: use1.xsd and use2.xsd:
use1.xsd:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:include schemaLocation="definition.xsd"></xs:include>
<xs:complexType name="ComplexType1">
<xs:attribute name="typeInstance1" type="myType" use="required" />
</xs:complexType>
</xs:schema>
use2.xsd:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:include schemaLocation="definition.xsd"></xs:include>
<xs:complexType name="ComplexType2">
<xs:attribute name="typeInstance2" type="myType" use="required" />
</xs:complexType>
</xs:schema>
Then there are 2 files particularUse1.xsd and particularUse2.xsd, that redefine two previous xsds:
particularUse1.xsd:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:redefine schemaLocation="use1.xsd">
<xs:simpleType name="myType">
<xs:restriction base = "myType">
<xs:enumeration value = "value1"/>
<xs:enumeration value = "value2"/>
</xs:restriction>
</xs:simpleType>
</xs:redefine>
</xs:schema>
particularUse2.xsd:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:redefine schemaLocation="use2.xsd">
<xs:simpleType name="myType">
<xs:restriction base = "myType">
<xs:enumeration value = "value1"/>
<xs:enumeration value = "value2"/>
</xs:restriction>
</xs:simpleType>
</xs:redefine>
</xs:schema>
Now the question: how I can reuse the redefinition of myType in particularUse1.xsd and particularUse2.xsd that they are not duplicated.
Ideally it would be another file reused.xsd:
<xs:simpleType name="myType">
<xs:restriction base = "myType">
<xs:enumeration value = "value1"/>
<xs:enumeration value = "value2"/>
</xs:restriction>
</xs:simpleType>
The reference from the file reused.xsd are used in particularUse1.xsd and particularUse2.xsd.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
答案实际上取决于您对 XSD 文件施加的限制、它们的使用以及您将用于使用 XML 架构文件的工具。
该图显示了问题所描述的文件布局。
由于您正在重新定义 use1.xsd 和 use2.xsd,我假设这些两个不可编辑(通常由其他人发布/维护)。
选项 1
一种方法是从specialUse#.xsd 中删除重新定义,将重新定义放入reused.xsd 中,并确保所有文件一起使用(例如,在.NET 中,您有XmlSchemaSet 对象,您需要将reused.xsd 和一个或两个specialUse#.xsd 添加到该集合中)。通过将文件置于范围内,PSVI 模型实际上将使用重新定义的 myType,即使任何特定Use#.xsd 文件未显式引用reused.xsd。
reused.xsd
specialUse1.xsd
specialUse2.xsd
选项 2
这个其他选项应该适用于大多数主流 XML 模式编辑器,并且在打开特定Use#.xsd 文件时它会为您提供重新定义的 myType;前提是您不介意将 use1.xsd 中的内容拉入specialUse2.xsd 的范围(基于插图)。
reused.xsd
pspecialUse2.xsd
无论选项,PSVI 准确显示您想要的内容。
The answer really depends on what constraints you put on the XSD files, the use of them, and the tooling that you'll be using to consume the XML Schema files.
This diagram shows the layout of the files as described by the question.
Since you're redefining use1.xsd and use2.xsd, I would assume that these two are not editable (typically released/maintained by someone else).
Option 1
One way is to remove the redefines from particularUse#.xsd, put the redefine in reused.xsd, and make sure that all the files are used together (for e.g., in .NET you have the XmlSchemaSet object, you need to have reused.xsd and one or both of the particularUse#.xsd added to the set). By having the files in scope, the PSVI model will actually use the redefined myType, even though the reused.xsd is not explicitly referenced by any particularUse#.xsd file.
reused.xsd
particularUse1.xsd
particularUse2.xsd
Option 2
This other option should work with most mainstream XML Schema editors and it would give you the redefined myType when opening just a particularUse#.xsd file; this provided you don't mind pulling in scope for particularUse2.xsd the content from use1.xsd (based on the illustration).
reused.xsd
particularUse2.xsd
Regardless of the option, the PSVI shows exactly what you want.