Search code examples
xsd

XSD variable element names


Is it possible in XSD to name an element dynamically?

I have a complexType with a varying (but limited) number of elements (x-n), each of which has a complicated substructure. I can copy and paste one (x-1) and just change the number for the name of each of the copies (x-2, x3, and so on), but it'd be cleaner if I didn't have to.

For example, as it is now:

<xs:schema attributeFormDefault="unq1" elementFormDefault="q1" xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="r1">
        <xs:complexType><xs:sequence>
            <xs:element name="w-s">
                <xs:complexType><xs:sequence maxOccurs="4">
                    <xs:element name="x-1" minOccurs="0">
                        <xs:complexType><xs:sequence>
                            <!-- long tedious substructure goes here -->
                        </xs:sequence></xs:complexType>
                    </xs:element>
                    <xs:element name="x-2" minOccurs="0">
                        <xs:complexType><xs:sequence>
                            <!-- long tedious substructure goes here -->
                        </xs:sequence></xs:complexType>
                    </xs:element>
                    <xs:element name="x-3" minOccurs="0">
                        <xs:complexType><xs:sequence>
                            <!-- long tedious substructure goes here -->
                        </xs:sequence></xs:complexType>
                    </xs:element>
                    <xs:element name="x-4" minOccurs="0">
                        <xs:complexType><xs:sequence>
                            <!-- long tedious substructure goes here -->
                        </xs:sequence></xs:complexType>
                    </xs:element>
                </xs:sequence></xs:complexType>
            </xs:element>
        </xs:sequence></xs:complexType>
    </xs:element>
</xs:schema>

Looking through w3schools now (https://www.w3schools.blog/xsd-xml-schema-definition-tutorial), the answer is not jumping out at me yet, and it's starting to look like the answer is "No.".


Solution

  • Judging by these stackoverflows, the answer appears to be "Not only no, but also you're wrong for asking" :-p

    xml schema list of incremental element name

    Can't design xsd schema - because of a variable element name

    XML variable tag names

    The better solution is to restructure the XSD so that the x-n element has a sub-element indicating the value of n, like so:

    <xs:schema attributeFormDefault="unq1" elementFormDefault="q1" xmlns:xs="http://www.w3.org/2001/XMLSchema">
        <xs:element name="r1">
            <xs:complexType><xs:sequence>
                <xs:element name="w-s">
                    <xs:complexType><xs:sequence maxOccurs="4">
                        <xs:element name="x" minOccurs="0">
                            <xs:complexType><xs:sequence>
                                <xs:element name="x-number">
                                <!-- long tedious substructure goes here -->
                            </xs:sequence></xs:complexType>
                        </xs:element>
                    </xs:sequence></xs:complexType>
                </xs:element>
            </xs:sequence></xs:complexType>
        </xs:element>
    </xs:schema>