XSD:如何限制派生复杂类型的枚举值?

发布于 2024-08-21 18:37:31 字数 1310 浏览 4 评论 0原文

给出以下示例:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:complexType name="Book" abstract="true">
        <xs:sequence>
            <xs:element name="titel" type="xs:string">
            </xs:element>
            <xs:element name="bookCode" type="BookEnum"/>
        </xs:sequence>
    </xs:complexType>
    <xs:complexType name="Lyric">
        <xs:complexContent>
            <xs:extension base="Book">
                <xs:sequence>
                    <xs:element name="author" type="xs:string">
                    </xs:element>
                </xs:sequence>
            </xs:extension>
        </xs:complexContent>
    </xs:complexType>
    <xs:simpleType name="BookEnum">
        <xs:restriction base="xs:int">
            <xs:enumeration value="Paperback"/>
            <xs:enumeration value="Hardcover"/>
            <xs:enumeration value="Liporello"/>
            <xs:enumeration value="None"/>
        </xs:restriction>
    </xs:simpleType>
</xs:schema>

Lyric 源自 Book。我想将复杂类型“Lyric”的 BookEnum 的可能值限制为“Paperback”。

“None”、“Liporello”和“Hardcover”不应再是“Lyric”的有效值。这可以在xsd中完成吗?

Given the following example:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:complexType name="Book" abstract="true">
        <xs:sequence>
            <xs:element name="titel" type="xs:string">
            </xs:element>
            <xs:element name="bookCode" type="BookEnum"/>
        </xs:sequence>
    </xs:complexType>
    <xs:complexType name="Lyric">
        <xs:complexContent>
            <xs:extension base="Book">
                <xs:sequence>
                    <xs:element name="author" type="xs:string">
                    </xs:element>
                </xs:sequence>
            </xs:extension>
        </xs:complexContent>
    </xs:complexType>
    <xs:simpleType name="BookEnum">
        <xs:restriction base="xs:int">
            <xs:enumeration value="Paperback"/>
            <xs:enumeration value="Hardcover"/>
            <xs:enumeration value="Liporello"/>
            <xs:enumeration value="None"/>
        </xs:restriction>
    </xs:simpleType>
</xs:schema>

Lyric derives from Book. I would like to restrict the possible values of the BookEnum for the complexType "Lyric" to be "Paperback".

"None", "Liporello" and "Hardcover" should no longer be valid values for "Lyric". can this be done in xsd?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

甜心小果奶 2024-08-28 18:37:31

不,不幸的是你不能这样做。没有办法限制这样的枚举,而且同时扩展和限制复杂类型也会遇到麻烦。 这里有一篇很好的文章,介绍了您可以使用枚举做什么< /a>.

也许可以考虑自下而上地工作:定义一个基本的书籍类型,其中几乎没有任何内容;然后定义一个 LyricType,它是基本类型和其他一些值的联合;在子类型中携带 bookCode。这并不理想,但遗憾的是,XSD 并不总是与面向对象语言保持一致。

Nope, unfortunately you cannot do that. There is no way to restrict an enumeration like that, and you also would have trouble both extending and restricting a complex type at the same time. There is a good article what you can do with enumerations here.

Maybe consider working bottom-up instead: define a basic book type with almost nothing in it; then define a LyricType that is a union of the basic type and some more values; carry the bookCode in the sub-types. It's not ideal, but alas, XSD does not always align with object-oriented languages.

流心雨 2024-08-28 18:37:31

最终..您试图使用另一个节点(父节点或兄弟节点等)的值来验证节点..这当然是不可能的..

Ultimately .. you are trying to validate a node using value of the other (Parent or sibling or etc) .. Which is certainly not possible ..

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