XML Schema

This should cover W3C XML Schema, Relax NG and DTD related problems.
Troix
Posts: 3

XML Schema

Fri Sep 18, 2015 8:40 pm

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
adrian
Posts: 2465

Re: XML Schema

Mon Sep 21, 2015 11:15 am

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>
Adrian Buza
<oXygen/> XML Editor, Schema Editor and XSLT Editor/Debugger
http://www.oxygenxml.com
Troix
Posts: 3

Re: XML Schema

Mon Sep 21, 2015 11:33 am

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
adrian
Posts: 2465

Re: XML Schema

Mon Sep 21, 2015 3:58 pm

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>
Adrian Buza
<oXygen/> XML Editor, Schema Editor and XSLT Editor/Debugger
http://www.oxygenxml.com
Troix
Posts: 3

Re: XML Schema

Mon Sep 21, 2015 6:15 pm

Thank you! :P
adrian
Posts: 2465

Re: XML Schema

Tue Sep 22, 2015 9:43 am

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
Adrian Buza
<oXygen/> XML Editor, Schema Editor and XSLT Editor/Debugger
http://www.oxygenxml.com

Return to “XML Schemas”

Who is online

Users browsing this forum: No registered users and 0 guests