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 ;-)
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 have a XSD file, which I need to convert to a C# class. There are few issues though:
Issue 1: All the elements are in a Hungarian language (and I don't speak it). What I would like to do, is to translate all the elements (I have translations), so that C# class will make sense to me, but on the output I need to see those original names.
Question: Can I simply translate all the elements and then simply use Attributes to specify the output name?.
Issue 2: This XSD schema contains custom types. Let me show you an example:
<xs:element name="szlatipus" type="szlatipus_tipus">
<xs:annotation>
<xs:documentation>A számla típusa: 1-számla/2-egyszerűsített adattartalmú számla/3-módosító számla/4-érvénytelenítő számla/5-gyűjtőszámla/6-számlával egy tekintet alá eső okirat</xs:documentation>
</xs:annotation>
</xs:element>
Where in the end of the document, this szlatipus_tipus is defined as following:
<xs:simpleType name="szlatipus_tipus">
<xs:annotation>
<xs:documentation>A számla típusát meghatározó kódszótár.</xs:documentation>
</xs:annotation>
<xs:restriction base="xs:integer">
<xs:enumeration value="1">
<xs:annotation>
<xs:documentation>1 - Számla</xs:documentation>
</xs:annotation>
</xs:enumeration>
<xs:enumeration value="2">
<xs:annotation>
<xs:documentation>2 - Egyszerűsített adattartalmú számla</xs:documentation>
</xs:annotation>
</xs:enumeration>
<xs:enumeration value="3">
<xs:annotation>
<xs:documentation>3 - Módosító számla</xs:documentation>
</xs:annotation>
</xs:enumeration>
<xs:enumeration value="4">
<xs:annotation>
<xs:documentation>4 - Érvénytelenítő számla</xs:documentation>
</xs:annotation>
</xs:enumeration>
<xs:enumeration value="5">
<xs:annotation>
<xs:documentation>5 - Gyűjtő számla</xs:documentation>
</xs:annotation>
</xs:enumeration>
<xs:enumeration value="6">
<xs:annotation>
<xs:documentation>6 - Számlával egy tekintet alá eső okirat</xs:documentation>
</xs:annotation>
</xs:enumeration>
</xs:restriction>
</xs:simpleType>
This I believe is causing problems when I tried to use regular Visual Studio dev. prompt xsd schema /classes. The output was a complete mess. I have also tried Liquid XML yet the result was more or less the same - Mess.
I can surely parse and create class myself, yet I was wondering if there is a tool, which can generate clean and simple C# class that I can use in my project?
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/
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?)
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.