Explanation of XML and XSD schema/root element / namespace

465 Views Asked by At

I try to understand how xml and xsd files root element work. i read already a few articles via google, some articles on stackoverflow but still dont understand how the following works. here are some articles that i already read.

targetNamespace and xmlns
What does "xmlns" in XML mean?
https://softwareengineering.stackexchange.com/questions/122002/why-do-we-need-uris-for-xml-namespaces

for my questions i use the example from w3school. ( they are also not able to explain it in a way that i can understand how it works.) https://www.w3schools.com/xml/schema_intro.asp

XML File

File name: note.xml
URL: ???

<?xml version="1.0"?>

<note xmlns="https://www.w3schools.com"                   [XML 1]
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"     [XML 2]
xsi:schemaLocation="https://www.w3schools.com note.xsd">  [XML 3]

<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>

XSD File

File name: note.xsd
URL: https://www.w3schools.com note.xsd ??????

<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"  [XSD 1]
targetNamespace="https://www.w3schools.com"             [XSD 2]
xmlns="https://www.w3schools.com"                       [XSD 3]
elementFormDefault="qualified">

<xs:element name="note">
  <xs:complexType>
    <xs:sequence>
      <xs:element name="to" type="xs:string"/>
      <xs:element name="from" type="xs:string"/>
      <xs:element name="heading" type="xs:string"/>
      <xs:element name="body" type="xs:string"/>
    </xs:sequence>
  </xs:complexType>
</xs:element>

</xs:schema>

As far as i understand we have

a) a kind of "grammar" that explains how the xml has to be composed and that is written inside the xsd. for example that we have a complex type or elements with attributes. this grammar is specified by w3 on that URL: http://www.w3.org/2001/XMLSchema and we use the grammar from w3.

b)in addition to that grammar we define some names of elements, such as the to-element or the from-element. only elements with these names can be used inside an xml which is validated/defined by that note.xsd.

My Problem

What does each code at lines XML1-3 and XSD1-3 mean and how to use these parts. I will write down what i think i have understood so far. Please explain me in simple words (not too sophisticated as w3schools or other websites)what each the green and blue part does ,how to use it and where it points to. For this example what URL of note.xml and note.xsd do i have to assume ???

[URL note.xml]
[URL note.xsd]

[XML 1]xmlns="https://www.w3schools.com"
[XML 2]xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
[XML 3]xsi:schemaLocation="https://www.w3schools.com note.xsd"

[XSD 1]xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
[XSD 2]targetNamespace="https://www.w3schools.com"

[XSD 3]xmlns="https://www.w3schools.com"


[URL note.xml and note.xsd] both files URL is https://www.w3schools.com ??

[XML1-3]i am confused, what does it mean?

[XSD 1]xmlns means xml namespace and points to the URL from where i use the grammar. we also declare that only all "grammar" / elements marked with xs use the "grammar" which is written at http://www.w3.org/2001/XMLSchema

[XSD 2]targetNamespace for what purpose do i use it and where does it point to???
[XSD 3]xmlns without xs does the same as XSD1? just it would declare where elements without an xs take their "grammar" from? It is confusing, because wouldnt it be http://www.w3.org/2001/XMLSchema again?

0

There are 0 best solutions below