You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

关于XSD 1.1中xs:alternative在线验证失败的技术问询

关于XSD 1.1条件类型分配(xs:alternative)的问题

参考条件类型分配方案,使用xs:alternative实现基于属性值的条件类型分配,但多数在线XML/XSD验证器报错:

s4s-elt-invalid-content.1: The content of '#AnonType_IMtypeIMtimehorizonenabler' is invalid. Element 'alternative' is invalid, misplaced, or occurs too often.

已在XSD代码中明确指定版本:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:vc="http://www.w3.org/2007/XMLSchema-versioning"  elementFormDefault="qualified" vc:minVersion="1.1">

提出以下疑问:

  1. 是否存在配置遗漏?
  2. 在线验证器是否不支持vc:minVersion="1.1"规定?

本人的XML文件

<?xml version="1.0" encoding="UTF-8"?>
<enabler>
  <name>my landing gear</name>
  <type> techno design</type>
  <subtype>landing gear</subtype>
  <timehorizon>
    <year>2030</year>
    <trl>5</trl>
    <ttm>P24M</ttm>
    <IM>
      <IMname>EpndB</IMname>
      <IMtype type="Metric">
        <Munit>dB</Munit>
        <Mvalue>4.5</Mvalue>
      </IMtype>
    </IM>
    <IM>
      <IMname>Nox</IMname>
      <IMtype type="Metric">
        <Munit>ppm</Munit>
        <Mvalue>4.5</Mvalue>
       </IMtype>
    </IM>
    <IM>
      <IMname>QOL</IMname>
      <IMtype type="Indicator">
      <Idescriptor>ppm</Idescriptor>
      <Ivalue>A+</Ivalue>
        </IMtype>
    </IM>
  </timehorizon>
  <timehorizon>
    <year>2050</year>
    <trl>7</trl>
    <ttm>P12M</ttm>
  </timehorizon>
</enabler>

本人的XSD文件

<?xml version="1.0" encoding="utf-8"?>
<pps:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:pps="http://www.w3.org/2001/XMLSchema" xmlns:vc="http://www.w3.org/2007/XMLSchema-versioning" vc:minVersion="1.1">
    <pps:element name="enabler">
        <pps:complexType>
            <pps:sequence>
                <pps:element name="name" type="pps:string" />
                <pps:element name="type" type="pps:string" />
                <pps:element name="subtype" type="pps:string" />
                <!-- Defining properties for each time horizon -->
                <pps:element name="timehorizon" minOccurs="1" maxOccurs="unbounded">
                    <pps:complexType>
                        <pps:sequence>
                            <pps:element name="year" type="pps:gYear"/>
                            <pps:element name="trl">
                                <pps:simpleType>
                                    <pps:restriction base="pps:integer">
                                        <pps:minInclusive value="1"/>
                                        <pps:maxInclusive value="12"/>
                                    </pps:restriction>
                                </pps:simpleType>
                            </pps:element>
                            <pps:element name="ttm" type="pps:duration"/>
                            <!-- Defining Indicator or metric for each time horizon -->
                            <pps:element name="IM" minOccurs="0" maxOccurs="unbounded">
                                <pps:complexType>
                                    <pps:sequence>
                                        <pps:element name="IMname" type="pps:string"/>
                                        <pps:element name="IMtype" maxOccurs="unbounded">
                                          <pps:complexType>
                                            <pps:alternative test="@type = Metric" type="MetricType"/>
                                            <pps:alternative test="@type = Indicator" type="IndicatorType"/>
                                          </pps:complexType>
                                         </pps:element>
                                  </pps:sequence>
                              </pps:complexType>
                          </pps:element>
                            <pps:complexType name="MetricType">
                                <pps:sequence>
                                    <pps:element name="Munit" type="pps:string"/>
                                    <pps:element name="Mvalue" type="pps:decimal"/>
                                </pps:sequence>
                            </pps:complexType>
                            <pps:complexType name="IndicatorType">
                                <pps:sequence>
                                    <pps:element name="Idescriptor" type="pps:string"/>
                                    <pps:element name="Ivalue" type="pps:string"/>
                                </pps:sequence>
                            </pps:complexType>
                                        </pps:sequence>
                    </pps:complexType>
                </pps:element>
            </pps:sequence>
        </pps:complexType>
    </pps:element>
</pps:schema>

问题解答

1. 存在多处配置遗漏

你的XSD有三个关键错误:

  • xs:alternative放置位置错误:xs:alternative是xs:element的直接子元素,不能嵌套在xs:complexType内部。必须把alternative节点放在IMtype元素定义的层级,而非匿名complexType里。
  • 测试表达式未加引号:test="@type = Metric"中的Metric和Indicator是字符串常量,必须用引号包裹,正确写法为test="@type = 'Metric'"。
  • 自定义类型作用域错误:MetricType和IndicatorType定义在timehorizon的complexType内部,属于局部类型,无法被alternative引用。需要将这两个类型提升为全局类型,直接放在schema节点下。

修复后的完整XSD:

<?xml version="1.0" encoding="utf-8"?>
<pps:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:pps="http://www.w3.org/2001/XMLSchema" xmlns:vc="http://www.w3.org/2007/XMLSchema-versioning" vc:minVersion="1.1">
    <!-- 全局定义条件类型 -->
    <pps:complexType name="MetricType">
        <pps:sequence>
            <pps:element name="Munit" type="pps:string"/>
            <pps:element name="Mvalue" type="pps:decimal"/>
        </pps:sequence>
    </pps:complexType>
    <pps:complexType name="IndicatorType">
        <pps:sequence>
            <pps:element name="Idescriptor" type="pps:string"/>
            <pps:element name="Ivalue" type="pps:string"/>
        </pps:sequence>
    </pps:complexType>

    <pps:element name="enabler">
        <pps:complexType>
            <pps:sequence>
                <pps:element name="name" type="pps:string" />
                <pps:element name="type" type="pps:string" />
                <pps:element name="subtype" type="pps:string" />
                <pps:element name="timehorizon" minOccurs="1" maxOccurs="unbounded">
                    <pps:complexType>
                        <pps:sequence>
                            <pps:element name="year" type="pps:gYear"/>
                            <pps:element name="trl">
                                <pps:simpleType>
                                    <pps:restriction base="pps:integer">
                                        <pps:minInclusive value="1"/>
                                        <pps:maxInclusive value="12"/>
                                    </pps:restriction>
                                </pps:simpleType>
                            </pps:element>
                            <pps:element name="ttm" type="pps:duration"/>
                            <pps:element name="IM" minOccurs="0" maxOccurs="unbounded">
                                <pps:complexType>
                                    <pps:sequence>
                                        <pps:element name="IMname" type="pps:string"/>
                                        <!-- 正确使用xs:alternative -->
                                        <pps:element name="IMtype" maxOccurs="unbounded">
                                            <pps:alternative test="@type = 'Metric'" type="pps:MetricType"/>
                                            <pps:alternative test="@type = 'Indicator'" type="pps:IndicatorType"/>
                                        </pps:element>
                                    </pps:sequence>
                                </pps:complexType>
                            </pps:element>
                        </pps:sequence>
                    </pps:complexType>
                </pps:element>
            </pps:sequence>
        </pps:complexType>
    </pps:element>
</pps:schema>

2. 在线验证器的XSD 1.1支持情况

多数在线验证器默认仅支持XSD 1.0,即使声明vc:minVersion="1.1",也不会自动切换到1.1模式:

  • 常见工具如W3C标记验证服务完全不支持XSD 1.1;
  • 支持XSD 1.1的在线工具较少,可尝试SaxonJS在线验证工具,部分工具需要手动指定验证时使用XSD 1.1版本;
  • 本地工具推荐使用Saxon系列、XMLSpy或Oxygen XML Editor,这些工具完整支持XSD 1.1的所有特性。

内容的提问来源于stack exchange,提问作者Mnatsortats

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.28 05:27:01