I have a use case to define XSD schema to describe a structure that is similar to Files and Directories.
File is a complex type with multiple attributes.
Directory is a File, so it should be an extension of a File or in other words, inherited from a File.
Directory can contain more Files and sub Directories, so it should be defined recursively.
File could be an element.
Directory could be an root element.
Can anyone provide an example schema file?
For example, the following C++ code compiles/runs fine:
#include <iostream>
#include <vector>
using namespace std;
class FileClass {
public:
string name;
int type;
string path;
long long size;
virtual int open(){
};
virtual int close (){
};
virtual ~FileClass(){
};
};
class DirectoryClass: public FileClass {
public:
vector<FileClass*> dir_contents;
};
int main() {
cout<<"started"<<endl;
DirectoryClass root;
root.open();
FileClass* newFile = new FileClass();
root.dir_contents.push_back(newFile);
DirectoryClass* subDir = new DirectoryClass();
root.dir_contents.push_back(subDir);
root.close();
cout<<"finished"<<endl;
}
As you can see, the DirectoryClass contains only one vector of FileClass, and it's capable of storing sub dirs since DirectoryClass is derived from FileClass.
I'd agree with Tony, why derive Directory from File?
But if thats what your trying to model...
<?xml version="1.0" encoding="utf-8" ?>
<!--Created with Liquid XML Studio 2013 Designer Edition 11.0.0.0 (http://www.liquid-technologies.com)-->
<xs:schema elementFormDefault="qualified"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:complexType name="FileType">
<xs:attribute name="name"
type="xs:string"
use="required" />
<xs:attribute name="creationDate"
type="xs:dateTime"
use="required" />
</xs:complexType>
<xs:element name="File"
type="FileType" />
<xs:element name="Directory">
<xs:complexType>
<xs:complexContent>
<xs:extension base="FileType">
<xs:sequence>
<xs:element ref="File"
minOccurs="0"
maxOccurs="unbounded" />
<xs:element ref="Directory"
minOccurs="0"
maxOccurs="unbounded" />
</xs:sequence>
</xs:extension>
</xs:complexContent>
</xs:complexType>
</xs:element>
</xs:schema>
Then if you are then trying to read/write the structured XML data from C#, C++, Java etc you could look at XML Data Binding. This generates a set of strongly typed objects from your XSD.
That changes things a bit and compilcates the XML. There are 2 approaches ComplexType Inheritance or Substitution Groups. Complex Types are a better fit to the breif.
<?xml version="1.0" encoding="utf-8" ?>
<!--Created with Liquid XML Studio 2013 Designer Edition (Trial) 11.0.0.0 (http://www.liquid-technologies.com)-->
<xs:schema elementFormDefault="qualified"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:complexType name="FileType">
<xs:attribute name="name"
type="xs:string"
use="required" />
<xs:attribute name="creationDate"
type="xs:dateTime"
use="required" />
<xs:attribute name="type"
type="xs:int" />
<xs:attribute name="size"
type="xs:long" />
</xs:complexType>
<xs:complexType name="DirectoryType">
<xs:complexContent>
<xs:extension base="FileType">
<xs:sequence>
<xs:element name="Content"
type="FileType"
minOccurs="0"
maxOccurs="unbounded" />
</xs:sequence>
</xs:extension>
</xs:complexContent>
</xs:complexType>
<xs:element name="Directory"
type="DirectoryType" />
</xs:schema>
Sample XML
<?xml version="1.0" encoding="utf-8"?>
<!-- Created with Liquid XML Studio 2013 Designer Edition (Trial) 11.0.0.0 (http://www.liquid-technologies.com) -->
<Directory xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="FoldersCT.xsd"
name="string"
creationDate="1975-12-12T08:25:00.94"
type="393"
size="-5191">
<Content name="FileA1"
creationDate="1993-02-23T21:13:52.11"
type="4931"
size="-1984" />
<Content name="FileA2"
creationDate="1991-12-26T15:57:44.30"
type="-6155"
size="18" />
<Content name="FileA3"
creationDate="1979-01-05T21:36:13.80"
type="8362"
size="5579" />
<Content xsi:type="DirectoryType"
name="DirA4"
creationDate="1979-01-05T21:36:13.80"
type="8362"
size="5579">
<Content name="FileB1"
creationDate="1979-01-05T21:36:13.80"
type="8362"
size="5579" />
<Content name="FileB2"
creationDate="1979-01-05T21:36:13.80"
type="8362"
size="5579" />
</Content>
</Directory>
Note the use of the xsi:type="DirectoryType" attribute in the directories. This tells the XML Parser that the content is defined by DirectoryType. Without this its a file. This is what your spec says, but its pretty messy I would say.
Another solution is to use substitution groups, which personally I don't like as they are difficult to read in a schema. This effectivley this is the same as the orginal solution.
<?xml version="1.0" encoding="utf-8" ?>
<!--Created with Liquid XML Studio 2013 Designer Edition (Trial) 11.0.0.0 (http://www.liquid-technologies.com)-->
<xs:schema elementFormDefault="qualified"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:complexType name="FileType">
<xs:attribute name="name"
type="xs:string"
use="required" />
<xs:attribute name="creationDate"
type="xs:dateTime"
use="required" />
<xs:attribute name="type"
type="xs:int" />
<xs:attribute name="size"
type="xs:long" />
</xs:complexType>
<xs:element name="File"
type="FileType"
substitutionGroup="File" />
<xs:element name="Directory"
substitutionGroup="File">
<xs:complexType>
<xs:complexContent>
<xs:extension base="FileType">
<xs:sequence>
<xs:element ref="File"
minOccurs="0"
maxOccurs="unbounded" />
</xs:sequence>
</xs:extension>
</xs:complexContent>
</xs:complexType>
</xs:element>
</xs:schema>
Sample XML
<?xml version="1.0" encoding="utf-8"?>
<!-- Created with Liquid XML Studio 2013 Designer Edition (Trial) 11.0.0.0 (http://www.liquid-technologies.com) -->
<Directory xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="FoldersSG.xsd"
name="string"
creationDate="1992-01-13T17:58:08.38"
type="-2296"
size="-8752">
<File name="string"
creationDate="1982-11-03T17:57:40.96"
type="6813"
size="5278" />
<File name="string"
creationDate="1986-12-08T12:38:03.46"
type="4479"
size="8453" />
<Directory name="string"
creationDate="1992-01-13T17:58:08.38"
type="-2296"
size="8752">
<File name="string"
creationDate="1982-11-03T17:57:40.96"
type="6813"
size="5278" />
<File name="string"
creationDate="1986-12-08T12:38:03.46"
type="4479"
size="8453" />
</Directory>
</Directory>
Related
I wish to create a repeating same named element with two attributes for which the values of each are tied to each other. i.e.
<anElement id="R1" description="Some definition for R1"/>
<anElement id="R2" description="Some definition for R2"/>
Ideally I would define "groups" of id and description attributes together and reference one of the groups for each instance of anElement element.
<complexType name="RElements">
<choice>
<element name="anElement" type="R1Group"/>
<element name="anElement" type="R2Group"/>
</choice>
</complexType>
<complexType name="RElementsType">
<sequence>
<element ref="RElements" minOccurs="1" maxOccurs="unbounded"/>
</sequence>
</complexType>
But Choice doesn't allow multiples of the same element name. I see discussions about how to have multiple elements of the same name but none allow additional attributes let alone having attributes tied to each other. The only way that I can get close is by having a different named element based on the id. i.e.
<R1 id="R1" description="Some definition for R1"/>
<R2 id="R2" description="Some definition for R2"/>
Any suggestions?
You need to provide more information.
Basically to achieve the XML you have provided a simple schema like this would suffice.
<?xml version="1.0" encoding="utf-8" ?>
<!--Created with Liquid XML 2015 Developer Bundle Edition 13.0.3.5737 (http://www.liquid-technologies.com)-->
<xs:schema elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="RootElement">
<xs:complexType>
<xs:sequence>
<xs:element name="anElement" minOccurs="0" maxOccurs="unbounded">
<xs:complexType>
<xs:attribute name="id" type="xs:string" />
<xs:attribute name="descritpion" type="xs:string" />
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
However you mention that the attribute values id and description are linked, you may be able to constrain these, but you need to provide more info. Also I'm not sure why you are looking at choices? Its difficult to see what your attempting give you only provide snippets of the schema
To include the constraints described in later comments the following schema would allow this, but force a change in the XML structure
<AnElement id="E1"> Exterminator serial <ref id="E1"/> </AnElement>
<?xml version="1.0" encoding="utf-8" ?>
<!--Created with Liquid XML 2015 Developer Bundle Edition 13.0.0.5686 (http://www.liquid-technologies.com)-->
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="RootElement">
<xs:complexType>
<xs:sequence minOccurs="0" maxOccurs="unbounded">
<xs:element name="AnElement">
<xs:complexType mixed="true">
<xs:sequence>
<xs:element name="ref" minOccurs="0">
<xs:complexType>
<xs:attribute name="id" type="xs:string" use="optional" />
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="id" type="xs:string" use="optional" />
</xs:complexType>
<xs:key name="IDVal">
<xs:selector xpath="." />
<xs:field xpath="#id" />
</xs:key>
<xs:keyref name="IdRef" refer="IDVal">
<xs:selector xpath="ref" />
<xs:field xpath="#id" />
</xs:keyref>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
I am trying to understand <any> element in xsd. I had two xsds.
Book Catalogue.xsd
<?xml version="1.0" encoding="ISO-8859-1"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.w3schools.com" xmlns="http://www.w3schools.com"
elementFormDefault="qualified">
<xs:element name="BookCatalogue">
<xs:complexType>
<xs:sequence>
<xs:element name="Book" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="Title" type="xs:string" />
<xs:element name="Author" type="xs:string" />
<xs:element name="Date" type="xs:string" />
<xs:element name="ISBN" type="xs:string" />
<xs:element name="Publisher" type="xs:string" />
<xs:any namespace="##any" minOccurs="0" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
Reviewer.xsd
<?xml version="1.0" encoding="ISO-8859-1"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.w3schools.com" xmlns="http://www.w3schools.com"
elementFormDefault="qualified">
<xs:element name="Reviewer">
<xs:complexType>
<xs:sequence>
<xs:element name="Name">
<xs:complexType>
<xs:sequence>
<xs:element name="First" type="xs:string" />
<xs:element name="Last" type="xs:string" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
But if i validate the below xml based on above xsd, i am getting cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be found for element 'p:Reviewer'. error. Does both xsd file should not be in same namespace?
<?xml version="1.0" encoding="UTF-8"?>
<pr:BookCatalogue xmlns:pr="http://www.w3schools.com"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.w3schools.com AddRequest.xsd ">
<pr:Book>
<pr:Title>pr:Title</pr:Title>
<pr:Author>pr:Author</pr:Author>
<pr:Date>pr:Date</pr:Date>
<pr:ISBN>pr:ISBN</pr:ISBN>
<pr:Publisher>pr:Publisher</pr:Publisher>
<p:Reviewer xmlns:p="http://www.w3schools.com"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.w3schools.com Children.xsd ">
<p:Name>
<p:First>p:First</p:First>
<p:Last>p:Last</p:Last>
</p:Name>
</p:Reviewer>
</pr:Book>
</pr:BookCatalogue>
Two options...
Option One: If you do not want to have to have the definition of p:Reviewer present, add processContents="lax" to your xs:any element:
<xs:any namespace="##any" minOccurs="0" processContents="lax"/>
Per XML Schema Part 0: Primer Second Edition:
The lax value of the processContents attribute instructs an XML
processor to validate the element content on a can-do basis: It will
validate elements and attributes for which it can obtain schema
information, but it will not signal errors for those it cannot obtain
any schema information.
See also XML Validation in Java: processContents=“lax” seems not to work correctly.
You should also carefully adjust your xsi:schemaLocation values to point to the actual filename of each XSD for each namespace in play. Here is your XML instance with the changes that I made:
<?xml version="1.0" encoding="UTF-8"?>
<pr:BookCatalogue
xmlns:pr="http://www.w3schools.com"
xmlns:p="http://www.w3schools.com/1"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.w3schools.com BookCatalogue.xsd http://www.w3schools.com/1 Reviewer.xsd">
<pr:Book>
<pr:Title>pr:Title</pr:Title>
<pr:Author>pr:Author</pr:Author>
<pr:Date>pr:Date</pr:Date>
<pr:ISBN>pr:ISBN</pr:ISBN>
<pr:Publisher>pr:Publisher</pr:Publisher>
<p:Reviewer>
<p:Name>
<p:First>p:First</p:First>
<p:Last>p:Last</p:Last>
</p:Name>
</p:Reviewer>
</pr:Book>
</pr:BookCatalogue>
Note: Make sure that the targetNamespace in Review.xsd matches what's declared for it in BookCatalogue.xml's xsi:schemaLocation attribute.
Option Two: If you do want to insist that the definition of p:Reviewer be present, just make the above changes to be sure that Review.xsd can be found per the xsi:schemaLocation mechanism. No processContents setting is required; it defaults to strict.
I'd like to have an XSD to valide an XML containing file elements with many aliases but each alias with a different value.
This is my XML:
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<folder>
<files>
<fileList>
<file>
<title>Document1</title>
<fileAlias>
<alias>Alias1</alias>
<alias>Alias2</alias>
</fileAlias>
</file>
</fileList>
</files>
</folder>
The former XML is good because Alias1 != Alias2
How could I specify it in my XSD:
<?xml version="1.0" encoding="utf-8"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" version="1.0"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="folder" type="folderType" />
<xs:complexType name="folderType">
<xs:sequence>
<xs:element name="files" type="filesType" />
</xs:sequence>
</xs:complexType>
<xs:complexType name="filesType">
<xs:sequence>
<xs:element name="fileList" type="fileListType" />
</xs:sequence>
</xs:complexType>
<xs:complexType name="fileListType">
<xs:sequence>
<xs:element maxOccurs="unbounded" name="file" type="fileType" />
</xs:sequence>
</xs:complexType>
<xs:complexType name="fileType">
<xs:sequence>
<xs:element name="title" type="xs:string" />
<xs:element name="fileAlias" type="fileAliasType" minOccurs="0" maxOccurs="unbounded" />
</xs:sequence>
<xs:attribute name="id" type="xs:string" />
<xs:attribute name="type" type="xs:int" />
</xs:complexType>
<xs:complexType name="fileAliasType">
<xs:sequence>
<xs:element maxOccurs="unbounded" name="alias" type="xs:string" minOccurs="1" />
</xs:sequence>
</xs:complexType>
</xs:schema>
I've tried with xsd:unique but I think is for attributes not for XML element text values.
If I understand well your need then xs:unique should be the right way. Following modification of fileType complex type should be enough.
<xs:complexType name="fileType">
<xs:sequence>
<xs:element name="title" type="xs:string"/>
<xs:element name="fileAlias" type="fileAliasType" minOccurs="0" maxOccurs="unbounded">
<xs:unique name="alias_unique">
<xs:selector xpath="alias"/>
<xs:field xpath="."/>
</xs:unique>
</xs:element>
</xs:sequence>
<xs:attribute name="id" type="xs:string"/>
<xs:attribute name="type" type="xs:int"/>
</xs:complexType>
This will validate
<?xml version="1.0" encoding="UTF-8"?>
<!--Sample XML file generated by XMLSpy v2013 sp1 (http://www.altova.com)-->
<folder xsi:noNamespaceSchemaLocation="unique.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<files>
<fileList>
<file id="String" type="0">
<title>String</title>
<fileAlias>
<alias>String1</alias>
<alias>String2</alias>
<alias>String3</alias>
</fileAlias>
</file>
</fileList>
</files>
</folder>
This won't
<?xml version="1.0" encoding="UTF-8"?>
<!--Sample XML file generated by XMLSpy v2013 sp1 (http://www.altova.com)-->
<folder xsi:noNamespaceSchemaLocation="unique.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<files>
<fileList>
<file id="String" type="0">
<title>String</title>
<fileAlias>
<alias>String1</alias>
<alias>String1</alias>
<alias>String3</alias>
</fileAlias>
</file>
</fileList>
</files>
</folder>
Here is a schema file, midi.xsd that defines a type, note, used to store MIDI note values:
<?xml version="1.0" encoding="utf-8"?>
<xs:schema elementFormDefault="qualified"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:simpleType name="note">
<xs:restriction base="xs:integer">
<xs:minInclusive value="0"/>
<xs:maxInclusive value="127"/>
</xs:restriction>
</xs:simpleType>
</xs:schema>
Here is another schema file, octaves.xsd which uses midi.xsd to help define the layout to be enforced on an XML file containing data about octaves:
<?xml version="1.0" encoding="utf-8"?>
<xs:schema elementFormDefault="qualified"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:include schemaLocation="midi.xsd"/>
<xs:element name="octaves">
<xs:complexType>
<xs:sequence>
<xs:element name="octave">
<xs:complexType>
<xs:sequence>
<xs:element name="name" type="xs:string" />
<xs:element name="midi">
<xs:complexType>
<xs:sequence>
<xs:element name="value" type="xs:integer" />
<xs:element name="from" type="note" />
<xs:element name="to" type="note" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="index" type="xs:integer" />
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
This is fine, and works exactly as you would expect it to, except that I have another requirement: I want note to be in its own namespace, midi, so that
<xs:element name="from" type="note" />
becomes
<xs:element name="from" type="midi:note" />
Try as I might, I cannot get this to work. My attempts have included use of the targetNamespace attribute in various places, the import element, and liberal use of xmlns:midi="...", but to no avail. I'd post one of these attempts here, were it not so cringe-worthy.
Could some kind soul point me in the right direction? I'm pretty sure the problem is to do with the fact that midi.xsd is a local file; it has never been, and never will be, hosted on a web server.
Change midi.xsd to be:
<xs:schema elementFormDefault="qualified"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetnamespace="/my/midi/namespace">
And then change octaves.xsd to say:
<xs:schema elementFormDefault="qualified"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:midi="/my/midi/namespace">
<xs:import namespace="/my/midi/namespace" schemaLocation="midi.xsd"/>
...
<xs:element name="from" type="midi:note" />
Note the use of xs:import rather than <xs:include> The two are very different - you use import for bringing in other namespaces, and include for inline inclusion of other schema files into the current namespace.
Note also that /my/midi/namespace can be anything you want, it's an arbitrary identifier.
I'm pretty sure the problem is to do with the fact that midi.xsd is a local file
Nope, not relevant.
I have a <font> tag having attributes name required , link='notusing' required and replaced_with (it should be optional only link value is not a string of 'notusing').
Also XSD has to check whether the xml has font with a name that is specified in replaced_with.
Example:
<fonts>
<font name='font1' link='inuse'/>
<font name='font2' link='inuse'/>
<font name='font3' link='notusing' replaced_with="font2"/>
</fonts>
How can i write the XSD for this? Thanks
I don't think it's possible to enforce that replaced_with can only occur if link='notusing'. You can make replaced_with optional using minOccurs='0', but that's about it.
If you are able to change the structure of the XML file, you could do something like this instead:
<?xml version="1.0" encoding="utf-8" ?>
<fontData
xmlns="someNamespace"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="someNamespace XMLSchema1.xsd">
<fonts>
<font name="font1" />
<font name="font2" />
</fonts>
<obsoleteFonts>
<font name="font3" replaced_with="font2" />
</obsoleteFonts>
</fontData>
And then you could use key and keyref to enforce that the name of any font within obsoleteFonts exists in fonts.
Here's how the XSD file would look to enforce this XML format:
<?xml version="1.0" encoding="utf-8"?>
<xs:schema
xmlns="someNamespace"
xmlns:tns="someNamespace"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="someNamespace"
elementFormDefault="qualified">
<xs:element name="fontData" type="fontData">
<xs:key name="fontKey">
<xs:selector xpath="tns:fonts/tns:font" />
<xs:field xpath="#name" />
</xs:key>
<xs:keyref name="obsoleteFontToFontKeyRef" refer="fontKey">
<xs:selector xpath="tns:obsoleteFonts/tns:font" />
<xs:field xpath="#replaced_with" />
</xs:keyref>
</xs:element>
<xs:complexType name="fontData">
<xs:sequence>
<xs:element name="fonts" type="fonts" />
<xs:element name="obsoleteFonts" type="obsoleteFonts" />
</xs:sequence>
</xs:complexType>
<xs:complexType name="fonts">
<xs:sequence>
<xs:sequence>
<xs:element name="font" type="font" maxOccurs="unbounded" />
</xs:sequence>
</xs:sequence>
</xs:complexType>
<xs:complexType name="obsoleteFonts">
<xs:sequence>
<xs:sequence>
<xs:element name="font" type="obsoleteFont" maxOccurs="unbounded" />
</xs:sequence>
</xs:sequence>
</xs:complexType>
<xs:complexType name="font">
<xs:attribute name="name" type="xs:string" />
</xs:complexType>
<xs:complexType name="obsoleteFont">
<xs:attribute name="name" type="xs:string" />
<xs:attribute name="replaced_with" type="xs:string" />
</xs:complexType>
</xs:schema>
I tested this with the Visual Studio schema validator, but hopefully it will work properly in whatever technology you are using.
In XML schema (XSD), you cannot express this kind of requirement / restriction. XML schema is all about structure, and you cannot use values of elements or attributes to influence the structure of other elements or attributes. Can't be done, it's just not part of the XML schema standard.
If you need this kind of checks, have a look at Schematron, a different kind of XML validation framework.