XSD和“最大发生”基于上下文
<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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
简短的回答是否定的,使用 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).
从您的代码来看,从逻辑上看,
Container/MyObject
和Container/Contained/MyObject
是不同的类型,尽管它们的名称不同。所以我在 XSD 中为这些声明了两种不同的类型。一种用于
Container/MyObject
,使用maxOccurs="1"
,另一种用于Contained/MyObject
,使用maxOccurs="whateverYouWant".
From your code it seems that logically, the
Container/MyObject
andContainer/Contained/MyObject
are different types, despite their names.So i'd declared two different types in XSD for these. One for
Container/MyObject
withmaxOccurs="1"
, and another forContained/MyObject
withmaxOccurs="whateverYouWant"
.