Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can an element in xsd have an arbitrary name?

Tags:

xml

xsd

I am new to xsd validation, and am trying to validate if a choice tag has a specific group of elements but also has ones with arbitrarily named. Something like the following:

...
<xs:choice>
  <xs:element name="test1" type="xs:string" />
  <xs:element name="test2" type="xs:string" />
  <xs:element name="-some regex or something to specify arbitrary name here" type="xs:string" />
</xs:choice>
...

Xml:

...
<test1>example text</test1>
<test2>example text again</test2>
<exampleNode>example text</exampleNode>
...

Is this possible in xsd? To validate against new arbitrarily named nodes?

like image 224
Slamice Avatar asked Nov 16 '25 13:11

Slamice


1 Answers

You can use the xs:any wildcard to allow any element name, or any name within a particular set of namespaces. You can't restrict the name further (except by using assertions in XSD 1.1).

Using names with an internal structure (e.g. address-line-1, address-line-2, address-line-3) is invariably bad practice in XML design. In this case, a better design is <address-line nr="1"> etc. So the idea of allowing names that match some regular expression suggests poor design. In general, XML Schema has been designed to make it difficult or impossible to write a schema for a poorly designed vocabulary.

like image 176
Michael Kay Avatar answered Nov 18 '25 15:11

Michael Kay