I have been tasked to replace the xsd for a particular solution. However, I keep getting an "element is not supported in this context."
Here is the original xsd:
public const string Xsd = #"
<xs:schema attributeFormDefault='unqualified' elementFormDefault='qualified' xmlns:xs='http://www.w3.org/2001/XMLSchema'>
<xs:element name='DataRow'>
<xs:complexType>
<xs:sequence>
<xs:element maxOccurs='unbounded' name='Data'>
<xs:complexType>
<xs:attribute name='Site' type='xs:string' use='required' />
<xs:attribute name='Month_Num' type='xs:unsignedShort' use='required' />
<xs:attribute name='Numerator' type='xs:unsignedByte' use='required' />
<xs:attribute name='Data_Indicator' type='xs:string' use='required' />
<xs:attribute name='Budgeted' type='xs:unsignedByte' use='required' />
<xs:attribute name='Executive_Comments' type='xs:string' use='required' />
<xs:attribute name='Fleet_Executive_Comments' type='xs:string' use='required' />
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>";
Here is what I am supposed to be replacing it with:
<xs:schema attributeFormDefault='unqualified' elementFormDefault='qualified' xmlns:xs='http://www.w3.org/2001/XMLSchema'>
<MonthlyValues>
<MonthlyValue IndicatorName='name' LocationName='name' GroupingName='name' Year='MonthNum.Value.Year' Month='MonthNum.Value.Month' Numerator='Numerator' Budget='Budget'>
</MonthlyValue>
</MonthlyValues>
</xs:schema>
The schema was made by someone else and I was supposed to just be able to replace it. Unfortunately its not working out that way and I know very little about it.
should I change
<MonthlyValues>
to
<xs:element name='MonthlyValues> and keep the
<xs:sequence>
<xs:element maxOccurs='unbounded' name='MonthlyValues'>
<xs:complexType>
and add the
<MonthlyValue IndicatorName='name' LocationName='name' GroupingName='name' Year='MonthNum.Value.Year' Month='MonthNum.Value.Month' Numerator='Numerator' Budget='Budget'>
</MonthlyValue>
afterward? Actually, I tried that and it didn't work, but is there something similar I have to do?
XSD is something else... you do seem to be new to XSD so maybe the quickest way to get you started is to generate an XSD from your sample XML. Tweak the generated to match the XMLs. Use the XSD below as a starting point.
<?xml version="1.0" encoding="utf-8"?>
<!--XML Schema generated by QTAssistant/XML Schema Refactoring (XSR) Module (http://www.paschidev.com)-->
<xsd:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="MonthlyValues">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="MonthlyValue">
<xsd:complexType>
<xsd:simpleContent>
<xsd:extension base="xsd:string">
<xsd:attribute name="IndicatorName" type="xsd:string" use="required" />
<xsd:attribute name="LocationName" type="xsd:string" use="required" />
<xsd:attribute name="GroupingName" type="xsd:string" use="required" />
<xsd:attribute name="Year" type="xsd:string" use="required" />
<xsd:attribute name="Month" type="xsd:string" use="required" />
<xsd:attribute name="Numerator" type="xsd:string" use="required" />
<xsd:attribute name="Budget" type="xsd:string" use="required" />
</xsd:extension>
</xsd:simpleContent>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
You should rely on an editor to help you through the learning... Eclipse, Netbeans, etc. come with decent editors, and free.
Related
Is it possible to reference an element in xsd, so that its content is written into another?
I think of someting like "shipping address" and "billing address".
If you choose that "billing address" is the same as "shipping address", than take the content of field "shipping address" and write it into "billing address".
I'm new to xsd/xml, so suggestions and criticism are welcome.
This is what I've got so far...
<xsd:group name="c_Shipping_Adress">
<xsd:element name="shipping_address">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="Title"/>
<xsd:element name="First_Name"/>
<xsd:element name="Last_Name"/>
<xsd:element name="Street"/>
<xsd:element name="Zip"/>
<xsd:element name="City"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:group>
<xsd:group name="c_Billing_Adress">
<xsd:element name="billing_address">
<xsd:complexType>
<xsd:choice>
<xsd:element name="is_same_as_shipping_address">
<xsd:group ref="shipping_address"/>
</xsd:element>
<xsd:group name="is_not_same_as_shipping_address">
<xsd:sequence>
<xsd:element name="Title"/>
<xsd:element name="First_Name"/>
<xsd:element name="Last_Name"/>
<xsd:element name="Street"/>
<xsd:element name="Zip"/>
<xsd:element name="City"/>
</xsd:sequence>
</xsd:group>
</xsd:choice>
</xsd:complexType>
</xsd:element>
</xsd:group>
To be honest its not very clear what your asking, and your XSD is not structured correctly, making it difficult to determine your intentions. But hopefully the following will provide enough of a basis to build on.
<?xml version="1.0" encoding="utf-8" ?>
<!--Created with Liquid Studio 2019 BETA (https://www.liquid-technologies.com)-->
<xs:schema elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="Root">
<xs:complexType>
<xs:sequence>
<xs:element name="ShippingAddress" type="AddressType" />
<xs:element name="BillingAddress" type="AddressType" />
<xs:element name="OtherAddress">
<xs:complexType>
<xs:complexContent>
<xs:extension base="AddressType">
<xs:sequence>
<xs:element name="ExtraThing" type="xs:string" />
</xs:sequence>
</xs:extension>
</xs:complexContent>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:complexType name="AddressType">
<xs:sequence>
<xs:element name="Title" type="xs:string" />
<xs:element name="First_Name" type="xs:string" />
<xs:element name="Last_Name" type="xs:string" />
<xs:element name="Street" type="xs:string" />
<xs:element name="Zip" type="xs:string" />
<xs:element name="City" type="xs:string" />
</xs:sequence>
</xs:complexType>
</xs:schema>
My XML looks like . I have two elements with same name. But I am not being able to get those elements in XSD.
Here Book is the element that appears twice but has different attributes. All attributes in their respective Book elements are required.
The error says
Element Books is not consistent with element Books
XML :
<TestRoot>
<Test Shelf="1">
<Value>
<Book Name="Wolves" />
</Value>
</Test>
<Test Shelf="2">
<Value>
<Book Name="Dogs" Pages="500" Photos="50" />
</Value>
</Test>
</TestRoot>
My XSD looks like :
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema....>
<xsd:element name="TestRoot">
<xsd:complexType>
<xsd:sequence minOccurs="1" maxOccurs="1">
<xsd:element name="Test" minOccurs="1" maxOccurs="unbounded">
<xsd:complexType>
<xsd:choice>
<xsd:element name="Value">
<xsd:complexType>
<xsd:choice minOccurs="1" maxOccurs="1">
<xsd:element name="Book">
<xsd:complexType>
<xsd:attribute name="Name" use="required">
<xsd:simpleType>
<xsd:restriction base="xsd:string">
<xsd:maxLength value="40"/>
<xsd:enumeration value="Wolves"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:attribute>
</xsd:complexType>
</xsd:element>
<xsd:element name="Book">
<xsd:complexType>
<xsd:attribute name="Book" use="required">
<xsd:simpleType>
<xsd:restriction base="xsd:string">
<xsd:maxLength value="40"/>
<xsd:enumeration value="Dogs"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:attribute>
<xsd:attribute name="Pages" use="required">
<xsd:simpleType>
<xsd:restriction base="xsd:string">
<xsd:maxLength value="50"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:attribute>
<xsd:attribute name="Photos" use="required">
<xsd:simpleType>
<xsd:restriction base="xsd:string">
<xsd:maxLength value="24"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:attribute>
</xsd:complexType>
</xsd:element>
</xsd:choice>
</xsd:complexType>
</xsd:element>
</xsd:choice>
<xsd:attribute name="Shelf" use="optional">
<xsd:simpleType>
<xsd:restriction base="xsd:string">
<xsd:maxLength value="100"/>
<xsd:enumeration value="1"/>
<xsd:enumeration value="2"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:attribute>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
Not sure where I am wrong ? Any help?
Try something like this:
<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="TestRoot" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xs:element name="TestRoot" msdata:IsDataSet="true" msdata:Locale="en-US">
<xs:complexType>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="Test">
<xs:complexType>
<xs:sequence>
<xs:element name="Value" minOccurs="0" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<!-- the <Book> node is just of "BookType" type -->
<xs:element name="Book" type="BookType" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="Shelf" type="xs:string" />
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
</xs:element>
<!-- define the "BookType" which represents a single <Book> node -->
<xs:complexType name="BookType">
<xs:attribute name="Name" type="xs:string" use="required" />
<xs:attribute name="Pages" type="xs:string" />
<xs:attribute name="Photos" type="xs:string" />
</xs:complexType>
</xs:schema>
This XSD defines a new complex type BookType which is used to define how the <Book> nodes inside <Value> look like.
In general, I would also define a complex type for any of the XML nodes you have - I'd create a ValueType to handle how a <Value> node is made up, and I'd create a TestType for <Test> and a TestRootType for the <TestRoot> as well.
Having those really deeply nested XSD structures makes it really hard to "grasp" what's going on - defining a type for each node and then assigning them to the nodes makes it much more readable and understandable, in my opinion
Can you have an allready restricted type and then derive from this type by extension and add elements that do not fit to a base type?
<xsd:complexType name="absHcontainerType">
<xsd:complexContent>
<xsd:restriction base="e:urContentType">
<xsd:sequence>
<xsd:element ref="e:absMcontainer" minOccurs="0"
maxOccurs="1" />
<xsd:element ref="e:absHtitle" minOccurs="1" maxOccurs="unbounded" />
<xsd:choice minOccurs="1" maxOccurs="unbounded">
<xsd:element ref="e:absMcontainer" />
<xsd:element ref="e:absHcontainer" />
<xsd:element ref="e:absContainer" />
</xsd:choice>
</xsd:sequence>
<xsd:attributeGroup ref="e:typehcontainer" />
<xsd:attributeGroup ref="e:anyattr" />
</xsd:restriction>
</xsd:complexContent>
</xsd:complexType>
And then derive this like this:
<xsd:complexType name="absHcontainerType2">
<xsd:complexContent>
<xsd:extension base="absHcontainerType">
<xs:sequence>
<xs:element name="xy" type="xs:string"/>
<xs:element name="xyz" type="xs:string"/>
</xs:sequence>
</xs:extension>
</xsd:complexContent>
</xsd:complexType>
As long as you don't put back things that got restricted away, I wouldn't expect a problem. Are you in fact getting an error when you try it?
I've created a key/keyref on the root element in order to create document-wide uniqueness based on the specified element.
Therefore, via .//foo/#name every occurrence of #name across all instances of foo must be unique; likewise for .//bar/#name. This seems to be working fine. These are referenced by .//foo-ref/#name-ref and .//bar-ref/#name-ref respectively, also defined at the root node.
However, I've gathered that one cannot create an optional key, and this is presenting a bit of a problem. Semantically, by the nature of the conforming documents, a key is not required on every single instance of foo or bar. The instances of foo-ref/#name-ref would obviously need to target an existing foo/#name, but it isn't semantically invalid for a foo to be without a #name.
Is there any work-around for this? I don't like the idea of consumers having to define a key for every single element, when reasonably only a handful will need them.
Here's the example schema (of course, I'm not deploying some foobar schema, but the structure is identical; this is just a testing schema I've been toying with)
<xs:schema elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:complexType name="ref">
<xs:attribute name="name-ref" type="xs:string" use="required" />
</xs:complexType>
<xs:complexType name="obj">
<xs:attribute name="name" type="xs:string" use="optional" />
</xs:complexType>
<xs:complexType name="foo">
<xs:complexContent>
<xs:extension base="obj">
<xs:sequence>
<xs:choice maxOccurs="unbounded">
<xs:element name="foo" type="foo" />
<xs:element name="bar" type="bar" />
<xs:element name="foo-ref" type="foo-ref" />
<xs:element name="bar-ref" type="bar-ref" />
</xs:choice>
</xs:sequence>
</xs:extension>
</xs:complexContent>
</xs:complexType>
<xs:complexType name="foo-ref">
<xs:complexContent>
<xs:extension base="ref" />
</xs:complexContent>
</xs:complexType>
<xs:complexType name="bar">
<xs:complexContent>
<xs:extension base="obj">
<xs:sequence>
<xs:choice maxOccurs="unbounded">
<xs:element name="bar" type="bar" />
<xs:element name="qux" type="qux" />
<xs:element name="bar-ref" type="bar-ref" />
</xs:choice>
</xs:sequence>
</xs:extension>
</xs:complexContent>
</xs:complexType>
<xs:complexType name="bar-ref">
<xs:complexContent>
<xs:extension base="ref" />
</xs:complexContent>
</xs:complexType>
<xs:complexType name="qux">
<xs:simpleContent>
<xs:extension base="xs:string" />
</xs:simpleContent>
</xs:complexType>
<xs:element name="root">
<xs:complexType>
<xs:sequence>
<xs:element name="foo" type="foo" maxOccurs="unbounded" />
</xs:sequence>
</xs:complexType>
<xs:key name="foo">
<xs:selector xpath=".//foo" />
<xs:field xpath="#name" />
</xs:key>
<xs:key name="bar">
<xs:selector xpath=".//bar" />
<xs:field xpath="#name" />
</xs:key>
<xs:keyref name="foo-ref" refer="foo">
<xs:selector xpath=".//foo-ref" />
<xs:field xpath="#name-ref" />
</xs:keyref>
<xs:keyref name="bar-ref" refer="bar">
<xs:selector xpath=".//bar-ref" />
<xs:field xpath="#name-ref" />
</xs:keyref>
</xs:element>
</xs:schema>
Addendum
Just following up with my revisions thanks to #PetruGardea. So unique can be referenced by a keyref, who knew? (not me apparently)
<xs:element name="root">
<xs:complexType>
<xs:sequence>
<xs:element name="foo" type="foo" maxOccurs="unbounded" />
</xs:sequence>
</xs:complexType>
<xs:keyref name="foo-ref" refer="foo">
<xs:selector xpath=".//foo-ref" />
<xs:field xpath="#name-ref" />
</xs:keyref>
<xs:keyref name="bar-ref" refer="bar">
<xs:selector xpath=".//bar-ref" />
<xs:field xpath="#name-ref" />
</xs:keyref>
<!--
the use of xs:unique here, in lieu of xs:key allows for
nullable "keys", retaining referential integrity with the
above defined keyrefs. awesome possum.
-->
<xs:unique name="foo">
<xs:selector xpath=".//foo" />
<xs:field xpath="#name" />
</xs:unique>
<xs:unique name="bar">
<xs:selector xpath=".//bar" />
<xs:field xpath="#name" />
</xs:unique>
</xs:element>
Use xsd:unique; unlike a key, its matched value is either unique or nil (nil or not present).
Example:
<?xml version="1.0" encoding="utf-8" ?>
<!-- XML Schema generated by QTAssistant/XSD Module (http://www.paschidev.com) -->
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="root">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="uk" maxOccurs="unbounded">
<xsd:complexType>
<xsd:attribute name="name" type="xsd:string"/>
</xsd:complexType>
</xsd:element>
<xsd:element name="fk" maxOccurs="unbounded">
<xsd:complexType>
<xsd:attribute name="name" type="xsd:string"/>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
<xsd:unique name="uq">
<xsd:selector xpath="uk"/>
<xsd:field xpath="#name"/>
</xsd:unique>
<xsd:keyref name="fk" refer="uq">
<xsd:selector xpath="fk"/>
<xsd:field xpath="#name"/>
</xsd:keyref>
</xsd:element>
</xsd:schema>
Sample (valid) XML:
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<!-- Sample XML generated by QTAssistant (http://www.paschidev.com) -->
<root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<uk name="name1"/>
<uk />
<fk/>
<fk name="name1"/>
</root>
You can't put choice tag inside the all tag. So, is there any workaround to get this functionallity?
For example, I have<settings> tag like:
<settings>
<logging />
<sending />
<useonly />
</settings>
Or something like
<settings>
<logging />
<notuseonly />
<sending />
</settings>
So I want to prevent <useonly> and <notuseonly> showing up together, while the order is not important. And if allowed, in XSD it would look like:
<xs:all>
<xs:element minOccurs="0" maxOccurs="1" ref="sending" />
<xs:element minOccurs="0" maxOccurs="1" ref="logging" />
<xs:choice>
<xs:element minOccurs="0" maxOccurs="1" ref ="useonly" />
<xs:element minOccurs="0" maxOccurs="1" ref ="notuseonly" />
</xs:choice>
</xs:all>
Any thoughts?
Check this link: http://www.w3.org/wiki/Needs_choice_inside_all
I summarize for you the solutions proposed:
One solution is to wrap the element that can change inside another:
<xsd:all>
<xsd:element minOccurs="0" maxOccurs="1" ref="sending" />
<xsd:element minOccurs="0" maxOccurs="1" ref="logging"/>
<xsd:element minOccurs="0" maxOccurs="1" ref ="usetype"/>
</xsd:all>
<xsd:element name="usetype">
<xsd:complexType>
<xsd:choice>
<xsd:element ref="useonly"/>
<xsd:element ref="notuseonly"/>
</xsd:choice>
</xsd:complexType>
</xsd:element>
The other one is to use a substitution group:
<xsd:all>
<xsd:element ref="sending"/>
<xsd:element ref="logging"/>
<xsd:element ref="usetype"/>
</xsd:all>
</xsd:complexType>
<xsd:element name="usetype" abstract="true"/>
<xsd:element name="useonly" substitutionGroup="usetype"> ... </xsd:element>
<xsd:element name="notuseonly" substitutionGroup="usetype"> ... </xsd:element>