Is there a way to override the value of base complextypes value in XSD ?. To be more clear,
i have my base complex type as
<?xml version="1.0" encoding="utf-8"?>
<xs:schema targetNamespace="Urn:commonBase"
elementFormDefault="qualified"
xmlns="Urn:commonBase"
xmlns:mstns="http://tempuri.org/XMLSchema.xsd"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:complexType name="RequestBase">
<xs:attribute name="Version" default="1.0"/>
</xs:complexType>
</xs:schema>
and in another XSD and a different namespace i have
<xs:complexType name="Request">
<xs:complexContent>
<xs:extension base="base:RequestBase">
<xs:sequence>
<xs:element name ="FirstName"/>
</xs:sequence>
<xs:attribute name="Version" fixed="2.0"/>
</xs:extension>
</xs:complexContent>
</xs:complexType>
But when i generate the C# class file for the above using XSD.exe, a duplicate property is generated with the name "Version*1*" . i actually want an override facility in the class file which set the existing property of "RequestBase" from 1.0 to 2.0 and not a extra property. How to acheive this ?
Try using a restriction step to restrict the value of the 'Version' attribute from any simple type to '2.0', then an extension step to add the 'FirstName' element.
(Note that by fixing the value of 'Version' at '2.0' you make it impossible for anyone to base a version 3.0 type on your version 2.0 type in the way you're basing your 2.0 type on the 1.0 type. Do you really want to stick your thumb in the eye of the next guy in line that way?)
Related
This is my xsd
<xs:schema id="RCDNetworkAdapterData" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xs:element name="RCDNetworkAdapterData">
<xs:complexType>
<xs:sequence>
<xs:element name="AdapterName" minOccurs="1" nillable="false" maxOccurs="3">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:minLength value="1"/>
<xs:maxLength value="3"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
And this is the xml that maps to it:
<RCDNetworkAdapterData>
<AdapterName>Ethernet</AdapterName>
<AdapterName>WI-FI</AdapterName>
</RCDNetworkAdapterData>
Please help in modifying the xsd to map to the xml structure
With Saxon as the schema validator I get the error messages:
Validation error on line 2 column 39 of test.xml:
FORG0001: The content "Ethernet" of element <AdapterName> does not match the required
simple type. Value "Ethernet" contravenes the maxLength facet "3" of the type of element AdapterName
See http://www.w3.org/TR/xmlschema-2/#cvc-datatype-valid clause 1
Validation error on line 3 column 36 of test.xml:
FORG0001: The content "WI-FI" of element <AdapterName> does not match the required simple
type. Value "WI-FI" contravenes the maxLength facet "3" of the type of element AdapterName
See http://www.w3.org/TR/xmlschema-2/#cvc-datatype-valid clause 1
Hopefully these messages are clear: you have constrained the values of the AdapterName to be 1 to 3 characters, and your values are longer than this.
I'm trying to use xsd.exe to generate a schema for my assembly. I'm fairly new to xsd but I know I'd like the design style of the file to be in the "russian doll" style, with no global components or types except of course the root element. In fact all of the elements being local would be the best for me. The xsd.exe tool seems to generate a file that advocates high component reusability, probably "salami slice" or some of the other design patterns similar to it.
Is there a way to specify some options when generating the schema so that it follows the russian doll style? If not, is there a tool that will generate xsd this way?
Russian Doll is a simple schema design which you can do yourself without xsd.exe using the idea that there is only one root element everything else inside it .
Read the below example to know more :
<xs:schema>
<xs:element name="HelpDoc">
<xs:complexType>
<xs:sequence>
<xs:element name="Section">
<xs:complexType>
<xs:sequence>
<xs:element name="Title" type="xs:string"/>
<xs:element name="Body" type="xs:string"/>
</xs:sequence>
<xs:attribute name="name" type="xs:string"/>
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:/complexType>
</xs:element>
</xs:schema>
Link here : http://www.ibm.com/developerworks/library/x-schemascope/
I am new to xml schemas, and i have a project for school to make a chess board schema.
Things are going good. and i suppose i could parse this with c# to make sure it is all valid, But i would rather the schema catch the issue.
So here is the issue
i have an xml document that is taking the pieces and making them have a modifier and a location
<pieces>
<black>
<!--Pawns-->
<piece modifier="P">17</piece>
<piece modifier="P">27</piece>
<piece modifier="P">37</piece>
<piece modifier="P">47</piece>
<piece modifier="P">57</piece>
<piece modifier="P">67</piece>
<piece modifier="P">77</piece>
<piece modifier="P">87</piece>
<!--Rooks-->
<piece modifier="R">18</piece>
<piece modifier="R">88</piece>
<!--Knights-->
<piece modifier="N">28</piece>
<piece modifier="N">78</piece>
<!--Bishops-->
<piece modifier="B">38</piece>
<piece modifier="B">68</piece>
<!--Royalty-->
<piece modifier="Q">48</piece>
<piece modifier="K">58</piece>
</black>
</pieces>
there is a few things i would like to do, but i don't know how to go about it, I want to verify that the integer value is not below 11, and not above 88. This is what i have now. I have looked at other stack overflow posts for similar topics, but i couldn't find one that worked.
<xs:element name="black">
<xs:complexType>
<xs:sequence>
<xs:element name="piece" minOccurs="0" maxOccurs="16">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:integer">
<xs:attribute name="modifier" type="xs:string"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element><!--End Piece-->
</xs:sequence>
</xs:complexType>
</xs:element><!--End black-->
Sometimes when XSD doesn't let you do what you want, there is a reason. For example, your values such as 67 and 82 are not really integers, because they are not amenable to integer operations like subtraction. They are actually pairs of digits in the range 1-8. So you might be better off identifying squares on the board in the way that is standard in modern chess notation ("algebraic notation") e.g. h1 or e5. Then you could describe this in XSD as a string restricted with a regular expression <xsd:pattern value="[a-h][1-8]"/>. (Of course you could also use strings restricted to "[1-8][1-8]", but in my view that would be confusing).
If you want to start expressing the semantic rules of chess in your schema, e.g. by having rules that you can't have more than eight pawns of each colour and that pawns can't be on the back row, then you'll need to start using XSD 1.1 assertions.
you have to declare a new simple type and then apply the extension with that simpleType as base.
This would then look like this:
<xs:element name="black">
<xs:complexType>
<xs:sequence>
<xs:element name="piece" minOccurs="0" maxOccurs="16">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="BlackModifiedPiece">
<xs:attribute name="modifier" type="xs:string"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:simpleType name="BlackModifiedPiece">
<xs:restriction base="xs:int">
<xs:minInclusive value="11"/>
<xs:maxInclusive value="88"/>
</xs:restriction>
</xs:simpleType>
Hope this is helpful ;-)
There is something fundemental I don't understand about Xml Schema declarations. I have the following declaration in an .xsd file:
<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:simpleType name="tile">
<xs:restriction base="xs:string">
<xs:pattern value="[a-z][0-9]"/>
</xs:restriction>
</xs:simpleType>
<xs:element name="move">
<xs:complexType>
<xs:sequence>
<xs:element name="T" type="tile" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
So, for example <move><T>a0</T><T>v5</T></move> should be a valid XML element according to the xsd file. (I've simplified the actual declaration, so the move may not make sense)
Background: I am developing a project in C# 4.0. I use this xsd file as a project source. When I receive an XElement from somewhere, I first check whether it is valid according to the xsd above. The C# code works OK.
Here is my question (hopefully a single question, asked three times):
1) I want to put my declarations on a domain. Let's say aliferhat.com. Or do I want to? Why should I want to do that? How can I do that? How can I use that declaration later from somewhere else?
2) I will have many similar xsd files. Most of them will use the "tile" definition, so I want to put the part "tile" on a seperate file, and refer to that file from other xsd files. How can I do that? How will the system know where to look for definitions?
3) This is what the visual studio generates when I add a new XSD file to the project:
<xs:schema id="temp"
targetNamespace="http://tempuri.org/temp.xsd"
elementFormDefault="qualified"
xmlns="http://tempuri.org/temp.xsd"
xmlns:mstns="http://tempuri.org/temp.xsd"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
>
</xs:schema>
What does xmlns:xs and targetNamespace attributes do? Are xs:element and the rest really defined in one of these URIs? Do the C# compiler really look up those URIs for the definitions?
I hope and guess I have asked only one question. I've read the "XML schema definition" page on W3 schools but could not find the answer. Any help will be appreciated.
I have a sequence with an allowed minimum length of zero in my xsd. When I try and load an xml file which doesn't have any elements of the sequence into the DataSet that xsd.exe created I get an exception indicating that my file violated one of the DataSet's constraints. The xml file validates against the schema so I know it's valid. Is there anything I can do to make the tool generate a valid dataset?
<xs:sequence minOccurs="0" maxOccurs="unbounded">
<xs:element name="Numbers" type="xs:double"/>
</xs:sequence>
Edit: if I change my schema to this the generated code works properly. It looks wrong to me though since it appears to be implying that I could have sequence items with nothing in them, which doesn't make any sense.
<xs:sequence minOccurs="0" maxOccurs="unbounded">
<xs:element name="Numbers" type="xs:double" minOccurs="0"/>
</xs:sequence>
This sounds like it is because of the way that xsd.exe handles minOccurs and maxOccurs. This MSDN article describes the way that xsd.exe handles minOccurs / maxOccurs. This section of that article seems to tally with your initial problem:
A loss of definition precision occurs
when Xsd.exe ignores the minOccurs
attribute in the case when the
maxOccurs attribute value dictates an
array binding. A reverse translation
from the generated array to a new
declaration produces not the
original minOccurs value but a value
of 0, plus a maxOccurs value of
unbounded.