1

Can I put my XML and XML Schema in one file? Or the schema has to be separate file? Im new to XML

I have noticed from tutorials that they have an XML file (.xml) and a separate XML Schema file (.xsd). Am I understanding this correctly?

user2881063
  • 1,049
  • 1
  • 10
  • 18

1 Answers1

4

Yes you can have xsd and xml in a same file such declaration is called as Inline Schema definition.

But as per general practice they are kept outside for many reasons, one of the very famous reason is:

  • Reusablility

Lets say you want to generate the sample xml/POJOs from the xsd for later.(Applies only for Java) (example JAXB)

Also as a best practice one should break the complex xsd into smaller xsd for easy to read and understand + flexibility of plugin the xsd required in a given case.

mhasan
  • 3,703
  • 1
  • 18
  • 37
  • 3
    Another good reason is that if A sends an XML file to B and B wants to validate it, B wants to validate it against a schema that B trusts, not against any old schema that happens to come with the document. – Michael Kay Sep 27 '16 at 11:18