将 xsd:any 的类型限制为仅 xsd:string? [英] Restrict type of xsd:any to xsd:string only?

查看:42
本文介绍了将 xsd:any 的类型限制为仅 xsd:string?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我正在编写一个新的 XSD,但我遇到了一个小问题.现在我承认我在这些方面不是最好的,但我本以为我所做的应该有效,但它没有.

So I'm writing a new XSD and I've come across a little issue. Now I'll admit I'm not the best with these, but I would have thought what I have done should have worked but it doesn't.

我所追求的是我有一个名为 extraInfo 的元素,该元素最多可以有 42 个具有任何名称但只能是字符串类型的子元素.这是我所拥有的:

What I am after is I have an element called extraInfo and this element can have up to 42 child elements with any name but only of type string. Here is what I have:

<xsd:element name="extraInfo" minOccurs="0" maxOccurs="1">
    <xsd:annotation>
       <xsd:documentation></xsd:documentation>
    </xsd:annotation>
    <xsd:complexType>
       <xsd:sequence>
           <xsd:any minOccurs="0" maxOccurs="42" type="xsd:string" />
       </xsd:sequence>
    </xsd:complexType>
</xsd:element>

我会认为只要我将类型作为 xsd:string 传递,它就应该只接受这些元素中的这种类型,但是元素名称可以随意命名.但是我在

I would have thought that as long as I am passing in the type as xsd:string it should only accept this type in these elements, but the element name can be named whatever it wants. However I am getting an error under the type attribute of

s4s-att-not-allowed:属性type"不能出现在元素any"中.

s4s-att-not-allowed: Attribute 'type' cannot appear in element 'any'.

如何获取它以便我可以传入 42 个未知名称的元素,但将它们作为类型字符串?

How can I get it so I can pass in 42 elements of unknown name but have them as type string?

编辑

所以基本上我们可能会有一个客户向我们传递以下信息

So basically we might have one client pass us the following

<extraInfo>
    <logoUrl>http://www.google.com/logo.png</logoUrl>
    <cssUrl>http://www.google.com/main.css</cssUrl>
</extraInfo>

但是另一个客户超过了我们

but another client pass us

<extraInfo>
    <headerText>Hello World</headerText>
    <footerText>Goodbye World</footerText>
</extraInfo>

我们无法保证元素名称的名称.我们所能保证的只是类型,也就是字符串,并且我们最多允许传入 42 个元素.(没有理由使用 42,除了它可以解决所有问题,对吗?基本上是从帽子里挑出来的.)

We can't guarantee what the element names are called. All we can guarantee is the type and that is string and that we will allow up to 42 elements to be passed in. (No reason for 42 except its the answer to everything right? Picked out of a hat basically.)

推荐答案

XSD 1.1

在评论中看到@IanRoberts 的好建议,关于断言 extraInfo 的子元素下不存在孙元素.

XSD 1.1

See @IanRoberts fine suggestion in the comments regarding asserting that no grandchild elements exist under the children of extraInfo.

如果您想更好地控制 extraInfo 子元素的类型,则必须先验地指定它们的名称.

If you want more control over the type of the extraInfo child elements, you'll have to specify their names a priori.

属性

或者,为什么不利用属性值已经被限制为没有子元素的事实,而是使用 xsd:anyAttribute 来代替:

Or, why not leverage the fact that attribute values already are constrained not to have subelements and use xsd:anyAttribute instead:

  <xsd:element name="extraInfo">
    <xsd:complexType>
      <xsd:anyAttribute processContents="skip" />
    </xsd:complexType>
  </xsd:element>

然后用户可以沿着这些行添加extraInfo:

Users could then add extraInfo along these lines:

<extraInfo
     logoUrl="http://www.google.com/logo.png"
     cssUrl="http://www.google.com/main.css"/>

<extraInfo
    headerText="Hello World"
    footerText="Goodbye World"/>

考虑到您希望只允许字符串值,这很自然.

which would be natural given that you wish to allow only string values.

元素(根据评论中的 OP 问题更新)

Elements (update per OP question in comments)

如果最大 42 约束对您很重要,您可以使用诸如

If the max of 42 constraint is important to you, you could go meta with a structure such as

<extraInfo>
    <item name="logoUrl" value="http://www.google.com/logo.png"/>
    <item name="cssUrl" value="http://www.google.com/main.css"/>
</extraInfo>

然后您可以通过 @maxOccurs 在 XSD 1.0 中简单地限制 item 元素的数量.

Then you could restrict the number of item elements trivially in XSD 1.0 via @maxOccurs.

这篇关于将 xsd:any 的类型限制为仅 xsd:string?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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