XML Schema - 是否可以在整个文档中只允许某个元素一次? [英] XML Schema - Is it possible to allow a certain element only once in the whole document?

查看:17
本文介绍了XML Schema - 是否可以在整个文档中只允许某个元素一次?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想扩展 XHTML 以将其用作模板语言.我有一个标记标签,如

I want to extend XHTML to use it as a template language. I have a marker tag like

 <mylang:content/>

我希望这个标记出现在 divp 可以出现的地方,但在整个文档中只出现一次.我认为 XML Schema 是不可能的,但也许一些 XML Schema 专家更了解.

I want this marker tag to appear where div or p can appear, but only once in the complete document. I think it is not possible with XML Schema, but maybe some XML Schema guru knows better.

当包含元素被允许显示为无界时,是否可以在整个文档中只允许某个元素一次?

Is it possible to allow a certain element only once in the whole document when the containing element is allowed to appear unbounded?

推荐答案

如果你知道根元素将是 ,那么我认为你可以在 doc 元素上定义一个约束

If you know that the root element will be , then I think you can define a constraint on the doc element

<xs:unique name="one-content">
  <xs:selector xpath=".//mylang:content"/>
  <xs:field xpath="."/>
</xs:unique>

这表示所有 mylang:content 后代必须具有不同的字符串值;但是由于元素被限制为空,如果每个元素都必须是不同的,那么只能有一个元素.

This says that all mylang:content descendants must have distinct string values; but since the element is constrained to be empty, if every element must be distinct then there can only be one element.

当然,在 XSD 1.1 中,断言会变得更容易.

In XSD 1.1 of course it becomes much easier with assertions.

这篇关于XML Schema - 是否可以在整个文档中只允许某个元素一次?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆