【问题标题】:type checking based no attribute's name in XML/XSD基于 XML/XSD 中无属性名称的类型检查
【发布时间】:2022-06-20 15:29:33
【问题描述】:

我必须编写一个 XSD 来根据元素的名称来验证元素的类型。我已经尝试过条件类型赋值,但我无法验证元素的值。提前感谢您的帮助。

我的 XSD

<xs:element name="PARAMS">
 <xs:complexType>
  <xs:sequence>
   <xs:element name="param" maxOccurs="unbounded" minOccurs="0">
     <xs:alternative test="@name='Cste'" type="float"/>
     <xs:alternative test="@name='Unité'" type="string"/>
   </xs:element>
  </xs:sequence>
 </xs:complexType>
</xs:element>

我的 XML

<PARAMS>
    <param name="Cste" type="Real" libelle="Constante">0.200000</param>
    <param name="Unit" type="String" libelle="Unité">bara</param>
</PARAMS>

【问题讨论】:

    标签: xml xsd


    【解决方案1】:

    你说对了,但是

    (a) 确保架构处理器支持 XSD 1.1(很多不支持)

    (b) 你对 param 的元素声明需要允许三个属性(name、type、libelle)

    (c) 类型必须是带有命名空间前缀的 xs:float 和 xs:string。

    如果您遇到错误,请告诉我们错误是什么。如果我们知道症状,我们总是可以更准确地诊断问题。

    【讨论】:

    • 我正在尝试在 python 上使用 xmlschema(它支持 xsd 1.1)并且我已经编辑了我的代码并添加了元素声明。我将在执行验证后立即与您分享结果。谢谢你的回答!
    【解决方案2】:

    所以我一直在尝试使用我更新的 XSD 来验证我的 XML。而且我认为我对类型分配的替代方法的使用是错误的。 这是我的更新的 XSD

    <xs:element name="PARAMS">
      <xs:complexType>
        <xs:sequence>
          <xs:element name="param" maxOccurs="unbounded">
            <xs:alternative test="@name='Commentaire'" type="xs:string" />                                                                             
            <xs:alternative test="@name='Commentaire2'" type="xs:string" />                                                                            
            <xs:alternative test="@name='SE'" type="xs:integer" /> 
            <xs:alternative test="@name='number'" type="xs:float" />                                                                              
          </xs:element> 
            <xs:attribute name="name" type="xs:string" />  
            <xs:attribute name="type" type="xs:string" /> 
            <xs:attribute name="libelle" type="xs:string" />   
        </xs:sequence>
      </xs:complexType>
    </xs:element>
    

    我在支持 XSD 1.1 的 python 上使用 xmlschema 但我收到此错误

    xmlschema.validators.exceptions.XMLSchemaParseError: Unexpected child with tag 'xs:attribute' at position 2:
    

    我认为属性的声明是在错误的级别上进行的。我需要帮助!

    【讨论】:

      猜你喜欢
      • 2011-05-14
      • 1970-01-01
      • 2017-04-03
      • 1970-01-01
      • 1970-01-01
      • 2016-07-29
      • 2017-12-07
      • 2021-03-09
      • 1970-01-01
      相关资源
      最近更新 更多