XSD elements with same name but different types

雨燕双飞 提交于 2021-01-29 04:02:09

问题


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.

My 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>

My 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>

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.


回答1:


XSD 1.0

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

XSD 1.1

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>

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.



来源:https://stackoverflow.com/questions/33418737/xsd-elements-with-same-name-but-different-types

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!