Need help on defining attribute as mandatory field in the XSD so that it will help for the Error handling part to raise the exception,
XSD:
===
<?xml version="1.0" encoding="utf-8"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="Req_Pay">
<xs:complexType>
<xs:sequence>
<xs:element name="Package" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="Payment" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="Line" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="LinePaymentAmount" minOccurs="1" nillable="false">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:minLength value="1"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
</xs:sequence>
<xs:attribute name="Agg" type="xs:string" use="required" />
<xs:attribute name="ConNum" type="xs:string" use="required" />
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="PayId" type="xs:string" use="required" />
<xs:attribute name="Cur" type="xs:string" use="required" />
<xs:attribute name="Memo" type="xs:string" use="required" />
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="PackagePaymentAmount" type="xs:string" use="required" />
<xs:attribute name="PackagePaymentQuantity" type="xs:string" use="required" />
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
The Hierarchical representation of the above XSD can be seen as below,
Req_Pay (Element)
- Package (Element)
- PackagePaymentAmount (attribute 1)
- PackagePaymentQuantity (attribute 2) **(Mandatory Field)**
- Payment (Element)
- PayId (attribute 1)
- Cur (attribute 2) **(Mandatory Field)**
- Memo (attribute 3) **(Mandatory Field)**
- Line (Element)
- LinePaymentAmount (Element) **(Mandatory Field)**
- Agg (attribute 1)
- ConNum (attribute 2) **(Mandatory Field)**
**Please find the below input data which will be passed to the above created XSD**,
<?xml version="1.0" encoding="UTF-8"?>
<Req_Pay>
<Package PackagePaymentAmount="452.88" PackagePaymentQuantity="200.00">
<Payment PayId="1000000-0001" Cur="USD" Memo="AK005-18308-20222">
<Line Agg="yes" ConNum="MED" >
<LinePaymentAmount>226.4400</LinePaymentAmount>
</Line>
<Line Agg="yes" ConNum="FFS" >
<LinePaymentAmount>226.4400</LinePaymentAmount>
</Line>
</Payment>
</Package>
</Req_Pay>
I am able to define LinePaymentAmount as mandatory element and is working perfectly (during missing field value the error is thrown and exception is raised for the missing field value), but I am not able to define Attributes as mandatory. I have already defined use=required for the mandatory attributes, but it is not working and I also tried the blog "https://stackoverflow.com/questions/7690949/element-mandatory-attribute-declaration-in-xsd-schema#:~:text=To%20mark%20an%20attribute%20as,simpleType%20%2F%3E%20and%20use%20that.&text=I%20am%20not%20certain%20what,that%20is%20not%20yet%20known." but still its not working.
Can you please help to let me know how to define mandatory attributes fields in the XSD.
It is not clear how your sample input looks and which parser you use and how it fails but certainly when I use your schema as posted, let e.g. oXygen XML editor generate a sample file and remove one or two of the required attributes to get e.g.
I get two validation errors
from Apache Xerces that oXygen uses as the default validating parser.