我有一个通用XML,需要使用XSD-Schema进行验证。
不幸的是,XML的结构是固定的,不能更改。
XML:
<?xml version="1.0" encoding="UTF-8"?>
<Root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="RatingRequestShema-V2.xsd">
<Request>
<Props id="Info">
<Prop id="A" type="DATETIME">2017-10-01T09:34:11Z</Prop>
<Prop id="B" type="ENUM">Test</Prop>
</Props>
<Props id="Example">
<Prop id="C" type="DATE">1980-08-14</Prop>
<Prop id="D" type="DECIMAL">34,5</Prop>
</Props>
</Request>
</Root>
XSD架构:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:vc="http://www.w3.org/2007/XMLSchema-versioning" elementFormDefault="qualified" attributeFormDefault="unqualified" vc:minVersion="1.1">
<xs:element name="Root">
<xs:complexType>
<xs:sequence>
<xs:element name="Request">
<xs:complexType>
<xs:sequence>
<xs:element name="Props" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="Prop" maxOccurs="unbounded">
<xs:complexType>
<xs:attribute name="id" use="required"/>
<xs:attribute name="type" use="required"/>
<xs:assert test="if (@id eq 'A') then @type eq 'DATETIME'
else if (@id eq 'B') then @type eq 'ENUM' else false()"/>
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="id" use="required">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="Info"/>
<xs:enumeration value="Example"/>
</xs:restriction>
</xs:simpleType>
</xs:attribute>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:simpleType name="datetime">
<xs:restriction base="xs:dateTime"/>
</xs:simpleType>
<xs:simpleType name="B_Enum">
<xs:restriction base="xs:string">
<xs:enumeration value="Test_1"/>
<xs:enumeration value="Test_2"/>
<xs:enumeration value="Test_3"/>
</xs:restriction>
</xs:simpleType>
现在,XSD-Schema仅检查id是否等于“ A”,类型是否需要为“ DATETIME”,还需要检查值“ 2017-10-01T09:34:11Z”是否为xs:dateTime类型。
其他元素也一样。
因此,目标是检查对于某个ID,只有一种类型和一种xs:simpleType有效。
使用当前的xs:assert表达式,我只能检查Prop元素是否具有特定的id和type属性。但是我还需要检查该值。
谢谢乔
最佳答案
您没有明确地说出您的问题是什么。我假设问题是“如何检查值的类型正确?”
一种方法是在一般形式的Prop声明中附加一个断言
if (@type = 'DATETIME') then . castable as xs:dateTime
else ...
可能更简单的方法是使用条件类型分配将适当的类型分配给Prop。
关于xml - 是否可以在assert和xpath的帮助下使用xsd 1.1检查或验证xml节点的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46181265/