1

I have an XSD file as shown below. The problem is the nodes for City and State are dynamic, and will consist of actual City and State names. Is there any way to use a wildcard name for these elements?

<?xml version="1.0"?>
<xs:schema attributeFormDefault="unqualified"
           elementFormDefault="qualified" 
           xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="Root">
<xs:complexType>
  <xs:sequence>
    <xs:element minOccurs="0" name="State">
      <xs:complexType>
        <xs:sequence>
          <xs:element minOccurs="0" name="City">
            <xs:complexType>
              <xs:sequence>
                <xs:element minOccurs="0" name="cafeID" type="xs:string" />
                <xs:element minOccurs="0" name="cafeName" type="xs:string" />
                <xs:element minOccurs="0" name="cafeState" type="xs:string" />
                <xs:element minOccurs="0" name="cafeCity" type="xs:string" />
                <xs:element minOccurs="0" name="cafeStreetName" type="xs:string" />
                <xs:element minOccurs="0" name="cafeZip" type="xs:string" />
                <xs:element minOccurs="0" name="cafeContact" type="xs:string" />
                <xs:element minOccurs="0" name="cafeEmail" type="xs:string" />
                <xs:element minOccurs="0" name="hasPickup" type="xs:string" />
                <xs:element minOccurs="0" name="hasDriveThru" type="xs:string" />
              </xs:sequence>
            </xs:complexType>
          </xs:element>
        </xs:sequence>
      </xs:complexType>
    </xs:element>
  </xs:sequence>
</xs:complexType>

kjhughes
  • 106,133
  • 27
  • 181
  • 240
PixelPaul
  • 2,609
  • 4
  • 39
  • 70

1 Answers1

1

Don't let element names vary across an unknown, open-ended domain because you'll frustrate efforts of

  1. Data modelers to constrain content models for these elements and of elements containing these elements.
  2. Consumers of this data to write handlers and transformations for these elements.

If you use XML as a markup language and markup the city and state names with city and state elements or attributes, you'll have much less trouble defining an XSD and creating applications for it.

kjhughes
  • 106,133
  • 27
  • 181
  • 240
  • I understand that, but the source xml file is coming from a client so I don't have any control over it. – PixelPaul Jun 20 '16 at 19:57
  • See [XSD element name pattern matching](http://stackoverflow.com/q/25568670/290085) for how to enforce that element names match a pattern, but that's about as far as I'd go with dynamic element names. In your case, push back on this terrible design, or punt via `xsd:any` (assuming, as [C.M. Sperberg-McQueen alludes to](http://stackoverflow.com/questions/37929416/using-wildcards-in-xsd-element-names/37930632?noredirect=1#comment63312591_37929416), that you're assured that element names will be purely NCNAMEs). – kjhughes Jun 20 '16 at 20:21
  • How would `xsd:any` fix this? – PixelPaul Jun 21 '16 at 00:47
  • I said you could [***punt***](http://ell.stackexchange.com/questions/73783/what-do-i-do-now-punt-what-is-meant-by-punt-here/73784) via `xsd:any` -- not ***fix*** this. – kjhughes Jun 21 '16 at 01:12