xml - 是否可以在assert和xpath的帮助下使用xsd 1.1检查或验证xml节点的值?

标签 xml validation xpath xsd assert

我有一个通用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/

相关文章:

javascript - 动态单选组的 jquery 表单验证

java - yyyy-mm-dd 的正则表达式日期验证

xml - 使用 XSLT 查找给定一组参数的节点

python - 如何使用 python 识别元素并通过 selenium 调用 send_keys

python - 检测空 XML 根元素

java - 使用java在xml中添加自定义声明

java - 我可以在没有明确提供 XSL 文件的情况下进行 XSL 转换吗?

c# - 在离开页面之前检查字段数据是否更改

javascript - 使用 XPath 查找节点后面的文本节点

具有属性的元素中的 xml 模式嵌套元素