WCF schema issue - wcf-client

The below is schema of the service.
<xs:element name="StartDate" type="tns:DateType">
<xs:annotation>
<xs:documentation>The start date of the planning item in question</xs:documentation>
</xs:annotation>
</xs:element>
<xs:complexType name="DateType">
<xs:annotation>
<xs:documentation>
0 - Hard Date (Use the date defined by the year, month and day attributes
2 - Retirement
3 - Death
4 - Disability
5 - Long Term Care
</xs:documentation>
</xs:annotation>
<xs:sequence>
<xs:element name="Date">
<xs:complexType>
<xs:attribute name="date_type" use="optional" default="0">
<xs:simpleType>
<xs:restriction base="xs:int">
<xs:enumeration value="0">
<xs:annotation>
<xs:documentation>Hard Date (Use the date defined by the year, month and day attributes</xs:documentation>
</xs:annotation>
</xs:enumeration>
<xs:enumeration value="2">
<xs:annotation>
<xs:documentation>Retirement</xs:documentation>
</xs:annotation>
</xs:enumeration>
<xs:enumeration value="3">
<xs:annotation>
<xs:documentation>Death</xs:documentation>
</xs:annotation>
</xs:enumeration>
<xs:enumeration value="4">
<xs:annotation>
<xs:documentation>Disability</xs:documentation>
</xs:annotation>
</xs:enumeration>
<xs:enumeration value="5">
<xs:annotation>
<xs:documentation>Long Term Care</xs:documentation>
</xs:annotation>
</xs:enumeration>
</xs:restriction>
</xs:simpleType>
</xs:attribute>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
This is the proxy class
public partial class DateTypeDate
{
private int date_typeField;
public DateTypeDate()
{
this.date_typeField = 0;
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
[System.ComponentModel.DefaultValueAttribute(0)]
public int date_type
{
get
{
return this.date_typeField;
}
set
{
this.date_typeField = value;
}
}
}
I have a requirement where we need to send 0 value in date_type.
1- I tried the below by assuming that by default it should assign "0" value.
StartDate = new DateType();
DateTypeDate date = new DateTypeDate();
2- Then i explicitly assign "date_type" value to 0
StartDate = new DateType();
DateTypeDate date = new DateTypeDate();
date.date_type = 0;
StartDate.Date = date;
In both cases "date_type" node doesn't get appear in actual request xml being sent over to service. This is request xml which gets generated.
<StartDate>
<Date></Date>
</StartDate>
But if i assign a different value than 0 then i can see the date_type node. for example
<StartDate>
<Date date_type="2"></Date>
</StartDate>
Would you able to help what could be the reason that node doesn't appear in request xml id i assign it to "0" also is there any way the node can appear without making chnages in schema. Thanks in advance

I'm not an XML Schema expert, but it seems to me the problem is almost certainly because of the following line in the schema:
<xs:attribute name="date_type" use="optional" default="0">
If the attribute is optional that means the client doesn't have to send it. Try making it required instead.

Related

XML Export - Denormalized Data

I work for a financial firm who are required to report trades, sales data etc. to the regulator. At the moment we are doing this per individual because we cannot get their XML schema template to work. I am trying to import this into excel using the xml source pane.
The problem we have is that when we come to export the document, there is a denormalized data error which we can't get around. Unfortunately, myself and the team haven’t got much experience in mapping XML to know how to properly troubleshoot it. I’ve changed various bits of code to no success and even contacted the FCA (who say there is nothing wrong with it or dead links…lol).
Can someone offer some insight or guidance on how we can correct this issue?
The Schema is as follows:
<?xml version="1.0" encoding="utf-8"?>
<!-- edited with XMLSpy v2005 U (http://www.xmlspy.com) by Clive Raven (LogicaCMG) -->
<!--
********************************************************************************
*
* Project Name : FSA Transaction Reporting System
* Reference :
* Description : Definition For FSA High-Street Firms Feed For Retail Investment Products
* Dependencies : http://www.fsa.gov.uk/XMLSchema/FSAFeedCommon-v1-1.xsd
*
* Revision History
* Version Author Date Description
* 1.1 FSA 07/04/2004 First published draft
* 1.2 FSA 22/11/2004 Final draft
* 1.3 FSA 19/02/2007 Added Self Invested Personal Pensions (SIPPs) product type in TypeOfPolicyType
*
* Copyright Financial Services Authority 2004
********************************************************************************
-->
<xs:schema xmlns="http://www.fsa.gov.uk/XMLSchema/FSAHSFFeedRI-v1-2"
xmlns:cmn="http://www.fsa.gov.uk/XMLSchema/FSAFeedCommon-v1-2"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.fsa.gov.uk/XMLSchema/FSAHSFFeedRI-v1-2"
elementFormDefault="qualified"
version="1.0"
id="FSAHSFFeedRI-v1-3">
<xs:import namespace="http://www.fsa.gov.uk/XMLSchema/FSAFeedCommon-v1-2"
schemaLocation="https://gabriel.fca.org.uk/specifications/MER/DRG/PSD-CommonTypes/v1.2/FSAFeedCommon-v1-2.xsd"/>
<!--Types used in FSAHSFFeedRI-->
<xs:simpleType name="TypeOfPolicyType">
<xs:annotation>
<xs:documentation>Retail Investment Product Code. </xs:documentation>
</xs:annotation>
<xs:restriction base="xs:string">
<xs:enumeration value="01">
<xs:annotation>
<xs:documentation>Unit trust/OEIC</xs:documentation>
</xs:annotation>
</xs:enumeration>
<xs:enumeration value="02">
<xs:annotation>
<xs:documentation>Investment trust</xs:documentation>
</xs:annotation>
</xs:enumeration>
<xs:enumeration value="03">
<xs:annotation>
<xs:documentation>ISA</xs:documentation>
</xs:annotation>
</xs:enumeration>
<xs:enumeration value="04">
<xs:annotation>
<xs:documentation>Structured capital at risk product</xs:documentation>
</xs:annotation>
</xs:enumeration>
<xs:enumeration value="05">
<xs:annotation>
<xs:documentation>With profit bond</xs:documentation>
</xs:annotation>
</xs:enumeration>
<xs:enumeration value="06">
<xs:annotation>
<xs:documentation>Unit linked bond</xs:documentation>
</xs:annotation>
</xs:enumeration>
<xs:enumeration value="07">
<xs:annotation>
<xs:documentation>Distribution bond</xs:documentation>
</xs:annotation>
</xs:enumeration>
<xs:enumeration value="08">
<xs:annotation>
<xs:documentation>With profit endowment</xs:documentation>
</xs:annotation>
</xs:enumeration>
<xs:enumeration value="09">
<xs:annotation>
<xs:documentation>Endowment savings plan</xs:documentation>
</xs:annotation>
</xs:enumeration>
<xs:enumeration value="10">
<xs:annotation>
<xs:documentation>Mortgage endowment</xs:documentation>
</xs:annotation>
</xs:enumeration>
<xs:enumeration value="11">
<xs:annotation>
<xs:documentation>Guaranteed income/growth/investment bond</xs:documentation>
</xs:annotation>
</xs:enumeration>
<xs:enumeration value="12">
<xs:annotation>
<xs:documentation>Trustee investment bond</xs:documentation>
</xs:annotation>
</xs:enumeration>
<xs:enumeration value="13">
<xs:annotation>
<xs:documentation>Life annuity</xs:documentation>
</xs:annotation>
</xs:enumeration>
<xs:enumeration value="14">
<xs:annotation>
<xs:documentation>Pension annuity</xs:documentation>
</xs:annotation>
</xs:enumeration>
<xs:enumeration value="15">
<xs:annotation>
<xs:documentation>Long term care insurance</xs:documentation>
</xs:annotation>
</xs:enumeration>
<xs:enumeration value="16">
<xs:annotation>
<xs:documentation>Stakeholder pension</xs:documentation>
</xs:annotation>
</xs:enumeration>
<xs:enumeration value="17">
<xs:annotation>
<xs:documentation>Personal pension</xs:documentation>
</xs:annotation>
</xs:enumeration>
<xs:enumeration value="18">
<xs:annotation>
<xs:documentation>Group personal pension</xs:documentation>
</xs:annotation>
</xs:enumeration>
<xs:enumeration value="19">
<xs:annotation>
<xs:documentation>FSAVC</xs:documentation>
</xs:annotation>
</xs:enumeration>
<xs:enumeration value="20">
<xs:annotation>
<xs:documentation>Individual pension transfer</xs:documentation>
</xs:annotation>
</xs:enumeration>
<xs:enumeration value="21">
<xs:annotation>
<xs:documentation>Pension opt out</xs:documentation>
</xs:annotation>
</xs:enumeration>
<xs:enumeration value="22">
<xs:annotation>
<xs:documentation>Section 32 buy out</xs:documentation>
</xs:annotation>
</xs:enumeration>
<xs:enumeration value="23">
<xs:annotation>
<xs:documentation>Group section 32 buy out</xs:documentation>
</xs:annotation>
</xs:enumeration>
<xs:enumeration value="24">
<xs:annotation>
<xs:documentation>Income drawdown</xs:documentation>
</xs:annotation>
</xs:enumeration>
<xs:enumeration value="25">
<xs:annotation>
<xs:documentation>Executive pension</xs:documentation>
</xs:annotation>
</xs:enumeration>
<xs:enumeration value="26">
<xs:annotation>
<xs:documentation>SSAS</xs:documentation>
</xs:annotation>
</xs:enumeration>
<xs:enumeration value="27">
<xs:annotation>
<xs:documentation>Group money purchase</xs:documentation>
</xs:annotation>
</xs:enumeration>
<xs:enumeration value="28">
<xs:annotation>
<xs:documentation>AVC final salary</xs:documentation>
</xs:annotation>
</xs:enumeration>
<xs:enumeration value="29">
<xs:annotation>
<xs:documentation>AVC group money purchase</xs:documentation>
</xs:annotation>
</xs:enumeration>
<xs:enumeration value="30">
<xs:annotation>
<xs:documentation>Self Invested Personal Pensions (SIPPs)</xs:documentation>
</xs:annotation>
</xs:enumeration>
<xs:enumeration value="99">
<xs:annotation>
<xs:documentation>Other. Use this when product is not one of above.</xs:documentation>
</xs:annotation>
</xs:enumeration>
</xs:restriction>
</xs:simpleType>
<!--Structures used in FSAHSFFeedRI -->
<xs:complexType name="RetailInvestmentStructure">
<xs:sequence>
<xs:element name="PrincipalOrNetworkFSARef" type="cmn:FSARefType" minOccurs="0">
<xs:annotation>
<xs:documentation>This field only applies if the sale has been made by an intermediary who has a principal or is part of a network. </xs:documentation>
</xs:annotation>
</xs:element>
<xs:element name="TypePolicy" type="TypeOfPolicyType">
<xs:annotation>
<xs:documentation>Type of policy.</xs:documentation>
</xs:annotation>
</xs:element>
<xs:element name="AdvisedSale" type="cmn:YNType" minOccurs="0">
<xs:annotation>
<xs:documentation>This field is optional until July 2006, when it will become mandatory. Y = Sale was Advised, N = Sale was non-Advised. For PSD reporting purposes non-Advised includes execution only and direct offer transactions.</xs:documentation>
</xs:annotation>
</xs:element>
<xs:element name="CustPostCode" type="cmn:PostCodeType" minOccurs="0">
<xs:annotation>
<xs:documentation>Full U.K postcode of customer.</xs:documentation>
</xs:annotation>
</xs:element>
<xs:element name="PremPaymentMethod" type="cmn:PremiumPaymentMethodType">
<xs:annotation>
<xs:documentation>Method of premium/ contribution payment.</xs:documentation>
</xs:annotation>
</xs:element>
<xs:element name="TotalPremAmt" type="cmn:HSFStdSterlingAmountType">
<xs:annotation>
<xs:documentation>Total premium/contribution amount. Annualised amount rounded to nearest pound.</xs:documentation>
</xs:annotation>
</xs:element>
<xs:element name="CustDOB" type="xs:date" minOccurs="0">
<xs:annotation>
<xs:documentation>Date of birth of customer. Applies to first named customer at time of sale i.e. age obtained at proposal stage. Must represent an age up to and including 115.</xs:documentation>
</xs:annotation>
</xs:element>
<xs:element name="DateInForce" type="xs:date"/>
</xs:sequence>
</xs:complexType>
<!--FSAHSFFeedRI -->
<xs:element name="FSAHSFFeedRI">
<xs:complexType>
<xs:sequence>
<xs:element ref="cmn:FSAFeedHeader"/>
<xs:element name="FSAHSFFeedRIMsg" minOccurs="0" maxOccurs="unbounded">
<xs:annotation>
<xs:documentation>Defines the inidividual transactions</xs:documentation>
</xs:annotation>
<xs:complexType>
<xs:sequence>
<xs:element ref="cmn:CoreItems"/>
<xs:element name="RetailInvestment" type="RetailInvestmentStructure">
<xs:annotation>
<xs:documentation>Defines the elements that are specific to a Retail Investment transaction.</xs:documentation>
</xs:annotation>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
When importing the schema there is an error importing the common feed due to a dead link referenced in the schema:
SchemaLocation="https://gabriel.fca.org.uk/specifications/MER/DRG/PSD-CommonTypes/v1.2/FSAFeedCommon-v1-2.xsd
This can be downloaded separately from the FCA resources section and the schema can be edited to point to the file locally. See below:
<?xml version="1.0" encoding="utf-8"?>
<!-- edited with XMLSpy v2005 U (http://www.xmlspy.com) by Clive Raven (LogicaCMG) -->
<!--
********************************************************************************
*
* Project Name : FSA Transaction Reporting System
* Reference :
* Description : Common Definitions For FSA Feeds
* Dependencies :
*
* Revision History
* Version Author Date Description
* 1.1 FSA 07/04/2004 Published with early draft of SBD Schema's
* 1.2 FSA 22/11/2004 Published with final draft of all TRS Schema's
*
* Copyright Financial Services Authority 2004
********************************************************************************
-->
<xs:schema xmlns="http://www.fsa.gov.uk/XMLSchema/FSAFeedCommon-v1-2"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.fsa.gov.uk/XMLSchema/FSAFeedCommon-v1-2"
elementFormDefault="qualified"
version="1.2"
id="FSAFeedCommon-v1-2">
<!--Common Types -->
<xs:simpleType name="NonEmptyString">
<xs:annotation>
<xs:documentation>Defines a string type that may not be the empty string. Values of this type are also whitespace normalized meaning all-whitespace strings are also invalid.</xs:documentation>
</xs:annotation>
<xs:restriction base="xs:string">
<xs:whiteSpace value="collapse"/>
<xs:minLength value="1"/>
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="FSARefType">
<xs:annotation>
<xs:documentation>FSA Reference Number. Either a 6 or 7 digit number. Leading zeroes are not expected.</xs:documentation>
</xs:annotation>
<xs:restriction base="xs:string">
<xs:pattern value="[1-9]{1}[0-9]{5,6}"/>
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="PostCodeType">
<xs:annotation>
<xs:documentation>Full or abbreviated UK postcode.</xs:documentation>
</xs:annotation>
<xs:restriction base="xs:string">
<xs:maxLength value="8"/>
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="YNType">
<xs:annotation>
<xs:documentation>Y or N</xs:documentation>
</xs:annotation>
<xs:restriction base="xs:string">
<xs:enumeration value="Y"/>
<xs:enumeration value="N"/>
</xs:restriction>
</xs:simpleType>
<!--Common SBD Types -->
<xs:simpleType name="HSFStdSterlingAmountType">
<xs:annotation>
<xs:documentation>Integer from 1 to 999999999. Representing sterling-equivalent amount.</xs:documentation>
</xs:annotation>
<xs:restriction base="xs:positiveInteger">
<xs:totalDigits value="9"/>
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="PremiumPaymentMethodType">
<xs:annotation>
<xs:documentation>Method of Premium/Contribution payment. Single or Regular.</xs:documentation>
</xs:annotation>
<xs:restriction base="xs:string">
<xs:enumeration value="S">
<xs:annotation>
<xs:documentation>Single payment.</xs:documentation>
</xs:annotation>
</xs:enumeration>
<xs:enumeration value="R">
<xs:annotation>
<xs:documentation>Regular payment.</xs:documentation>
</xs:annotation>
</xs:enumeration>
</xs:restriction>
</xs:simpleType>
<!--Common Structures -->
<xs:element name="CoreItems">
<xs:annotation>
<xs:documentation>Defines the elements that are common to all transactions.</xs:documentation>
</xs:annotation>
<xs:complexType>
<xs:sequence>
<xs:element name="FirmFSARef" type="FSARefType">
<xs:annotation>
<xs:documentation>The FSA code of the reporting firm for this transaction.</xs:documentation>
</xs:annotation>
</xs:element>
<xs:element name="TransRef">
<xs:annotation>
<xs:documentation>The unique reference, internal to the reporting firm, that will enable the firm to provide the FSA with more information concerning the trade if required. This reference must be unique within each report file, with the exception that a reference can occur twice if one occurrence is a cancellation. </xs:documentation>
</xs:annotation>
<xs:simpleType>
<xs:restriction base="NonEmptyString">
<xs:maxLength value="25"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="Cancellation" type="xs:boolean" minOccurs="0">
<xs:annotation>
<xs:documentation>Indicates if the transaction is a cancellation. If ommitted this is logically the same as providing a value of 'false'.</xs:documentation>
</xs:annotation>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="FSAFeedHeader">
<xs:annotation>
<xs:documentation>Defines the report header, common to all reports.</xs:documentation>
</xs:annotation>
<xs:complexType>
<xs:sequence>
<xs:element name="FeedTargetSchemaVersion" type="xs:string">
<xs:annotation>
<xs:documentation>Identifies the version of the business specific schema to which this report conforms.</xs:documentation>
</xs:annotation>
</xs:element>
<xs:element name="Submitter">
<xs:complexType>
<xs:sequence>
<xs:element name="SubmittingFirm" type="FSARefType">
<xs:annotation>
<xs:documentation>The FSA code of the firm submitting the report.</xs:documentation>
</xs:annotation>
</xs:element>
<xs:element name="SubmittingDept" minOccurs="0">
<xs:annotation>
<xs:documentation>The identifier of the department within the submitting firm who created this report - optional.</xs:documentation>
</xs:annotation>
<xs:simpleType>
<xs:restriction base="NonEmptyString">
<xs:maxLength value="20"/>
<xs:pattern value="[a-zA-Z0-9]+"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="ReportDetails">
<xs:complexType>
<xs:sequence>
<xs:element name="ReportCreationDate" type="xs:date">
<xs:annotation>
<xs:documentation>Report creation date.</xs:documentation>
</xs:annotation>
</xs:element>
<xs:element name="ReportIdentifier">
<xs:annotation>
<xs:documentation>Unique identifier for the report (wthin the context of the submitter, submitting firm, submitting department and report creation date).</xs:documentation>
</xs:annotation>
<xs:simpleType>
<xs:restriction base="NonEmptyString">
<xs:maxLength value="25"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
Resources:
https://regdata.fca.org.uk/specifications/DRG/PSD002/v1.5/FSAHSFFeedRI-v1-3.xsd - Full Schema
https://regdata.fca.org.uk/specifications/DRG/PSD-CommonTypes/v1.2/FSAFeedCommon-v1-2.xsd - Schema referenced using a dead link
https://regdata.fca.org.uk/#/layout/resources - General Resources
I'm not sure what your question is exactly. You point to 2 problems:
"denormalized data error": what do you mean? how is this related to the schema?
The broken link in the main schema file. You solved it yourself by editing the schemaLocation in the main schema. If you need a way to automatically correct the location without editing the main schema file, you can use an XML Catalog. Is it what you're looking for?
What do you mean also by "importing" the schema? Are you importing it in your own schema?
A catalog would look like this:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE catalog PUBLIC "-//OASIS//DTD XML Catalogs V1.1//EN" "http://www.oasis-open.org/committees/entity/release/1.1/catalog.dtd">
<catalog xmlns="urn:oasis:names:tc:entity:xmlns:xml:catalog">
<uri name="http://www.fsa.gov.uk/XMLSchema/FSAFeedCommon-v1-2"
uri="path/to/local/copy/of/FSAFeedCommon-v1-2.xsd"/>
</catalog>
Then, you need to configure your parser (validation tool) to use that catalog.

xml: using conditioned attributes

I'm looking for a way to realize a scheme, where an element does have an attribute "topic" only when its enumeration has a specific value like in the upcoming example:
<article>
<type>Blogpost</type>
</article>
<article>
<type topic='news'>Article</type>
</article>
<article>
<type>Comment</type>
</article>
The challenge is to make it that the attribute will only be used when the value "Article" or "Documentary" is chosen. Furthermore I want only enumerations from predefined list to be valid. In case the right enumeration is chosen I want the attribute to be required in all other cases I don't want to have the attribute at all.
So far I tried the following schemes in order to do so but it doesn't work at all:
<xs:element name="type" minOccurs="0">
<xs:alternative test="#name='type'" type="type1"/>
<xs:alternative test="#name='type'" type="type2"/>
<xs:alternative type="xs:error"/>
</xs:element>
(...)
<xs:simpleType name="type1">
<xs:restriction base="xs:string">
<xs:enumeration value="Blogpost"/>
<xs:enumeration value="Comment"/>
<xs:enumeration value="Sentence"/>
</xs:restriction>
</xs:simpleType>
<xs:complexType name="type2">
<xs:simpleContent>
<xs:extension base="type1">
<xs:attribute name="topic" type="topics" use='required'>
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="Article"/>
<xs:enumeration value="Documentary"/>
</xs:restriction>
</xs:simpleType>
</xs:attribute>
</xs:extension>
</xs:simpleContent>
</xs:complexType-->
I always get an error that any chosen element is 'not a valid value of "error"'. The other example I tried worked to make use the attribute however it allowed any text and didn't care about the enumeration restrictions making it basicly useless:
<xs:element name="type" minOccurs="0">
<xs:alternative test="#ItemType='type'" type="type1"/>
<xs:alternative test="#ItemType='type'" type="type2"/>
</xs:element>
(...)
How do I do it that the same element does only have the attribute when it is actually the right enumeration and still the restrictions to enumerations are valid? Is my approach to work with xs:alternative the right one in order to have a same element one time with and one time without an attribute?
I was not able to figure out right test conditions in order to work with xs:assert.
I now have a useful answer - sorry for the non-useful comments!
This question is almost identical yours: XSD 1.1 alternative test the contents of text()
. On that basis, I suggest that you try something like this (not tested, because I don't have a XSD1.1 processor)
<xs:element name="type" type="type2" minOccurs="0"/>
<xs:complexType name="type2">
<xs:simpleContent>
<xs:extension base="type1">
<xs:attribute name="topic" type="topics" use='required'/>
<xs:assert test="not ( #topic or ( #topic and ($value eq 'Article' or $value eq 'Documentary'))"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
<xs:simpleType name="type1">
<xs:restriction base="xs:string">
<xs:enumeration value="Article"/>
<xs:enumeration value="Documentary"/>
<xs:enumeration value="Blogpost"/>
<xs:enumeration value="Comment"/>
<xs:enumeration value="Sentence"/>
</xs:restriction>
</xs:simpleType>```
Thank you again for your approach #kimbert.
With your help I was able to figure out the assertion that is fullfilling the demand. I just had to adjust your idea slightly:
<xs:element name="type" type="type2" minOccurs="0"/>
<xs:complexType name="type2">
<xs:simpleContent>
<xs:extension base="type1">
<xs:attribute name="topic" type="topics"/>
<xs:assert test="#topic and ($value eq 'Article' or $value eq 'Documentary') or not ($value eq 'Article' or $value eq 'Documentary')"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
<xs:simpleType name="type1">
<xs:restriction base="xs:string">
<xs:enumeration value="Article"/>
<xs:enumeration value="Documentary"/>
<xs:enumeration value="Blogpost"/>
<xs:enumeration value="Comment"/>
<xs:enumeration value="Sentence"/>
</xs:restriction>
</xs:simpleType>
Like this the compiler now always demands the attribute "topic" in case the value "Article" or "Documentary" is asked for. In all other cases the attribute is ignored as it should be.

Extract enumerations with documentation from XSD file in Python

I'm trying to write a function to get the description of some values from a XSD file, with a structure like this
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" attributeFormDefault="unqualified" elementFormDefault="qualified">
<xs:element name="File">
<xs:annotation>
<xs:documentation>List of centers</xs:documentation>
</xs:annotation>
<xs:complexType>
<xs:sequence>
<xs:element maxOccurs="unbounded" name="Register">
<xs:annotation>
<xs:documentation>Center list registers</xs:documentation>
</xs:annotation>
<xs:complexType>
<xs:sequence>
<xs:element name="CenterType">
<xs:annotation>
<xs:documentation>Type of center </xs:documentation>
</xs:annotation>
<xs:simpleType>
<xs:restriction base="xs:int">
<xs:totalDigits value="1"/>
<xs:enumeration value="1">
<xs:annotation>
<xs:documentation>Own center</xs:documentation>
</xs:annotation>
</xs:enumeration>
<xs:enumeration value="2">
<xs:annotation>
<xs:documentation>External center</xs:documentation>
</xs:annotation>
</xs:enumeration>
<xs:enumeration value="3">
<xs:annotation>
<xs:documentation>Associated center</xs:documentation>
</xs:annotation>
</xs:enumeration>
<xs:enumeration value="4">
<xs:annotation>
<xs:documentation>Other</xs:documentation>
</xs:annotation>
</xs:enumeration>
</xs:restriction>
</xs:simpleType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
By example, if I put
get_value("CenterType", "1")
my function might return "Own center"
I'm using Python 3.8 with XMLSchema.
I wrote this snippet, and I get to print the tag of all elements
xsd_xml = xmlschema.XMLSchema(xsd_file)
fichero = xsd_xml.elements["File"][0]
for elem in fichero:
print(elem.tag)
But I need to access to the enumeration and documentation fields.
How can I extract this data?
Use untangle
import untangle
xsd_file = "C:\\code\\python\\xsd\\test.xsd"
obj = untangle.parse(xsd_file)
res = obj.xs_schema.xs_element.xs_complexType.xs_sequence.xs_element.xs_complexType.xs_sequence.xs_element.xs_simpleType.xs_restriction.xs_enumeration
Finally, I solved my problem using LXML and XMLSchema namespace
def get_value(self, field: str, code: str, file: str):
desc = ""
xsd_xml = ET.parse(file)
search_elem = f".//{{http://www.w3.org/2001/XMLSchema}}element[#name='{field}']"
element = xsd_xml.find(search_elem)
search_enum = f".//{{http://www.w3.org/2001/XMLSchema}}enumeration[#value='{code}']"
enumeration = element.find(search_enum)
if enumeration is not None:
documentation = enumeration.find(".//{http://www.w3.org/2001/XMLSchema}documentation")
desc = documentation.text
else:
desc = "N/A"
return desc

JAXB: xjc and fixed values for attributes of derived types

I'm trying to create an xsd with a type hierarchy.
What I'm hoping to archieve is that the subtypes fill the base type's attributes with a certain value.
This is my best result:
<xs:complexType name="base_type">
<xs:attribute name="att" use="required" type="xs:string" />
</xs:complexType>
<xs:complexType name="type1_t">
<xs:complexContent>
<xs:restriction base="base_type">
<xs:attribute name="att" fixed="value1" use="required" type="xs:string" />
</xs:restriction>
</xs:complexContent>
</xs:complexType>
<xs:complexType name="type2_t">
<xs:complexContent>
<xs:restriction base="base_type">
<xs:attribute name="att" fixed="value2" use="required" type="xs:string" />
</xs:restriction>
</xs:complexContent>
</xs:complexType>
The "att" attribute is made available by base_type, and in elements of type1_t and type2_t, I'd like them set to "value1" and "value2", respectively.
The container element looks like this:
<xs:element name="root">
<xs:complexType>
<xs:choice maxOccurs="unbounded">
<xs:element name="type1" type="type1_t" />
<xs:element name="type2" type="type2_t" />
</xs:choice>
</xs:complexType>
</xs:element>
Now I'm running xjc to generate Java classes for this code, but the "fixed" attribute is not represented in what I get in the Type1T.java, which consists only of
#XmlAccessorType(XmlAccessType.FIELD)
#XmlType(name = "type1_t")
public class Type1T extends BaseType { }
I would have expected
{
#Override public String getAtt() { return "value1"; }
}
?
That is (almost) what gets generated when the type is generated by itself (ie not as an extension of a base type).
I'm I doing something wrong? Is what I'm trying to do just not possible?
My goal is to automatically generate all java code from xsd; otherwise I suppose a way to implement this would be to code the fixed values in the created ObjectFactory methods for each class manually. However, then I would need to prevent ObjectFactory from getting generated every time...
Any hints? Tips? Thoughts?

How make univoque my enumeration by xs:unique

Can anyone point me as to why the unique element in my XSD is not forcing unique-ness? This should throw an error because the last ScreenResult element does not contain a unique value for the Type attribute. I should also note that I'm truly after forcing one of each Type within ScreenResults (ScreenResult is required to exist 3 times, there are 3 types of screens and I am requiring unique-ness) so if there is a better way to accomplish that, I'm all for that as well.
Thank you.
Here is my XML snippet:
<ScreenResults>
<ScreenResult Type="Screen Type A">1</ScreenResult>
<ScreenResult Type="Screen Type B">1</ScreenResult>
<ScreenResult Type="Screen Type B">2</ScreenResult>
</ScreenResults>
Here is my XSD snippet (also note that my original XSD snippets span multiple files but I have verified all of my namespaces are correct):
<xs:element name="ScreenResults" type="import:ScreenResults" minOccurs="0" maxOccurs="1">
<xs:unique name="UniqueScreenResults">
<xs:selector xpath="ScreenResult" />
<xs:field xpath="#Type" />
</xs:unique>
</xs:element>
<!--============ ScreenResults =============-->
<xs:complexType name="ScreenResults">
<xs:sequence minOccurs="1" maxOccurs="1">
<xs:element name="ScreenResult" minOccurs="3" maxOccurs="3">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="enum:ScreenResult">
<xs:attribute name="Type" type="enum:ScreenType" use="required" />
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
<!--============= ScreenType =============-->
<xs:simpleType name="ScreenType">
<xs:restriction base='xs:token'>
<xs:enumeration value='Screen Type A' >
<xs:annotation>
<xs:documentation>1</xs:documentation>
</xs:annotation>
</xs:enumeration>
<xs:enumeration value='Screen Type B' >
<xs:annotation>
<xs:documentation>2</xs:documentation>
</xs:annotation>
</xs:enumeration>
<xs:enumeration value='Screen Type C' >
<xs:annotation>
<xs:documentation>3</xs:documentation>
</xs:annotation>
</xs:enumeration>
</xs:restriction>
</xs:simpleType>
<!--============ ScreenResult ============-->
<xs:simpleType name="ScreenResult">
<xs:restriction base='xs:token'>
<xs:enumeration value='1' >
<xs:annotation>
<xs:documentation>Positive</xs:documentation>
</xs:annotation>
</xs:enumeration>
<xs:enumeration value='2' >
<xs:annotation>
<xs:documentation>Negative</xs:documentation>
</xs:annotation>
</xs:enumeration>
<xs:enumeration value='3' >
<xs:annotation>
<xs:documentation>Not administered</xs:documentation>
</xs:annotation>
</xs:enumeration>
</xs:restriction>
</xs:simpleType>
I'm posting my solution for anyone else who runs into this problem.
Although I had asserted that all of my namespaces were correct, that was, of course the problem. All of the namespaces were correct except on the unique element itself.
I erroneously assumed that the unique element would not need to prefix a namespace as it was within the context. But that is not the case. Since I did declare a default namespace for the file, I still needed the prefix.
So my only change, and the solution, is as follows:
<xs:element name="ScreenResults" type="import:ScreenResults" minOccurs="0" maxOccurs="1">
<xs:unique name="UniqueScreenResults">
<xs:selector xpath="import:ScreenResult" />
<xs:field xpath="#Type" />
</xs:unique>
</xs:element>

Resources