如何在 XSD 中重用 simpleType 重定义

发布于 2024-12-09 02:38:06 字数 2726 浏览 0 评论 0原文

假设在 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.xsduse2.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.xsdpreferredUse2.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.xsdpspecialUse2 .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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦在深巷 2024-12-16 02:38:06

答案实际上取决于您对 XSD 文件施加的限制、它们的使用以及您将用于使用 XML 架构文件的工具。
该图显示了问题所描述的文件布局。

问题所描述的布局。

由于您正在重新定义 use1.xsd 和 use2.xsd,我假设这些两个不可编辑(通常由其他人发布/维护)。

选项 1

一种方法是从specialUse#.xsd 中删除重新定义,将重新定义放入reused.xsd 中,并确保所有文件一起使用(例如,在.NET 中,您有XmlSchemaSet 对象,您需要将reused.xsd 和一个或两个specialUse#.xsd 添加到该集合中)。通过将文件置于范围内,PSVI 模型实际上将使用重新定义的 myType,即使任何特定Use#.xsd 文件未显式引用reused.xsd。

布局选项 1

reused.xsd

<?xml version="1.0" encoding="utf-8"?>
<!--XML Schema generated by QTAssistant/XSR Module (http://www.paschidev.com)-->
<xsd:schema attributeFormDefault="unqualified" elementFormDefault="qualified"     xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <xsd:redefine schemaLocation="definition.xsd">
    <xsd:simpleType name="myType">
      <xsd:restriction base="myType">
        <xsd:enumeration value="value1"/>
        <xsd:enumeration value="value2"/>
    </xsd:restriction>
    </xsd:simpleType>
  </xsd:redefine>
</xsd:schema>

specialUse1.xsd

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> 
    <xs:include schemaLocation="use1.xsd"/>
</xs:schema> 

specialUse2.xsd

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> 
    <xs:include schemaLocation="use2.xsd"/>
</xs:schema>

选项 2

这个其他选项应该适用于大多数主流 XML 模式编辑器,并且在打开特定Use#.xsd 文件时它会为您提供重新定义的 myType;前提是您不介意将 use1.xsd 中的内容拉入specialUse2.xsd 的范围(基于插图)。

布局选项 2

reused.xsd

<?xml version="1.0" encoding="utf-8"?>
<!--XML Schema generated by QTAssistant/XSR Module (http://www.paschidev.com)-->
<xsd:schema attributeFormDefault="unqualified" elementFormDefault="qualified"     xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <xsd:redefine schemaLocation="use1.xsd">
    <xsd:simpleType name="myType">
      <xsd:restriction base="myType">
        <xsd:enumeration value="value1"/>
        <xsd:enumeration value="value2"/>
    </xsd:restriction>
    </xsd:simpleType>
  </xsd:redefine>
</xsd:schema>

pspecialUse2.xsd

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> 
    <xs:include schemaLocation="reused.xsd"/>
    <xs:include schemaLocation="use2.xsd"/>
</xs:schema>

无论选项,PSVI 准确显示您想要的内容。

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.

The layout 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.

Layout option 1

reused.xsd

<?xml version="1.0" encoding="utf-8"?>
<!--XML Schema generated by QTAssistant/XSR Module (http://www.paschidev.com)-->
<xsd:schema attributeFormDefault="unqualified" elementFormDefault="qualified"     xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <xsd:redefine schemaLocation="definition.xsd">
    <xsd:simpleType name="myType">
      <xsd:restriction base="myType">
        <xsd:enumeration value="value1"/>
        <xsd:enumeration value="value2"/>
    </xsd:restriction>
    </xsd:simpleType>
  </xsd:redefine>
</xsd:schema>

particularUse1.xsd

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> 
    <xs:include schemaLocation="use1.xsd"/>
</xs:schema> 

particularUse2.xsd

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> 
    <xs:include schemaLocation="use2.xsd"/>
</xs:schema>

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).

Layout option 2

reused.xsd

<?xml version="1.0" encoding="utf-8"?>
<!--XML Schema generated by QTAssistant/XSR Module (http://www.paschidev.com)-->
<xsd:schema attributeFormDefault="unqualified" elementFormDefault="qualified"     xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <xsd:redefine schemaLocation="use1.xsd">
    <xsd:simpleType name="myType">
      <xsd:restriction base="myType">
        <xsd:enumeration value="value1"/>
        <xsd:enumeration value="value2"/>
    </xsd:restriction>
    </xsd:simpleType>
  </xsd:redefine>
</xsd:schema>

particularUse2.xsd

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> 
    <xs:include schemaLocation="reused.xsd"/>
    <xs:include schemaLocation="use2.xsd"/>
</xs:schema>

Regardless of the option, the PSVI shows exactly what you want.

PSVI showing the redefined type

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文