I have a potential of having quite a lot of small XSD's containing individual types of a big project. (Components)
I also have a biggish number of XSD's requiring a large number of these individual XSD's. (Screens)
I know I could import each one of the "Components" into each "Screen" XSD. But its a lot of work to do on each one.
What I was hoping I could do is import each one of these "Components" into a single XSD (ComponentLibrary) and then just import this one "ComponentLibrary" into each "Screen" XSD.
I wrote the code that I thought may have been what was needed but it doesn't seem to want to work. I get errors that it is undeclared.
TF.xsd:
<xs:schema targetNamespace="http://namespace.com/TF"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified">
<xs:complexType name="TFType">
<xs:attribute name="size" type="xs:decimal" />
<xs:attribute name="colour" type="xs:decimal" />
</xs:complexType>
</xs:schema>
ComponentsLibrary.xsd:
<xs:schema targetNamespace="http://namespace.com/ComponentsLibrary"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:tf="http://namespace.com/TF"
elementFormDefault="qualified">
<xs:import schemaLocation="TF.xsd" namespace="http://namespace.com/TF"/>
<xs:element name="TF" type="tf:TFType" />
</xs:schema>
Screen1.xsd:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:cl="http://namespace.com/ComponentsLibrary"
elementFormDefault="qualified">
<xs:import schemaLocation="ComponentsLibrary.xsd" namespace="http://namespace.com/ComponentsLibrary" />
<xs:element name="tfTitle" type="cl:TF" />
</xs:schema>
Error I get
Screen1.xsd (8:3) Error Type 'http://namespace.com/Components:TF' is not declared.
Your components library schema declares an element TF, not a type TF, hence the error in the Screen1.xsd.
If you really want to make the TF type available in ComponentLibrary then you could use a chameleon schema approach.
TF.xsd should have no namespace
ComponentLibrary should then include (not import) TF.xsd
All types in TF.xsd are now available under the ComponentLibrary namespace
Related
So I found this post which is very similar to what I want to achieve, but I can't quite figure out if it's not possible to do what I am attempting, or if I'm just missing something...
Use XML Schema to extend an element rather than a complexType
The gist is that I have an XSD that contains a defined Element. I would prefer not to edit this original xsd file. Is there any way for me to extend the Element so that I also can add my own attributes, "attributeC"?
The other post creates a new complexType of fooType, and places the element inside of it, and extends fooType to contain more elements. By doing so, they are able to achieve an Element that contains fooElement, and two other added elements. The issue is that I'd like to add to the Element itself, not add to the element at the same level.
XSD1
<xs:schema xmlns:bas="http://www.base.com"
xmlns="https://www.adding.com"
attributeFormDefault="qualified"
elementFormDefault="qualified"
targetNamespace="https://www.adding.com"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:import schemaLocation="XSD2.xsd" namespace="http://www.base.com" />
<xs:complexType name="FileSpecType">
<xs:sequence>
<xs:element ref="bas:FileSpec"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="FullFileSpecType">
<xs:complexContent mixed="false">
<xs:extension base="FileSpecType">
<xs:attribute name="attributeC" type="xs:string" />
</xs:extension>
</xs:complexContent>
</xs:complexType>
<xs:element name="FileSpec" type="FileSpecType" />
This code ends up generating a new FileSpec
<FileSpec AttributeC="attCval" />>
<bas:FileSpec/>
</FileSpec>
What I want to achieve is more like...
<bas:FileSpec AttributeA="attAval" AttributeB="attBval" AttributeC="attCval/>
Can anybody point me in the right direction to solve my issues?
I'm thinking I could define my own dataType=FileSpec and add my own reference to external attributes, but that'd require manually copying over each attribute that exists in my original XSD2 FileSpec so I'd prefer to avoid that if possible. Then I think I could take that new FileSpec that I created and redefine the older FileSpec with it. Is this possible to do? It sounds like a lot of work that must have a simpler solution.
Its somewhat unclear what you are trying to do, if this does not cover it, please post the schema you are including as well as the XML you are trying to produce including the containing element.
You can extend an existing xs:complexType like this.
<?xml version="1.0" encoding="utf-8" ?>
<!--Created with Liquid Studio 2020 (https://www.liquid-technologies.com)-->
<xs:schema elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:complexType name="BaseType">
<xs:attribute name="attributeA" type="xs:string" />
</xs:complexType>
<xs:complexType name="ExtendedType">
<xs:complexContent>
<xs:extension base="BaseType">
<xs:attribute name="attributeB" />
</xs:extension>
</xs:complexContent>
</xs:complexType>
<xs:element name="RootElm">
<xs:complexType>
<xs:sequence>
<xs:element name="BaseElm" type="BaseType" />
<xs:element name="ExtendedElm" type="ExtendedType" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
This allows you to produce XML that looks like this
<?xml version="1.0" encoding="utf-8"?>
<!-- Created with Liquid Studio 2020 (https://www.liquid-technologies.com) -->
<RootElm xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<BaseElm attributeA="XX" />
<ExtendedElm attributeA="YY" attributeB="ZZ" />
</RootElm>
There is another way to make use of the ExtendedType when ever its valid to use a BaseType
<?xml version="1.0" encoding="utf-8"?>
<!-- Created with Liquid Studio 2020 (https://www.liquid-technologies.com) -->
<RootElm xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<BaseElm xsi:type="ExtendedType" attributeB="XX" attributeA="YY" />
<ExtendedElm attributeA="XX" attributeB="YY" />
</RootElm>
Notice the xsi:type="ExtendedType" in the BaseElm, this tells the XML processor that this element actually contains data for the ExtendedType.
Given a top level xsd that imports a second level which in turn imports a third, is it possible to use a type from the third in the first? Or does the first have to import the third directly?
Good question!
From reading the spec, I can't really tell. It doesn't explicitly address the issue of transitive imports explicitly.
In the Primer (part 0), they only talk about one level of importing: http://www.w3.org/TR/xmlschema-0/#import
In Structure (part 1), it also only defines direct importing http://www.w3.org/TR/2004/REC-xmlschema-1-20041028/structures.html#composition-schemaImport Since it talks about "a means of addressing such foreign component" (which I think means namespace), maybe it's reasonable to assume that an explicit way of addressing imported schemas is necessary - in other words, an explicit namespace in an import is needed for each one.
A google search shows that other people have also been confused by this issue:
http://xsd.stylusstudio.com/2002Apr/post00021.htm
http://xsd.stylusstudio.com/2005Mar/post05007.htm
What's of most concern about those posts is that different xsd processors have different behaviour - suggesting that the writers of the processors were also confused.
Although that may have changed since those posts (2002, 2005), the wisest course seems to be to avoid the issue, and just use direct imports, because this will work with all processors.
As I said: good question.
Here is a test, to check an xsd processor (of course, this won't guarantee that it will work for someone else using some other xsd processor...). I found that my favourite one (xmllint) does not allow transitive imports.
The test is three schemas: a.xsd imports b.xsd which in turn imports c.xsd; and a type defined in c.xsd is referenced from a.xsd:
<!-- a.xsd -->
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:c="http://www.c.com" targetNamespace="http://www.a.com">
<xsd:import namespace="http://www.b.com" schemaLocation="b.xsd"/>
<!-- UNCOMMENT FOR DIRECT IMPORT
<xsd:import namespace="http://www.c.com" schemaLocation="c.xsd"/>
-->
<xsd:element name="eg" type="c:TypeC"/>
</xsd:schema>
<!-- b.xsd -->
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:import namespace="http://www.c.com" schemaLocation="c.xsd"/>
</xsd:schema>
<!-- c.xsd -->
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.c.com">
<xsd:simpleType name="TypeC">
<xsd:restriction base="xsd:string"/>
</xsd:simpleType>
</xsd:schema>
<!-- a.xml -->
<eg xmlns="http://www.a.com">hello world</eg>
$ xmllint --schema a.xsd a.xml --noout
a.xsd:6: element element: Schemas parser error : Element
'{http://www.w3.org/2001/XMLSchema}element', attribute 'type': References from
this schema to components in the namespace 'http://www.c.com' are not allowed,
since not indicated by an import statement.
WXS schema a.xsd failed to compile
The error message is: References from this schema to components in the namespace 'http://www.c.com' are not allowed, since not indicated by an import statement., suggesting that the developers of xmllint at least are quite sure that imports are not transitive.
If type is what you are talking about then .. It's <xs:Include> not <xs:Import> ..
And the answer is : Parser takes care of linking all XSDs together
see the example below:
<?xml version="1.0" encoding="utf-8"?>
<root>
<child>trial</child>
<child2>trial2</child2>
<trunk>
<branch>1</branch>
<branch>2</branch>
</trunk>
<specialchild>test</specialchild>
</root>
For the above XML I will design an XSD:
<?xml version="1.0" encoding="utf-8"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:include schemaLocation="include multiple XSDs2.xsd"/>
<xs:element name="root" type ="root"/>
<xs:complexType name="root">
<xs:sequence>
<xs:element name="child" type="xs:string" />
<xs:element name="child2" type="xs:string" />
<xs:element name="trunk" type="trunk"/>
<xs:element name="specialchild" type="specialchild"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
where type trunk is defined in import multiple XSDs2.xsd file and linked by using <xs:include> .. (which is residing in the same folder).. And the code looks like this:
<?xml version="1.0" encoding="utf-8"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:include schemaLocation="include multiple XSDs3.xsd"/>
<xs:complexType name="trunk">
<xs:sequence>
<xs:element maxOccurs="unbounded" name="branch" type="branch" />
</xs:sequence>
</xs:complexType>
</xs:schema>
and type branch is a simple type defined in include multiple XSDs3.xsd file, and the code looks like this:
<?xml version="1.0" encoding="utf-8"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:simpleType name="branch">
<xs:restriction base="xs:string"/>
</xs:simpleType>
<xs:simpleType name="specialchild">
<xs:restriction base="xs:string"/>
</xs:simpleType>
</xs:schema>
*Now the tricky part is: specialchild is declared in XSD_1 where as defined in XSD_3 and these two XSDs are linked by XSD_2..
You can observe that parser by default takes care of linking all XSDs and treating them all as one! *
Hope this solves your question!
I have been developing some xml schema files over the past few days, and learned of a specific element to extend simpletypes and complextype elements.
I am currently using the visual studio 2012 professional edition, and I am currently testing relationships of XSD files (I daresay parent-child relationships , or one to many relationships) between these files, for example (I am using objects from Google DFA API):
RichMediaAsset
∟ RichMediaExpandingHtmlAsset
∟ RichMediaExpandingHtmlAsset
∟ RichMediaFloatingHtmlAsset
...
All these classes "extend" or "Inherit" from RichMediaAsset (which is the base, or abstract). I have defined RichMediaAsset as the following in XSD
<?xml version="1.0" standalone="yes"?>
<xs:schema id="RedirectCreativeBase" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<!-- simpleTypes=primitive -->
<!-- extBooleanMethodPrefix=is -->
<xs:complexType name="RichMediaAssetWrapper" abstract="true">
<xs:sequence>
<xs:element name="fileName" type="xs:string" minOccurs="0" />
<xs:element name="fileSize" type="xs:int" minOccurs="0" />
<xs:element name="id" type="xs:long" minOccurs="0" />
<xs:element name="parentAssetId" type="xs:long" minOccurs="0" />
<xs:element name="type" type="xs:string" minOccurs="0" />
</xs:sequence>
</xs:complexType>
</xs:schema>
I have defined the second file, RichMediaExpandingHtmlAsset as follows:
<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="RichMediaExpandingHtmlAssetWrapper" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:include schemaLocation="./RichMediaAsset.xsd"/>
<xs:complexType name="RichMediaExpandingHtmlAssetWrapper" abstract="false" >
<xs:extension base="RichMediaAssetWrapper"> <!-- Not happy here -->
<xs:sequence>
<!-- content to be included,extending RichMediaAsset's complex type called RichMediaAssetWrapper -->
</xs:sequence>
</xs:extension>
</xs:complexType>
</xs:schema>
The part which I mentioned VS2012 is not happy with is defined as follows:
Warning 1 The 'http://www.w3.org/2001/XMLSchema:extension' element is not supported in this context. C:\eclipse\Workspace\aem_adservices_google_dfa\aem.adservices.google.dfa\xsd\Creative\RichMediaExpandingHtmlAsset.xsd 5 6 Miscellaneous Files
Warning 2 The element 'complexType' in namespace 'http://www.w3.org/2001/XMLSchema' has invalid child element 'extension' in namespace 'http://www.w3.org/2001/XMLSchema'. List of possible elements expected: 'annotation, simpleContent, complexContent, group, all, choice, sequence, attribute, attributeGroup, anyAttribute' in namespace 'http://www.w3.org/2001/XMLSchema'. C:\eclipse\Workspace\aem_adservices_google_dfa\aem.adservices.google.dfa\xsd\Creative\RichMediaExpandingHtmlAsset.xsd 5 6 Miscellaneous Files
The question now: Is this a possible bug of 2012, have I made an error, is this simply not supported (even though I checked the usage examples at w3schools.com), or is there better ways for me to define the one to many relationships?
I found the issue with my XSD. It was in fact missing a tag. In these cases, xs:complexContent or xs:simpleContent must be defined in order to define extensions or restrictions on a complex/simple type respectively that contains mixed content or elements only.
Here is my solution, and the errors in my code also disappeared.
<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="RichMediaExpandingHtmlAssetWrapper" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:include schemaLocation="./RichMediaAsset.xsd"/>
<xs:complexType name="RichMediaExpandingHtmlAssetWrapper" abstract="false" >
<xs:complexContent>
<xs:extension base="RichMediaAssetWrapper">
<xs:sequence>
...
</xs:sequence>
</xs:extension>
</xs:complexContent>
</xs:complexType>
</xs:schema>
Sources: ComplexContent; SimpleContent
I'm writing down some XSD file for webservice communication between an application and sharepoint.. I'm trying to make my parameters "REQUIRED" but even if i put minOccurs to 1, they could be not specified..
How can i resolve this problem? Here's one of mine XSD:
<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="RemoveGroup"
targetNamespace="http://tempuri.org/RemoveGroup.xsd"
elementFormDefault="qualified"
xmlns="http://tempuri.org/RemoveGroup.xsd"
xmlns:mstns="http://tempuri.org/RemoveGroup.xsd"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
>
<xs:element name="RemoveGroup">
<xs:complexType>
<xs:sequence>
<xs:element name="tt_group_id" type="xs:long" />
<xs:element name="tt_network_id" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
I hope there's a way not to write down houndred of "if (input.Parameter != null)" ...
Using minOccurs="1" at either the <element/> or <sequence/> level is the correct thing to do. What specific error are you getting?
UPDATE
Actually within a <sequence/> parsers should expect exactly one instance of an element
UPDATE
Your parser may be emitting errors as events which you need to handle in order to capture the errors - many common parsers have this behaviour.
Something which could cause an error is a null value in the long simple type - this type does not allow blanks. If you want to indicate that nulls are allowed you should use nil=true from namespace http://www.w3.org/2001/XMLSchema-instance.
I have the following in a schema:
<xs:element name="td">
<xs:complexType>
<xs:complexContent>
<xs:extension base="cell.type"/>
</xs:complexContent>
</xs:complexType>
</xs:element>
<xs:complexType name="cell.type" mixed="true">
<xs:sequence minOccurs="0" maxOccurs="unbounded">
<xs:element ref="p"/>
</xs:sequence>
</xs:complexType>
Some parsers allow PCDATA directly in the element, while others don't. There's something in the XSD recommendation (3.4.2) that says when a complex type has complex content, and neither has a mixed attribute, the effective mixed is false. That means the only way mixed content could be in effect is if the extension of cell.type causes the mixed="true" to be inherited.
Could someone more familiar with schemas comment on the correct interpretation?
(BTW: if I had control of the schema I would move the mixed="true" to the element definition, but that's not my call.)
Anyone reading my question might want to read this thread also (by Damien). It seems my answer isn't entirely right: parsers/validators don't handle mixed attribute declarations on base/derived elements the same way.
Concerning extended complex types, sub-section 1.4.3.2.2.1 of section 3.4.6 in part 1 of W3C's XML Schema specification says that
Both [derived and base] {content type}s must be mixed or both must be element-only.
So yes, it is inherited (or more like you cannot overwrite it—same thing in the end).
Basically, what you've described is the desired (and as far as I'm concerned) the most logical behavior.
I've created a simple schema to run a little test with Eclipse's XML tools.
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="c">
<xs:complexType>
<xs:complexContent mixed="false">
<xs:extension base="a"/>
</xs:complexContent>
</xs:complexType>
</xs:element>
<xs:complexType name="a" mixed="true">
<xs:sequence minOccurs="0" maxOccurs="unbounded">
<xs:element name="b"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
The above schema is valid, in the sense that not Eclipse's nor W3C's "official" XML Schema validator notices any issues with it.
The following XML passes validation against the aforementioned schema.
<?xml version="1.0" encoding="UTF-8"?>
<c xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="test.xsd">
x
<b/>
y
</c>
So basically you cannot overwrite the mixedness of a complex base type. To support this statement further, try and swap the base and dervied types' mixedness. In that case the XML fails to validate, because the derived type won't be mixed as it (yet again) cannot overwrite the base's mixedness.
You've also said that
Some parsers allow PCDATA directly in the element, while others don't
It couldn't hurt to clarify which parsers are you talking about. A good parser shouldn't fail when it encounters mixed content. A validating parser, given the proper schema, will fail if it encounters mixed content when the schema does not allow it.