XML Validation with XSD and xmlns = ""
I have XML:
<?xml version="1.0" encoding="utf-8"?>
<song id="id1"
xmlns="urn:Test:Song:1.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="urn:Test:Song:1.0 song.xsd">
<name>name1</name>
</song>
It cannot validate against XSD:
<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns="urn:Test:Song:1.0"
targetNamespace="urn:Test:Song:1.0"
xmlns:xs="http://www.w3.org/2001/XMLSchema" >
<xs:element name="song">
<xs:complexType>
<xs:sequence>
<xs:element name="name" type="xs:string" minOccurs="0" />
</xs:sequence>
<xs:attribute name="id" type="xs:string" />
</xs:complexType>
</xs:element>
</xs:schema>
in Eclipse and Visual Studio. In Eclipse error: cvc-complex-type.2.4.a: Invalid content was found starting at element 'name'. One of "{name}" is expected.
Validation for XML:
<?xml version="1.0" encoding="utf-8"?>
<song id="id1"
xmlns="urn:Test:Song:1.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="urn:Test:Song:1.0 song.xsd">
<name xmlns="">name1</name>
</song>
The only difference is: xmlns = "from the name element. Is there a way to make the validation succeed with the first one without using" no namespace "? What exactly causes the first XML to fail?
source to share
You need to add the attribute elementFormDefault="qualified"
to the schematic element <xs:schema>
.
Only globally defined elements and attributes are automatically included in the target schema namespace. Elements defined in the definition <complexType>
are called local. The attribute elementFormDefault
determines whether local elements must be qualified or not. For attributes, the attribute attributeFormDefault
.
The default value for these attributes is unqualified
. Therefore, in your schema, the element <name>
is considered to have no namespace URI. Typically all elements should be in the target namespace, so using the attribute elementFormDefault="qualified"
is common practice. Attributes, on the other hand, usually do not need to be namespaced, so they attributeFormDefault
are often omitted.
More information in the W3C Recommendation http://www.w3.org/TR/xmlschema-0/#ref50
source to share