使用 XSD 中的属性限制 complexType? [英] Restrict complexType with attributes in XSD?

查看:29
本文介绍了使用 XSD 中的属性限制 complexType?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 XSD,例如:

<xs:element name="algo"><xs:complexType><xs:序列><xs:element name="nota" type="t_algo" minOccurs="0"maxOccurs="无界"/></xs:sequence></xs:complexType></xs:element><xs:complexType name="t_algo"><xs:restriction base="xs:string"><xs:pattern value="[1][0]|[0-9]"/></xs:restriction><xs:attribute name="modul" type="t_modul"/></xs:complexType><xs:simpleType name="t_modul"><xs:restriction base="xs:string"><xs:pattern value="[m][0][0-9]"/></xs:restriction></xs:simpleType></xs:schema>

并使用这样的测试 XML:

<nota modul="m01">0</nota><nota modul="m01">7</nota><nota modul="m01">3</nota><nota modul="m01">1</nota></算法>

我想让孩子 nota 的值在 0-10 之间,并且属性 modul 的值为 m0X where x 介于 (0-9) 之间.我认为以前的 XSD 会起作用,但它不会.有人可以解释一下我做错了什么吗?

解决方案

为了在内容受限的元素上拥有一个属性,定义一个新的 xs:simpleType 然后使用 xs:extension 用一个属性扩展它:

<xs:element name="algo"><xs:complexType><xs:序列><xs:element name="nota" type="t_algo" minOccurs="0" maxOccurs="unbounded"/></xs:sequence></xs:complexType></xs:element><xs:complexType name="t_algo"><xs:extension base="t_algo_content"><xs:attribute name="modul" type="t_modul"/></xs:extension></xs:simpleContent></xs:complexType><xs:simpleType name="t_modul"><xs:restriction base="xs:string"><xs:pattern value="m0[0-9]"/></xs:restriction></xs:simpleType><xs:simpleType name="t_algo_content"><xs:restriction base="xs:integer"><xs:minInclusive value="0"/><xs:maxInclusive value="10"/></xs:restriction></xs:simpleType></xs:schema>

另请注意,我在第一种情况下简化了您的正则表达式模式,并使用 minInclusive/maxInclusive 在第二种情况下更自然地表达您的整数范围.>

I'm working with an XSD such as:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" 
           elementFormDefault="qualified">
  <xs:element name="algo">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="nota" type="t_algo" minOccurs="0"
                    maxOccurs="unbounded"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>

  <xs:complexType name="t_algo">
    <xs:restriction base="xs:string">
      <xs:pattern value="[1][0]|[0-9]" />
    </xs:restriction>
    <xs:attribute name="modul" type="t_modul"/>
  </xs:complexType>

  <xs:simpleType name="t_modul">
    <xs:restriction base="xs:string">
      <xs:pattern value="[m][0][0-9]"/>
    </xs:restriction> 
  </xs:simpleType> 

</xs:schema>

and using a test XML like this:

<?xml version="1.0" encoding="UTF-8"?>
<algo xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
      xsi:noNamespaceSchemaLocation="file:///D:/DAM/RECUF1/Mispruebas/telf.xsd">
  <nota modul="m01">0</nota>
  <nota modul="m01">7</nota>
  <nota modul="m01">3</nota>
  <nota modul="m01">1</nota>
</algo>

I want to have children nota with values between 0-10 and with the attribute modul with a value m0X where x between (0-9). I thought the previous XSD would work, but it does not. Can somebody explain me what I'm doing wrong?

解决方案

In order to have an attribute on an element with restricted content, define a new xs:simpleType and then use xs:extension to extend it with an attribute:

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

  <xs:element name="algo">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="nota" type="t_algo" minOccurs="0" maxOccurs="unbounded"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>

  <xs:complexType name="t_algo">
    <xs:simpleContent>
      <xs:extension base="t_algo_content">
        <xs:attribute name="modul" type="t_modul"/>
      </xs:extension>
    </xs:simpleContent>
  </xs:complexType>

  <xs:simpleType name="t_modul">
    <xs:restriction base="xs:string">
      <xs:pattern value="m0[0-9]"/>
    </xs:restriction> 
  </xs:simpleType> 

  <xs:simpleType name="t_algo_content">
    <xs:restriction base="xs:integer">
      <xs:minInclusive value="0"/>
      <xs:maxInclusive value="10"/>
    </xs:restriction>
  </xs:simpleType>

</xs:schema>

Note also that I've simplified your regex pattern in the first case and used minInclusive/maxInclusive to more naturally express your integer range in the second case.

这篇关于使用 XSD 中的属性限制 complexType?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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