名称相同但类型不同的 XSD 元素 [英] XSD elements with same name but different types

查看:28
本文介绍了名称相同但类型不同的 XSD 元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要编写一个 XSD 来验证属性以及元素的值.到目前为止,我能够验证属性但不能验证值.提前感谢您的帮助.

I need to write a XSD which will validate the attribute as well as the value of the element. So far I am able to validate the attributes but not the value. Thanks for your help in advance.

我的 XML:

<params>
    <param dataType="java.lang.String">12A</param>
    <param dataType="java.lang.Integer">6455</param>
    <param dataType="oracle.jbo.domain.Date">2014-10-01</param>
    <param dataType="oracle.jbo.domain.Date">2018-10-01</param>
    <param dataType="java.lang.String">H1</param>
    <param dataType="java.lang.String">4235AS</param>
    <param dataType="java.lang.String">5aZ</param>
    <param dataType="java.lang.String">q2w</param>
    <param dataType="java.lang.Integer">10</param>
    <param dataType="java.lang.Integer">3</param>
</params>

我的 XSD

<?xml version="1.0"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" 
           xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="params">
    <xs:complexType>
      <xs:sequence>
        <xs:element maxOccurs="10" 
                    name="param" type="paramType" />
      </xs:sequence>
    </xs:complexType>
  </xs:element>

  <xs:complexType name="paramType">
    <xs:simpleContent>
      <xs:extension base="xs:string">
        <xs:attribute name="dataType" type="validAttributeType" use="optional" />
      </xs:extension>
    </xs:simpleContent>
  </xs:complexType>

  <xs:simpleType name="validAttributeType">
    <xs:restriction base="xs:string">
      <xs:enumeration value="java.lang.String" />
      <xs:enumeration value="java.lang.Integer" />
      <xs:enumeration value="oracle.jbo.domain.Date" />

    </xs:restriction>
  </xs:simpleType>
</xs:schema>

所以我的需要是,如果 dataType 说 Integer 它应该只接受数字作为值,或者如果它说日期,那么值应该是日期而不是其他任何东西.

So my need is, if the dataType says Integer it should only accept numbers as value or if it says date, then the value should be date not anything else.

推荐答案

XSD 1.0

不能在 XSD 1.0 中进行基于属性值的类型检查.

XSD 1.0

Type checking based on an attribute value cannot be done in XSD 1.0.

您可以使用条件类型分配:

You can use Condition Type Assignment:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
           elementFormDefault="qualified"
           xmlns:vc="http://www.w3.org/2007/XMLSchema-versioning"
           vc:minVersion="1.1">

  <xs:element name="params">
    <xs:complexType>
      <xs:sequence>
        <xs:element maxOccurs="10" name="param">
          <xs:alternative test="@dataType='java.lang.String'" type="xs:string"/> 
          <xs:alternative test="@dataType='java.lang.Integer'" type="xs:integer"/> 
          <xs:alternative test="@dataType='oracle.jbo.domain.Date'" type="xs:date"/> 
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>

</xs:schema>

虽然这是表示基于属性的类型的正确结构,但您应该注意,在某些情况下,XSD 类型可能与 Java 类型不同,因此请根据您的要求检查以确保任何差异都可以容忍.

While this is the proper structure for representing attribute-based types, you should take care to note that XSD types may vary from Java types in some cases, so check to make sure that any differences are tolerable given your requirements.

这篇关于名称相同但类型不同的 XSD 元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆