根据 XSD 中的另一个字段值定义必填字段
Define require fields based on other field value in XSD
我有一个要求,我试图根据 XSD 中的其他字段值定义必填字段 (minOccurs=1),但我无法这样做。
我想举个简单的例子来更了解我的问题。请参考下面两个简单的 xml,其中 state
是定义发生了什么事件的字段。如果 state
字段值为 PROCESS_END
,我需要将 processingTime
和 processResult
定义为 XSD 文件中的必填字段,因为进程启动时不需要这些字段.这只是说明我的问题的简单示例。提前致谢。
<?xml version = "1.0"?>
<metadata>
<title>The processing started event</title>
<state>PROCESS_START</state>
</metadata>
<?xml version = "1.0"?>
<metadata>
<title>The processing ended event</title>
<state>PROCESS_END</state>
<processingTime>100s</processingTime>
<processResult>success</processResult>
</metadata>
在XSD 1.1中,定义processingTime
和processResult
作为可选元素,在metadata
的complexType上,定义<xs:assert test="if (state='PROCESS_END') then exists(processingTime) and exists(processResult) else true()"/>
.
在 XSD 1.0 中无法完成。许多常用的 XSD 验证器,例如来自 Microsoft 的那个,从未升级到 XSD 1.1.
我有一个要求,我试图根据 XSD 中的其他字段值定义必填字段 (minOccurs=1),但我无法这样做。
我想举个简单的例子来更了解我的问题。请参考下面两个简单的 xml,其中 state
是定义发生了什么事件的字段。如果 state
字段值为 PROCESS_END
,我需要将 processingTime
和 processResult
定义为 XSD 文件中的必填字段,因为进程启动时不需要这些字段.这只是说明我的问题的简单示例。提前致谢。
<?xml version = "1.0"?>
<metadata>
<title>The processing started event</title>
<state>PROCESS_START</state>
</metadata>
<?xml version = "1.0"?>
<metadata>
<title>The processing ended event</title>
<state>PROCESS_END</state>
<processingTime>100s</processingTime>
<processResult>success</processResult>
</metadata>
在XSD 1.1中,定义processingTime
和processResult
作为可选元素,在metadata
的complexType上,定义<xs:assert test="if (state='PROCESS_END') then exists(processingTime) and exists(processResult) else true()"/>
.
在 XSD 1.0 中无法完成。许多常用的 XSD 验证器,例如来自 Microsoft 的那个,从未升级到 XSD 1.1.