1

I'm trying to validate XML according to an XSD. In this XML there are tags with namespaces and without.

<my:person xmlns:my="http://my.namespace.com">
  <my:name>John Doe</my:name>
  <my:year>1988</my:year>
  <namespaceLessElement>some value</namespaceLessElement>
</my:person>

My XSD looks like this. Validation fails because according to the XSD, namespaceLessElement has a namespace.

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<xs:schema xmlns:myNamespace="http://my.namespace.com"
           xmlns:xs="http://www.w3.org/2001/XMLSchema"
           targetNamespace="http://my.namespace.com"
           elementFormDefault="qualified">

    <xs:element name="person" type="myNamespace:person"/>

    <xs:complexType name="person">
        <xs:sequence>
            <xs:element name="name" type="xs:string"/>
            <xs:element name="year" type="xs:int"/>
            <xs:element name="namespaceLessElement" type="xs:string"/>
        </xs:sequence>
    </xs:complexType>

</xs:schema>

I can't find any info on how to turn off the namespace for namespaceLessElement. Is there a way to do that?

kjhughes
  • 106,133
  • 27
  • 181
  • 240
Mr Blowder
  • 138
  • 2
  • 9

1 Answers1

5

All of the components defined in your XSD are in the namespace defined by its targetNamespace. Use xsd:import to bring in an XSD from a namespace different than that namespace:

Main XSD

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<xs:schema xmlns:myNamespace="http://my.namespace.com"
           xmlns:xs="http://www.w3.org/2001/XMLSchema"
           targetNamespace="http://my.namespace.com"
           elementFormDefault="qualified">

  <xs:import schemaLocation="namespaceLessElement.xsd"/>

  <xs:element name="person" type="myNamespace:person"/>

  <xs:complexType name="person">
    <xs:sequence>
      <xs:element name="name" type="xs:string"/>
      <xs:element name="year" type="xs:int"/>
      <xs:element ref="namespaceLessElement"/>
    </xs:sequence>
  </xs:complexType>

</xs:schema>

Imported XSD

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
           elementFormDefault="qualified">

  <xs:element name="namespaceLessElement" type="xs:string"/>

</xs:schema>

Then your XML,

<?xml version="1.0" encoding="UTF-8"?>
<my:person xmlns:my="http://my.namespace.com"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://my.namespace.com try.xsd">
  <my:name>John Doe</my:name>
  <my:year>1988</my:year>
  <namespaceLessElement>some value</namespaceLessElement>
</my:person>

will validate as requested.

See also

kjhughes
  • 106,133
  • 27
  • 181
  • 240
  • How would be the XSD if only the root element has the namespace and not the child elements. John Doe 1988 – Bharat Darakh Jul 18 '20 at 20:12
  • 1
    @BharatDarakh: Your change of several siblings of `namespaceLessElement` to also be in no namespace would not change the answer's use of `xs:import`; the siblings' definitions would migrate to the imported XSD and be in no namespace as is already shown for `namespaceElement`. – kjhughes Jul 19 '20 at 04:35