Restrict type of xsd:any to xsd:string only?

余生长醉 提交于 2019-12-01 20:54:48

XSD 1.1

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

XSD 1.0

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

Attributes

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>

Users could then add extraInfo along these lines:

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

and

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

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

Elements (update per OP question in comments)

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>

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!