Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.1k views
in Technique[技术] by (71.8m points)

xml - Restrict complexType with attributes in XSD?

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...