[英]Defining Attributes as Mandatory in the XSD
需要幫助將屬性定義為 XSD 中的必填字段,以便它有助於錯誤處理部分引發異常,
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>
上述 XSD 的分層表示如下所示,
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>
我能夠將LinePaymentAmount定義為強制性元素並且工作正常(在缺少字段值期間拋出錯誤並針對缺少的字段值引發異常),但我無法將屬性定義為強制性元素。 我已經為強制屬性定義了use=required ,但它不起作用,我還嘗試了博客“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 ” 但仍然無法正常工作。
你能幫忙告訴我如何在 XSD 中定義強制屬性字段嗎?
目前尚不清楚您的示例輸入的外觀以及您使用的解析器以及它是如何失敗的,但肯定是當我使用您發布的模式時,讓例如 oXygen XML 編輯器生成一個示例文件並刪除一個或兩個必需的屬性以獲得例如
<?xml version="1.0" encoding="UTF-8"?>
<Req_Pay xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="sample1.xsd">
<Package PackagePaymentAmount="PackagePaymentAmount0" PackagePaymentQuantity="PackagePaymentQuantity0">
<Payment PayId="PayId0" Cur="Cur0" Memo="Memo0">
<Line Agg="Agg0" ConNum="ConNum0">
<LinePaymentAmount>LinePaymentAmount0</LinePaymentAmount>
</Line>
<Line Agg="Agg1" ConNum="ConNum1">
<LinePaymentAmount>LinePaymentAmount1</LinePaymentAmount>
</Line>
</Payment>
<Payment PayId="PayId1" Cur="Cur1" Memo="Memo1">
<Line Agg="Agg2">
<LinePaymentAmount>LinePaymentAmount2</LinePaymentAmount>
</Line>
<Line Agg="Agg3" ConNum="ConNum3">
<LinePaymentAmount>LinePaymentAmount3</LinePaymentAmount>
</Line>
</Payment>
</Package>
<Package PackagePaymentAmount="PackagePaymentAmount1" PackagePaymentQuantity="PackagePaymentQuantity1">
<Payment PayId="PayId2" Cur="Cur2" Memo="Memo2">
<Line Agg="Agg4" ConNum="ConNum4">
<LinePaymentAmount>LinePaymentAmount4</LinePaymentAmount>
</Line>
<Line
ConNum="ConNum5">
<LinePaymentAmount>LinePaymentAmount5</LinePaymentAmount>
</Line>
</Payment>
<Payment PayId="PayId3" Cur="Cur3" Memo="Memo3">
<Line Agg="Agg6" ConNum="ConNum6">
<LinePaymentAmount>LinePaymentAmount6</LinePaymentAmount>
</Line>
<Line Agg="Agg7" ConNum="ConNum7">
<LinePaymentAmount>LinePaymentAmount7</LinePaymentAmount>
</Line>
</Payment>
</Package>
</Req_Pay>
我收到兩個驗證錯誤
Description: Attribute 'ConNum' must appear on element 'Line'.
Start location: 14:14
End location: 14:18
Description: Attribute 'Agg' must appear on element 'Line'.
Start location: 27:14
End location: 27:18
來自 oXygen 用作默認驗證解析器的 Apache Xerces。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.