1

Guys.

I have a following XSD parts:

<!-- Enumeration of supported types -->
<xs:simpleType name="SupportedTypes">
    <xs:restriction base="xs:string">
        <xs:enumeration value="creditCard" />
        <xs:enumeration value="directDebit" />
        <xs:enumeration value="paypal" />
        <xs:enumeration value="webmoney" />
    </xs:restriction>
</xs:simpleType>

This works perfectly when I want to check the attributes values for instace (XSD below):

<!-- this is what I know how to make, not what I want to do :) -->
<xs:element name="PaymentType" type="SupportedTypes" />

But what if I have a list of payment types, e.g. (XML below):

<!-- existing XML that I need to validate (preferably by the same enum) -->
<paymentTypes>
    <creditCard price="123.50" />
    <webmoney price="100.00" />
    <directDebit never="true" />
</paymentTypes>

Is there a way to check the tag names agains an existing SupportedTypes type?

Cheers!

t1gor
  • 1,244
  • 12
  • 25

1 Answers1

1

No, you can't use a declaration of a simple type to specify the names of child elements.

You could have a list of enumerations via xs:list, but you also want to associate price data with each entry, so a xs:list of SupportedTypes isn't really want you want.

You could have an attribute, @type, whose type could be SupportedTypes:

<paymentTypes>
    <paymentType type="creditCard" price="123.50" />
    <paymentType type="webmoney" price="100.00" />
    <paymentType type="directDebit" never="true" />
</paymentTypes>

Under XSD 1.1, you could even use Conditional Type Assignment to assign the type of the associated element based on the value of @type.

Or, you could keep your current XML and simply specify a normal content model for paymentTypes consisting of a sequence/all/choice of multiple, normally defined child elements, creditCard, webmoney, and directDebit.


Update: Or, as Michael Kay adds in the comments, you could use a substitution group, as shown in XSD element substitution group example.

Community
  • 1
  • 1
kjhughes
  • 106,133
  • 27
  • 181
  • 240
  • 2
    Or use a substitution group, where paymentTypes has a content model as a repeating sequence of paymentType, and paymentType is an abstract element declaration with creditCard, webmoney, and directDebit in its substitution group. – Michael Kay Mar 09 '17 at 07:47
  • @MichaelKay could you please make an example? – t1gor Mar 09 '17 at 08:38
  • 1
    @MichaelKay: True, thanks. t1gor: See [XSD element substitution group example](http://stackoverflow.com/questions/39868769/xsd-element-substitution-group-example). – kjhughes Mar 09 '17 at 13:15
  • 1
    @t1gor My general approach on this site is to point people in the right direction, not to give them a free ride to their destination. There are plenty of resources you can use once you know what construct you need to use. – Michael Kay Mar 18 '17 at 08:50
  • Somehow I really missed the substritution groups. Thanks a lot! – t1gor Aug 22 '17 at 07:40