XML Schema: AttributeGroup without supporting element? Duplicate attributes?
I've been tasked with writing a parser for xml documents with a supporting schema that was created by people purportedly smarter and more experi开发者_如何学Goenced than I. I am no xml expert, however, from what I can tell, I believe there are some considerations missing from their schema.
Here's a simplified version of the schema I've been given:
<?xml version="1.0"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
<xsd:attributeGroup name="Group1">
<xsd:attribute name="attrib1" type="xsd:NMTOKENS"/>
<xsd:attribute name="attrib2" type="xsd:NMTOKENS"/>
</xsd:attributeGroup>
</xsd:schema>
Before I go back to my clients and criticize their schema design, I'd really appreciate the collective stackoverflow intellect to confirm or deny the following for me:
- An attributeGroup should be used by some sort of element within a schema. It does not make sense to just have an attributeGroup standing alone in a schema. (unless this schema is intended to be imported into another)
An attribute within an attributeGroup can only occur once within a single element of a valid xml document. for example:
<?xml version="1.0"?> <element1 attrib1="first second third" attrib1="first second third"/>
is not valid because there is a duplicate 'attrib1', correct?
- I have been told that I may receive documents with duplicate attributes. I would like to tell my clients that the schema needs to reflect that possibility (if that's even possible...). Is there a way to define an attribute which can occur more than once? I'm aware of the 'maxOccurs' attribute but I don't believe that is valid for an attribute definition.
Thanks in advance for your input.
An attributeGroup should be used by some sort of element within a schema. It does not make sense to just have an attributeGroup standing alone in a schema.
Well, it depends if it makes sense or not. In XML attributes are like "extra information attached to an element". So in the instance document an attribute cannot exist without an element that "hosts" it. To have an attribute in the instance document requires that 1) there is an element that has this attribute 2) this attribute is somehow defined or referenced in the schema. If no element in the schema is defined (allowed) to use a certain attribute, then that attribute cannot appear in the instance document.
Then again, defining attributes that are not used is allowed, it is not an error. In some cases this could be reasonable, for example to ease adoption of features planned for the future or to allow importing or including the schema into another schema which would make use of those attributes.
An attribute within an attributeGroup can only occur once within the valid xml document. for example:
<?xml version="1.0"?> <element1 attrib1="first second third" attrib1="first second third"/>
is not valid because there is a duplicate 'attrib1', correct?
Your code example is clear: that is not allowed in XML. As stated in the XML recommendation: "An attribute name MUST NOT appear more than once in the same start-tag or empty-element tag." Then again, the claim you wrote above your code example is not exactly correct. It is possible that the same attribute can appear multiple times within a valid XML document. It just has to appear on different elements. Duplicating attributes on one element is not allowed. However, you can duplicate elements so that they all have the same attributes.
Not allowed:
<element1 attrib1="first second third" attrib1="first second third"/>
Allowed:
<element1 attrib1="first second third"/>
<element1 attrib1="first second third"/>
As a corner case, duplicate attribute names applies to expanded names of the attributes.
<element ns1:attrib="value" ns2:attrib="value"/>
is not allowed, if both prefixes ns1
and ns2
resolve to same namespace URI.
精彩评论