Can an element in xsd have an arbitrary name?

穿精又带淫゛_ 提交于 2019-12-24 02:24:13

问题


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?


回答1:


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.




回答2:


Try this: minOccurs="0" makes it non-mandatory.

<xs:choice>
  <xs:element name="test1" type="xs:string" />
  <xs:element name="test2" type="xs:string" />
  <xs:any minOccurs="0"/>
</xs:choice>



回答3:


It should be using entity references: The schema:

<xsd:schema xmlns:xsd="http://www.w3.org/1999/XMLSchema"> 
    <xsd:element name="Ferrari"/> 
    <xsd:element name="Lambo"/> 
    <xsd:element name="&supercar;"/>
</xsd:schema>

The XML document:

<?xml version="1.0"?>
<!DOCTYPE xsd:schema PUBLIC "-//W3C//DTD XML Schema 20000406//EN" "XMLSchema.dtd" [ <!ENTITY % p 'xsd:'> <!ENTITY % s ':xsd'>
<!ENTITY supercar 'McLaren'> 
]> 

To change you dynamic name, when defining the schema in the XML document, just change supercar's name. I did not tested this, but it should give you the idea on how to achieve what you're trying to do.



来源:https://stackoverflow.com/questions/9630765/can-an-element-in-xsd-have-an-arbitrary-name

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!