1

我有这个 XML 文件:

<?xml version="1.0" encoding="UTF-8"?>
<vehicle_list>
    <vehicle id="v001">
        <type registration="ABC-XYZ" year="2010">car</type>
        <spent_fuel>8</spent_fuel>
        <constructor>XPTO</constructor>
    </vehicle>  
    <vehicle id="v002">
        <type>bike</type>
        <spent_fuel>0</spent_fuel>
        <constructor>XIMA</constructor>
    </vehicle>
    <vehicle id="v003">
        <type>boat</type>
        <spent_fuel>40</spent_fuel>
        <constructor>AnyBoat</constructor>
    </vehicle>
</vehicle_list>

现在我需要(出于学习目的)制作一个 XSD,在其中我使用枚举来限制车辆的类型(汽车、自行车和船)并定义规则,如果类型是“汽车”,那么我需要有两个属性:注册和年份。

我应该如何做到这一点?

我一直在阅读,但找不到任何可以帮助我找到解决方案的东西。

我仅限于 XSD 1.0(不是 XSD 1.1)。

4

1 回答 1

1

XSD 1.0 不能表达基于元素值的约束。为此,您需要 XSD 1.1 断言,并且您在评论中声明您只能使用 XSD 1.0。

但是,实际上,如果您修复了 XML 设计,就不需要 XSD 1.1。vehicle与其拥有需要通过type元素进一步规范的泛型,不如将type元素值提升为适当的元素本身。

以机智:

<?xml version="1.0" encoding="UTF-8"?>
<vehicle_list>
    <car id="v001" registration="ABC-XYZ" year="2010">
        <spent_fuel>8</spent_fuel>
        <constructor>XPTO</constructor>
    </car>  
    <bike id="v002">
        <spent_fuel>0</spent_fuel>
        <constructor>XIMA</constructor>
    </bike>
    <boat id="v003">
        <spent_fuel>40</spent_fuel>
        <constructor>AnyBoat</constructor>
    </boat>
</vehicle_list>

然后为这个 XML 设计编写一个 XSD 1.0 就很简单了。

于 2016-05-22T01:08:05.463 回答