XSD 耦合两个 XML 元素的值?
XSD to couple values of two XML elements?
我的程序可以 return 2 个 XML,例如:
<RESPONSE>
<ERROR_ID>1</ERROR_ID>
<ERROR_MESSAGE>Parse error</ERROR_MESSAGE>
</RESPONSE>
<RESPONSE>
<ERROR_ID>2</ERROR_ID>
<ERROR_MESSAGE>Unexpected attribute</ERROR_MESSAGE>
</RESPONSE>
我尝试编写一些 XSD 文件来验证它们是否正常。这是我最终得到的:
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="qualified">
<xsd:element name="RESPONSE" type="Response"/>
<xsd:complexType name="Response">
<xsd:all>
<xsd:element name="ERROR_ID" type="ErrorId"/>
<xsd:element name="ERROR_MESSAGE" type="ErrorMessage"/>
</xsd:all>
</xsd:complexType>
<xsd:simpleType name="ErrorId">
<xsd:restriction base="xsd:positiveInteger">
<xsd:enumeration value="1"/>
<xsd:enumeration value="2"/>
</xsd:restriction>
</xsd:simpleType>
<xsd:simpleType name="ErrorMessage">
<xsd:restriction base="xsd:string">
<xsd:enumeration value="Parse error"/>
<xsd:enumeration value="Unexpected attribute"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:schema>
它验证正常,但我在想我是否可以 link 设置错误 ID 和错误消息以不让错误 id=2 与错误 id=1 的消息一起通过验证文件:
<RESPONSE>
<ERROR_ID>2</ERROR_ID>
<ERROR_MESSAGE>Parse error</ERROR_MESSAGE>
</RESPONSE>
有什么好的方法吗?我的程序 returns 当然有更多的错误 ID 和消息。
也许更好的问题是我是否应该期待 XSD 的这种验证?
XSD 1.0 无法根据另一个元素的值来约束一个元素的值。
XSD 1.1 可以使用 xsd:assert
做到这一点,但是您应该重新考虑您的设计...
备选设计建议:
不要检查 XSD 中的 ERROR_ID
-ERROR_MESSAGE
配对。
将错误 ID 与错误消息连接起来:
<Error>1. Parse error</Error>
针对更具体的错误元素名称使用固定属性:
<ParseError id="1" message="Parse error"/>
<UnexpectedAttributeError id="2" message="Unexpected attribute"/>
None 这些替代设计需要 XSD 1.1.
我的程序可以 return 2 个 XML,例如:
<RESPONSE>
<ERROR_ID>1</ERROR_ID>
<ERROR_MESSAGE>Parse error</ERROR_MESSAGE>
</RESPONSE>
<RESPONSE>
<ERROR_ID>2</ERROR_ID>
<ERROR_MESSAGE>Unexpected attribute</ERROR_MESSAGE>
</RESPONSE>
我尝试编写一些 XSD 文件来验证它们是否正常。这是我最终得到的:
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="qualified">
<xsd:element name="RESPONSE" type="Response"/>
<xsd:complexType name="Response">
<xsd:all>
<xsd:element name="ERROR_ID" type="ErrorId"/>
<xsd:element name="ERROR_MESSAGE" type="ErrorMessage"/>
</xsd:all>
</xsd:complexType>
<xsd:simpleType name="ErrorId">
<xsd:restriction base="xsd:positiveInteger">
<xsd:enumeration value="1"/>
<xsd:enumeration value="2"/>
</xsd:restriction>
</xsd:simpleType>
<xsd:simpleType name="ErrorMessage">
<xsd:restriction base="xsd:string">
<xsd:enumeration value="Parse error"/>
<xsd:enumeration value="Unexpected attribute"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:schema>
它验证正常,但我在想我是否可以 link 设置错误 ID 和错误消息以不让错误 id=2 与错误 id=1 的消息一起通过验证文件:
<RESPONSE>
<ERROR_ID>2</ERROR_ID>
<ERROR_MESSAGE>Parse error</ERROR_MESSAGE>
</RESPONSE>
有什么好的方法吗?我的程序 returns 当然有更多的错误 ID 和消息。
也许更好的问题是我是否应该期待 XSD 的这种验证?
XSD 1.0 无法根据另一个元素的值来约束一个元素的值。
XSD 1.1 可以使用 xsd:assert
做到这一点,但是您应该重新考虑您的设计...
备选设计建议:
不要检查 XSD 中的
ERROR_ID
-ERROR_MESSAGE
配对。将错误 ID 与错误消息连接起来:
<Error>1. Parse error</Error>
针对更具体的错误元素名称使用固定属性:
<ParseError id="1" message="Parse error"/> <UnexpectedAttributeError id="2" message="Unexpected attribute"/>
None 这些替代设计需要 XSD 1.1.