XSD 限制

发布于 2024-07-30 05:10:47 字数 327 浏览 2 评论 0原文

是否可以使用 XSD 将节点名称限制为枚举,然后基于此枚举添加另一个限制?

例如,我有这个 xml:

<a>
    <b name="string" value="hello">
    <b name="integer" value="123">
</a>

我希望“b”节点具有枚举 {“string”,“integer”} 中的名称属性。 然后,如果它是“字符串”,我希望该“值”属性是 xs:string 的类型, 如果它是“整数”,我希望该“值”属性是 xs:integer 类型。

Is it possible using XSD to restrinct node names to enumeration, and then based on this enumeration add another restrictions?

In example, I have this xml:

<a>
    <b name="string" value="hello">
    <b name="integer" value="123">
</a>

I want "b" nodes have name attribute from enumeration { "string", "integer" }.
Then if it's "string" I want that "value" attribute to be type of xs:string,
and if it's "integer" I want that "value" attribute to be type of xs:integer.

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

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

发布评论

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

评论(3

甜扑 2024-08-06 05:10:47

不可以。您不能在 XSD 中执行此操作。 本质上,您有 2 个不同类型的 。 这违反了元素一致性规则

您有几个选项,

  1. 在您的应用程序中强制执行模式之外的规则。 这就是我要做的。
  2. 正如其他人提到的,使用像 Schematron 这样的验证语言。
  3. 切换到更强大的架构语言,例如 Relax NG

No. You can't do this in XSD. In essence, you have 2 <b>s with different types. This violates the Element Consistency rule.

You have a few options,

  1. Enforce the rules outside schema, in your application. This is what I will do.
  2. Use a validation language like Schematron, as mentioned by others.
  3. Switch to a more powerful schema language like Relax NG.
≈。彩虹 2024-08-06 05:10:47

您可以进行某些限制,例如您可以限制 name 属性的值来自给定列表 - 但您无法表达“如果名称是字符串,则值的类型”之间的这种关系必须是 XML 架构中的 xs:string"。

您必须使用其他技术(例如 Schematron),或者在您的应用程序代码。

马克

You can do certain limit, e.g. you can limit that the values of your name attribute come from a given list - but you cannot express these kind of relations between "if name is string, then the type of value must be xs:string" in the XML schema.

You'll have to either use some other technique (like Schematron), or check this in your app code.

Marc

烟燃烟灭 2024-08-06 05:10:47

不。但是您将名称视为类型 - 并且 XML 模式确实对由字符串值确定的元素的complexType 有一定的支持。 但是,您必须在 XML 文档中使用属性名称“xsi:type”,因此它看起来像这样:

<a>
    <b xsi:type="string" value="hello">
    <b xsi:type="integer" value="123">
</a>

恐怕这是 XML Schema 能做的最好的事情了。 官方入门手册中有更多详细信息(不幸的是,这可能非常令人困惑):
http://www.w3.org/TR/xmlschema-0/#UseDerivInInstDocs< /a>

No. But you're treating name like a type - and XML Schema does have some support for the complexType of an element being determined by a string value. However, you have to use the attribute name "xsi:type" in your XML document, so it would look like this:

<a>
    <b xsi:type="string" value="hello">
    <b xsi:type="integer" value="123">
</a>

That's the best that XML Schema can do I'm afraid. More details in the official primer (which can be pretty confusing, unfortunately):
http://www.w3.org/TR/xmlschema-0/#UseDerivInInstDocs

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