XSD和“最大发生”基于上下文

发布于 2025-01-06 22:48:27 字数 1251 浏览 1 评论 0原文

<Container>
    <MyObject>
        <property1>abcd</property1>
        <property2>
            <Version>3.2</Version>
        </property2>
    </MyObject>

    <Contained>

        <MyObject>
            <property1>something</property1>
            <property2>
                <Version>1.1</Version>
                <Version>1.2</Version>
                <Version>1.6</Version>
            </property2>
        </MyObject>

        <MyObject>
            <property1>something else</property1>
            <property2>
                <Version>2.3</Version>
                <Version>2.5</Version>
                <Version>2.6</Version>
            </property2>
        </MyObject>

    </Contained>
</Container>

给定这个 xml 结构,在相应的 xsd 文件中,我可以对 Version 属性设置一个最大出现次数限制,这样如果 MyObject 直接包含在 Container 中,它应该恰好出现一次,但如果 MyObject 包含在 Contained 中,那么它可能会出现一次。发生任意多次?

基于类似问题,我倾向于我认为这不可能,但我想确定一下。

<Container>
    <MyObject>
        <property1>abcd</property1>
        <property2>
            <Version>3.2</Version>
        </property2>
    </MyObject>

    <Contained>

        <MyObject>
            <property1>something</property1>
            <property2>
                <Version>1.1</Version>
                <Version>1.2</Version>
                <Version>1.6</Version>
            </property2>
        </MyObject>

        <MyObject>
            <property1>something else</property1>
            <property2>
                <Version>2.3</Version>
                <Version>2.5</Version>
                <Version>2.6</Version>
            </property2>
        </MyObject>

    </Contained>
</Container>

Given this xml structure, in the corresponding xsd file, can I put a max occurs limit on Version property, such that if MyObject is directly contained in Container, it should occur exactly one time, but if MyObject is contained in Contained, then it may occur any number of times?

Based on a similar question, I am inclined to think it it not possible, but I would like to be sure.

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

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

发布评论

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

评论(2

马蹄踏│碎落叶 2025-01-13 22:48:27

简短的回答是否定的,使用 XSD 1.0 无法完成此操作。如果您可以升级版本或添加混合 Schematron 断言,那么您就可以。或者,如果纯粹的XSD 1.0是唯一的答案,那么我会尝试找到一种重塑的方法; @penartur 可以让您知道从哪里开始(我会通过扩展重用,确保在内容模型的末尾对“差异”进行建模,以允许扩展发挥其魔力)。

The short answer is no, it cannot be done using XSD 1.0. If you can move up the version or add to the mix Schematron assertions, then you can. Alternatively, if a pure XSD 1.0 is the only answer, then I would try to find a way to remodel; @penartur's could give you an idea where to start with (I would instead reuse by extension, ensuring that the "differences" are modeled at the end of the content model, to allow extension to work its magic).

牛↙奶布丁 2025-01-13 22:48:27

从您的代码来看,从逻辑上看, Container/MyObjectContainer/Contained/MyObject 是不同的类型,尽管它们的名称不同。

所以我在 XSD 中为这些声明了两种不同的类型。一种用于 Container/MyObject,使用 maxOccurs="1",另一种用于 Contained/MyObject,使用 maxOccurs="whateverYouWant".

From your code it seems that logically, the Container/MyObject and Container/Contained/MyObject are different types, despite their names.

So i'd declared two different types in XSD for these. One for Container/MyObject with maxOccurs="1", and another for Contained/MyObject with maxOccurs="whateverYouWant".

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