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

含命名空间的XML校验XSD报错,求XSD修改指导

解决XML命名空间导致的XSD校验失败问题

问题根源

你的XML使用了默认命名空间http://www.sample.com/itemfile,但原XSD未指定对应目标命名空间,也未配置元素命名空间限定规则,导致XSD中的pos_integer_type类型无法被带命名空间的XML识别,从而抛出解析错误。

修改后的可用XSD

<xs:schema 
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
  targetNamespace="http://www.sample.com/itemfile"
  xmlns="http://www.sample.com/itemfile"
  elementFormDefault="qualified">

  <xs:simpleType name="pos_integer_type">
    <xs:restriction base="xs:integer">
      <xs:minInclusive value="1"/>
    </xs:restriction>
  </xs:simpleType>

  <xs:element name="item">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="id" type="pos_integer_type"/>
        <xs:element name="quantity" type="pos_integer_type"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

关键修改说明

  • targetNamespace="http://www.sample.com/itemfile":指定XSD的目标命名空间,与XML的默认命名空间完全一致,实现两者命名空间匹配。
  • xmlns="http://www.sample.com/itemfile":给XSD设置默认命名空间,使XSD内部引用自身定义的pos_integer_type时无需额外添加前缀。
  • elementFormDefault="qualified":强制要求XML中所有属于目标命名空间的元素都被命名空间限定(XML用默认命名空间时,元素自动归属该命名空间),确保XSD能正确识别XML元素。

修改后无需调整XML内容,即可正常完成校验。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.22 04:34:52