Reputation: 705
I'm generating an empty xml from user specified xml schema but i have confused with namespace declarations on attributes.
For example for this schema;
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns="http://tempuri.org/Product"
targetNamespace="http://tempuri.org/Product">
<xs:complexType name="ProductType">
<xs:sequence>
</xs:sequence>
<xs:attribute name="Id" type="xs:string" />
<xs:attribute name="Name" type="xs:string" />
</xs:complexType>
<xs:element name="Product" type="ProductType" />
</xs:schema>
I have generated the this;
<root>
<Product xmlns="http://tempuri.org/Product"
xmlns:ns1="http://tempuri.org/Product"
ns1:Id="1"
ns1:Name="2" />
</root>
But visual studio generating this;
<root>
<Product xmlns="http://tempuri.org/Product"
Id="1"
Name="2" />
</root>
Which one is correct ?
Update 1:
Whatever xml schema form attribute is set to, if I load the generated xml in XML DOM, Name attribute NamespaceURI is always empty string. (While product has namespace)
I'm performing xpath queries on these attributes and I cannot decide between should I always use namespace prefix or assume it is always same with parent element's namespace.
Example difference;
/*/ns1:Product/@Id
/*/ns1:Product/ns1:@Id
What is common use case for namespaced attribute syntax (qualified ?, unqualified ?) without knowing schema ?
Upvotes: 0
Views: 401
Reputation: 31610
I believe this is about form
attribute on xs:attribute element. If the from
attribute value is qualified
that the attribute described by the schema needs to be prefixed (i.e. belong to the namespace) otherwise it must not be prefixed. If the form
attribute is missing then the value comes from attributeFormDefault
on the xs:schema element. This attribute has the default value of unqualified
if it is not specified. Go to Xsd schema spec and search for 'attributeFormDefault' for all the details. I would say VS is right in this case.
Upvotes: 0
Reputation: 25034
According to sec. 3.2.2 of the XSD 1.0 spec, or sec. 3.2.2.2 of XSD 1.1, local attribute declarations (like those for the Id
and Name
attributes in your example) are namespace-qualified if
xsd:attribute
declaration has form="qualified"
, orxsd:attribute
declaration has no form
attribute, and the enclosing xsd:schema
element has attributeFormDefault="qualified"
.Otherwise they are unqualified (their expanded name has a null namespace value).
(Note that XSD 1.0 provides the XML-to-schema-component mappings for both top-level and local attribute declarations in the same section; you have to be careful to check which case is being described. XSD 1.1 subdivides the text more finely, to try to reduce confusion.)
So given the schema you show, it is the second instance, not the first, that is valid against the schema.
Upvotes: 0
Reputation: 122394
The latter is correct, as the schema does not specify attributeFormDefault="qualified"
on the root <xs:schema>
element. The default behaviour (attributeFormDefault="unqualified"
) is that <xs:attribute>
declarations that are nested inside a complex type have no namespace. If it were set to qualified
they would take the targetNamespace
of the schema, at which point your first alternative would be correct.
Upvotes: 1