问题
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.
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>
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: Attribute 'type' cannot appear in element 'any'.
How can I get it so I can pass in 42 elements of unknown name but have them as type string?
Edit
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>
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.)
回答1:
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
.
来源:https://stackoverflow.com/questions/28746575/restrict-type-of-xsdany-to-xsdstring-only