Allow xml:space="preserve" in XSD for validation

php, xml, xsd, xsd-validation

Solution

You have to change `Route` from being of type `xs:string` to being of type `string-with-xml-space`, where `string-with-xml-space` is a complex type with simple content, defined something like this:

<xs:complexType name="string-with-xml-space">
  <xs:complexContent>
    <xs:extension base="xs:string">
      <xs:attribute ref="xml:space"/>

You will also need an `xs:import` of the schema for the XML namespace:

<xs:import namespace='http://www.w3.org/XML/1998/namespace'  
           schemaLocation='http://www.w3.org/2001/xml.xsd'/>

Problem

I am trying to validate an xml file with a schema i wrote but its failing with the line: ``` Element 'Route', attribute '{http://www.w3.org/XML/1998/namespace}space': The attribute '{http://www.w3.org/XML/1998/namespace}space' is not allowed. ``` the XML file sometimes contains this: ``` <Route xml:space="preserve"> </Route> ``` which is obviously causing the problem, what can i do to my xsd file to allow for this? Here is my XSD with everything non relevant removed ``` <?xml version="1.0" standalone="yes"?> <xs:schema id="NewDataSet" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata"> <xs:element name="NewDataSet"> <xs:element name="Route" type="xs:string" minOccurs="0" /> <xs:element name="FurtherRequirements" type="xs:string" minOccurs="0" /> ``` etc. etc. All help gratefully received!

Original source