2

For example, if we have a file looking like this:

<?xml version="1.0" encoding="utf-8"?>
<Cars>
    <Audi>
        <PlateNumber>9488</PlateNumber>
        <Color>blue</Color>
    </Audi>
    <BMW>
        <PlateNumber>12345</PlateNumber>
        <Color>red</Color>
    </BMW>
</Cars>

Where the elements inside the element Cars can have any name, but they always have the same child nodes, PlateNumber and Color. Is something like this possible to define in a schema?

kjhughes
  • 106,133
  • 27
  • 181
  • 240
Njubster
  • 69
  • 2
  • 7

1 Answers1

2

XSD constraints are heavily anchored on XML component (element and attribute) names. There is a provision to allow any element to be at a given location via xs:any, but once employed, there are very limited ways to add further constraints.

Consider adjusting your design to adhere to the general principle of using similar names for similar things:

<Cars>
    <Car>
        <Make>Audi</Make>
        <PlateNumber>9488</PlateNumber>
        <Color>blue</Color>
    </Car>
    <Car>
        <Make>BMW</Make>
        <PlateNumber>12345</PlateNumber>
        <Color>red</Color>
    </Car>
</Cars>

Then you'll have no problem expressing the commonality between Car elements.

Alternative approach: XSD element substitution group example?

kjhughes
  • 106,133
  • 27
  • 181
  • 240