问题
I'm creating a xml document based on some conditions in my app. the number of elements inside my xml is always variable. For instance, one time it may look like:
<TransactionTypes>
<X value="false" text="" />
<O value="false" text="" />
<E value="false" text="" />
<P value="false" text="" />
<C value="false" text="" />
<K value="false" text="" />
</TransactionTypes>
and the other time like:
<TransactionTypes>
<TT value="false" text="" />
<EP value="false" text="" />
<PY value="false" text="" />
</TransactionTypes>
So the child elements inside of TransactionTypes always vary in terms of the name of the elements, but they always have the value and text attributes. How can i make a XSD for this?
回答1:
I don't think you can build a XSD restriction on a wildcard and then still specify the attributes of the wild-card element.
The only possible tag might be "any": http://www.w3schools.com/schema/schema_complex_any.asp
And what attributes "any" may have cannot be defined. I don't think that there is another solution. As you did say "there can be any kind of element" ... You would definitely do better if you could define all possible elements that are child of "TransactionTypes". No matter how many there are. You could map all those child element to the same complexType and define the attributes value/text then only one time still. However wildcard will not work.
Sebastian
回答2:
In XSD 1.1 you can define an assertion on the TransactionTypes element:
<xs:assertion test="every $c in * satisfies @value='false' and @text=''"/>
However, it's worth pointing out that XSD is not designed to allow you to express arbitrary constraints on your XML, but rather to describe the kind of constraints you find in what the XSD designers consider to be well-designed XML; and this XML doesn't appear to fall into that category.
来源:https://stackoverflow.com/questions/12929550/xsd-from-variable-number-of-xml-elements