提问人:Nikita 提问时间:12/18/2022 最后编辑:Nikita 更新时间:12/18/2022 访问量:83
在 XSD 中将属性定义为必需属性
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 中定义必填属性字段。
答:
0赞
Martin Honnen
12/18/2022
#1
目前尚不清楚您的示例输入的外观、您使用的解析器以及它是如何失败的,但可以肯定的是,当我使用您的模式时,例如 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
来自 Apache Xerces,oXygen 将其用作默认验证解析器。
评论
0赞
Nikita
12/18/2022
嗨,马丁,非常感谢您的回复。我已针对 XSD 添加了输入 XML。我们将收到该格式的 xsd
1赞
Martin Honnen
12/18/2022
@Nikita,显示的XML示例似乎完全有效,具有所有必需的属性。
0赞
Nikita
12/18/2022
嗨,马丁,是的,但是如果传入有效负载中的强制属性没有值,那么它应该引发异常。如前所述,我能够为强制性元素文件 LinePaymentAmount 实现这一点,为此我定义了结构,但对于属性,我需要定义类似的结构,但它不起作用。我已经添加了一个 URL,他们在其中定义了属性的结构,我尝试了类似的属性,但它不起作用。
0赞
kjhughes
12/19/2022
@Nikita:发布您认为应该无效但验证与您的预期相反的 XML。你试图描述问题所在的地方的口头尝试太不精确,无法传达问题。
0赞
Martin Honnen
12/19/2022
@Nikita,并说明您使用的哪个工具/哪个解析器无法指示具有必需属性的验证问题。
评论