Page 1 of 1

XML Schema

Posted: Fri Sep 18, 2015 8:40 pm
by Troix
Hello there

I have the following .xml code and the corresponding .xsd:

XML file:

Code: Select all

<?xml version="1.0" encoding="UTF-8"?>
<lecturelist>
xmlns="http://www.my-fh.com"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.my-fh.com
file:lecturelist1.xsd">
<lecture>
<name>maths</name>
<semsester>1</semester>
</lecture>
</lecturelist>


XSD file:

Code: Select all

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.my-fh.com"
xmlns:fh="http://www.my-fh.com"
>

<xs:element name="lecturelist">
<xs:complexType>
<xs:sequence>
<xs:element ref="fh:lecture" maxOccurs="unbounded" />
</xs:sequence>
</xs:complexType>
</xs:element>

<xs:element name="lecture">
<xs:complexType>
<xs:sequence>
<xs:element ref:"fh:name" />
<xs:element ref:"fh:semester" />
</xs:sequence>
</xs:complexType>
</xs:element>

<xs:element name="title" type="xs:string"/>
<xs:element name="semester" type="xs:string"/>
Now I would like to extend the .xml file as following:

Extended XML file:

Code: Select all

<?xml version="1.0" encoding="UTF-8"?>
<lecturelist>
xmlns="http://www.my-fh.com"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.my-fh.com
file:lecturelist1.xsd">
<lecture>
<name>maths</name>
<semester>1</semester>
</lecture>
<teacher>
<name>steinberg</name>
<age>55</age>
</teacher>
</lecturelist>
Evidently I have to add some lines to the .xsd file too.
I tried this:

Code: Select all

]<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.my-fh.com"
xmlns:fh="http://www.my-fh.com"
xmlns:fb="http://www.my-fb.com"
>

<xs:element name="lecturelist">
<xs:complexType>
<xs:sequence>
<xs:element ref="fh:lecture" maxOccurs="unbounded" />
<xs:element ref="fb:teacher" maxOccurs="unbounded" />
</xs:sequence>
</xs:complexType>
</xs:element>

<xs:element name="lecture">
<xs:complexType>
<xs:sequence>
<xs:element ref:"fh:name" />
<xs:element ref:"fh:semester" />
</xs:sequence>
</xs:complexType>
</xs:element>

<xs:element name="teacher">
<xs:complexType>
<xs:sequence>
<xs:element ref:"fb:name" />
<xs:element ref:"fb:age" />
</xs:sequence>
</xs:complexType>
</xs:element>

<xs:element name="title" type="xs:string"/>
<xs:element name="semester" type="xs:string"/>
My Problem: How do I set up the new .xsd properly if I insist on remaining the structure as shown in the first .xsd?

Thank you for your help! :D

Kind regards,
Troix

Re: XML Schema

Posted: Mon Sep 21, 2015 11:15 am
by adrian
Hi,

Stick to the same namespace (the schema target namespace) when working with a single XSD.

Code: Select all

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.my-fh.com"
xmlns:fh="http://www.my-fh.com">

<xs:element name="lecturelist">
<xs:complexType>
<xs:sequence>
<xs:element ref="fh:lecture" maxOccurs="unbounded" />
<xs:element ref="fh:teacher" maxOccurs="unbounded" />
</xs:sequence>
</xs:complexType>
</xs:element>

<xs:element name="lecture">
<xs:complexType>
<xs:sequence>
<xs:element ref="fh:name" />
<xs:element ref="fh:semester" />
</xs:sequence>
</xs:complexType>
</xs:element>

<xs:element name="teacher">
<xs:complexType>
<xs:sequence>
<xs:element ref="fh:name" />
<xs:element ref="fh:age" />
</xs:sequence>
</xs:complexType>
</xs:element>

<xs:element name="title" type="xs:string"/>
<xs:element name="semester" type="xs:string"/>
<xs:element name="name" type="xs:string"/>
<xs:element name="age" type="xs:string"/>
</xs:schema>

Re: XML Schema

Posted: Mon Sep 21, 2015 11:33 am
by Troix
Hello adrian

Thank you for the reply.
So there is a "name" in "lecture" and "teacher".
If I were to say that the "name" in "lecture" is different to the "name" of "teacher" in terms of type how would I have to separate
them?

Thank you in advance, :D
Troix

Re: XML Schema

Posted: Mon Sep 21, 2015 3:58 pm
by adrian
If I were to say that the "name" in "lecture" is different to the "name" of "teacher" in terms of type how would I have to separate
them?
You can't declare two elements of the same name globally, so just declare the elements (or just one of them) locally (where used).

Code: Select all

    <xs:element name="teacher">
<xs:complexType>
<xs:sequence>
<xs:element name="name" type="xs:string"/>
<xs:element ref="fh:age" />
</xs:sequence>
</xs:complexType>
</xs:element>

Re: XML Schema

Posted: Mon Sep 21, 2015 6:15 pm
by Troix
Thank you! :P

Re: XML Schema

Posted: Tue Sep 22, 2015 9:43 am
by adrian
There's one other aspect that I forgot to mention. If you go with the local elements, you will also need to specify elementFormDefault="qualified" on the schema root, so that the local elements are placed in the target namespace. Otherwise, by default (unqualified), the local elements are in "no namespace".

Regards,
Adrian