【问题标题】:Is it possible to check or validate the value of an xml node using xsd 1.1, with the help of assert and xpath?是否可以在 assert 和 xpath 的帮助下使用 xsd 1.1 检查或验证 xml 节点的值?
【发布时间】:2017-09-12 16:18:16
【问题描述】:

我有一个需要使用 XSD 架构进行验证的通用 XML。 很遗憾,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:日期时间。 这同样适用于其他元素。

所以目标是检查某个 id 只有 one 类型并且只有 one xs:simpleType 是有效的。

使用我当前的 xs:assert 表达式,我只能检查 Prop 元素是否具有特定的 id 和 type 属性。但我还需要检查值。

谢谢,乔

【问题讨论】:

    标签: xml validation xpath xsd assert


    【解决方案1】:

    您没有明确说明您的问题是什么。我假设问题是“我如何检查值的类型是否正确?”

    一种方法是在一般形式的 Prop 声明中添加一个附加断言

    if (@type = 'DATETIME') then . castable as xs:dateTime
    else ...
    

    也许更简单的方法是使用条件类型分配来为 Prop 分配适当的类型。

    【讨论】:

      猜你喜欢
      • 2013-11-17
      • 1970-01-01
      • 2021-06-14
      • 1970-01-01
      • 2012-06-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-13
      相关资源
      最近更新 更多