powered by simpleCommunicator - 2.0.49     © 2025 Programmizd 02
Форумы / XML, XSL, XPath, XQuery [игнор отключен] [закрыт для гостей] / Исправить схему
7 сообщений из 7, страница 1 из 1
Исправить схему
    #38567766
akabynga
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Гость
Доброго времени суток!
Возникла проблема при написании схемы к xml файлу.
Можно ли сделать элемент, который бы принимал разный комплексный тип, то есть есть элемент device, нужно, чтобы он мог быть как типа PhoneType, так и Photocamera, Tablet, EBook.
Вот моя XSD схема:
Код: xml
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
37.
38.
39.
40.
41.
42.
43.
44.
45.
46.
47.
48.
49.
50.
51.
52.
53.
54.
55.
56.
57.
58.
59.
60.
61.
62.
63.
64.
65.
66.
67.
68.
69.
70.
71.
72.
73.
74.
75.
76.
77.
78.
79.
80.
81.
82.
83.
84.
85.
86.
87.
88.
89.
90.
91.
92.
93.
94.
95.
96.
97.
<?xml version="1.0" encoding="utf-8"?><!-- -->
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
	<xsd:element name="gadgets">
		<xsd:complexType>
			<!-- Создание составного элемента persons -->
			<xsd:sequence>
				<xsd:element name="device" type="GadgetType" minOccurs="0" />
			</xsd:sequence>
		</xsd:complexType>
	</xsd:element>
	<!-- Тип для расширения -->
	<xsd:complexType name="GroupType">
		<xsd:sequence>
			<xsd:element name="name" type="xsd:string" />
			<xsd:element name="memory" type="xsd:string" />
			<xsd:element name="cost" type="CostType" />
			<xsd:element name="weight" type="WeightType" />
		</xsd:sequence>
		<xsd:attributeGroup ref="DeviceAttributes" />
	</xsd:complexType>
	<!-- CostType - тип с ограничением для цены(не может быть меньше нуля) -->
	<xsd:simpleType name="CostType">
		<xsd:restriction base="xsd:integer">
			<xsd:minInclusive value="0" />
		</xsd:restriction>
	</xsd:simpleType>
	<!-- WeightType - тип с ограничением для веса(не может быть меньше нуля) -->
	<xsd:simpleType name="WeightType">
		<xsd:restriction base="xsd:float">
			<xsd:minInclusive value="0" />
		</xsd:restriction>
	</xsd:simpleType>
	<!-- Тип для атрибутов device -->
	<xsd:attributeGroup name="DeviceAttributes">
		<xsd:attribute name="id" type="xsd:ID" use="required" />
		<xsd:attribute name="type" type="GadgetEnumType" use="required" />
	</xsd:attributeGroup>
	<!-- GadgetEnumType - тип перечисление для атрибута type -->
	<xsd:simpleType name="GadgetEnumType">
		<xsd:restriction base="xsd:string">
			<xsd:enumeration value="Phone" />
			<xsd:enumeration value="Photocamera" />
			<xsd:enumeration value="Tablet" />
			<xsd:enumeration value="EBook" />
		</xsd:restriction>
	</xsd:simpleType>
	<!-- Расширяем тип GroupType до типа EBook -->
	<xsd:complexType name="EBookType">
		<xsd:complexContent>
			<xsd:extension base="GroupType">
				<xsd:sequence>
					<xsd:element name="screen-type" type="xsd:string" />
				</xsd:sequence>
			</xsd:extension>
		</xsd:complexContent>
	</xsd:complexType>
	<!-- Расширяем тип GroupType до типа TabletType -->
	<xsd:complexType name="TabletType">
		<xsd:complexContent>
			<xsd:extension base="GroupType">
				<xsd:sequence>
					<xsd:element name="screen-size" type="xsd:float" />
				</xsd:sequence>
			</xsd:extension>
		</xsd:complexContent>
	</xsd:complexType>
	<!-- Расширяем тип GroupType до типа PhotocameraType -->
	<xsd:complexType name="PhotocameraType">
		<xsd:complexContent>
			<xsd:extension base="GroupType">
				<xsd:sequence>
					<xsd:element name="lens" type="xsd:string" />
				</xsd:sequence>
			</xsd:extension>
		</xsd:complexContent>
	</xsd:complexType>
	<!-- Расширяем тип GroupType до типа PhoneType -->
	<xsd:complexType name="PhoneType">
		<xsd:complexContent>
			<xsd:extension base="GroupType">
				<xsd:sequence>
					<xsd:element name="camera" type="xsd:string" />
				</xsd:sequence>
			</xsd:extension>
		</xsd:complexContent>
	</xsd:complexType>

	<xsd:complexType name="GadgetType">
		<xsd:choice>
			<xsd:element name="device" type="PhotocameraType"
				minOccurs="0" />
			<xsd:element name="device" type="PhoneType" minOccurs="0" />
			<xsd:element name="device" type="TabletType" minOccurs="0" />
			<xsd:element name="device" type="EBookType" minOccurs="0" />
		</xsd:choice>
	</xsd:complexType>
</xsd:schema>



И XML файл к нему:
Код: xml
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
37.
38.
39.
40.
41.
42.
43.
44.
45.
46.
47.
48.
49.
50.
51.
52.
53.
54.
55.
56.
57.
58.
59.
60.
<?xml version="1.0" encoding="UTF-8"?>
<gadgets xmlns:xsd="http://www.w3.org/2001/XMLSchema-instance"
	xsd:noNamespaceSchemaLocation="gadgets.xsd">
	<device id="1" type="Phone">
		<name>Nokia 5800</name>
		<memory>4GB</memory>
		<cost>200</cost>
		<weight>213</weight>
		<camera>3.2 mpx</camera>
	</device>
	
	<device id="2" type="Tablet">
		<name>Apple iPad mini</name>
		<memory>16GB</memory>
		<cost>500</cost>
		<weight>312</weight>
		<screen-size>7</screen-size>
	</device>
	
	<device id="3" type="EBook">
		<name>teXet TB-138</name>
		<memory>2GB</memory>
		<cost>118</cost>
		<weight>150</weight>
		<screen-type>Монохромный</screen-type>
	</device>
	
	<device id="4" type="Photocamera">
		<name>Canon 60D</name>
		<memory>16GB</memory>
		<cost>1100</cost>
		<weight>868</weight>
		<lens>Kit 18-55mm IS</lens>
	</device>
	
	<device id="5" type="Phone">
		<name>Apple iPhone 4S</name>
		<memory>8GB</memory>
		<cost>550</cost>
		<weight>140</weight>
		<camera>8 mpx</camera>
	</device>
	
	<device id="6" type="Tablet">
		<name>ZIFRO Zt-7800</name>
		<memory>4GB</memory>
		<cost>210</cost>
		<weight>374</weight>
		<screen-size>10</screen-size>
	</device>
	
	<device id="7" type="Photocamera">
		<name>Canon 7D</name>
		<memory>32GB</memory>
		<cost>1600</cost>
		<weight>1158</weight>
		<lens>50mm 1.2 L</lens>
	</device>
	
</gadgets>


Заранее спасибо!
...
Рейтинг: 0 / 0
Исправить схему
    #38567806
Фотография Antonariy
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Участник
Не вижу смысла городить такую типизацию. Для device все правила сведутся к тому, что name, memory, cost и weight являются обязательными, а screen-type, screen-size, lens и camera не обязательны и исключают друг друга. Это и нужно описать.

Код: xml
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
  <xsd:complexType name="DeviceType">
    <xsd:complexContent>
      <xsd:extension base="GroupType">
        <xsd:choice minOccurs="0" maxOccurs="1">
          <xsd:element name="screen-type" type="xsd:string" />
          <xsd:element name="screen-size" type="xsd:float" />
          <xsd:element name="lens" type="xsd:string" />
          <xsd:element name="camera" type="xsd:string" />
        </xsd:choice>
      </xsd:extension>
    </xsd:complexContent>
  </xsd:complexType>

  <xsd:complexType name="GadgetType">
    <xsd:sequence minOccurs="0" maxOccurs="unbounded">
      <xsd:element name="device" type="DeviceType" minOccurs="0" />
    </xsd:sequence>
  </xsd:complexType>
...
Рейтинг: 0 / 0
Исправить схему
    #38567809
akabynga
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Гость
Antonariy, это пример, по таску обязательное наследование.
...
Рейтинг: 0 / 0
Исправить схему
    #38567810
Фотография Antonariy
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Участник
Antonariy а screen-type, screen-size, lens и camera не обязательныВру, наоборот, обязателен один из них.

Код: xml
1.
<xsd:choice minOccurs="1" maxOccurs="1">
...
Рейтинг: 0 / 0
Исправить схему
    #38567822
Фотография Antonariy
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Участник
akabyngaAntonariy, это пример, по таску обязательное наследование.Ок.

akabyngaМожно ли сделать элемент, который бы принимал разный комплексный тип, то есть есть элемент device, нужно, чтобы он мог быть как типа PhoneType, так и Photocamera, Tablet, EBook.Нельзя.

И я сомневаюсь, что можно добиться этого наследованием.
...
Рейтинг: 0 / 0
Исправить схему
    #38567824
Фотография Antonariy
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Участник
Этого — в смысле выполнения правил, которые я описал.
...
Рейтинг: 0 / 0
Исправить схему
    #38567826
akabynga
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Гость
Antonariy, Ок, спасибо, я кажется понял, как можно тут вырулить) дело в том, что в дальнейшем будет расширение дополнительных полей, таких как screen-type, screen-size, lens и camera. Можно сделать эти поля, как комплексный тип и расширять до бесконечности. Еще раз спасибо, 3 день учу схемы, до этого не сталкивался.
...
Рейтинг: 0 / 0
7 сообщений из 7, страница 1 из 1
Форумы / XML, XSL, XPath, XQuery [игнор отключен] [закрыт для гостей] / Исправить схему
Целевая тема:
Создать новую тему:
Автор:
Закрыть
Цитировать
Найденые пользователи ...
Разблокировать пользователей ...
Читали форум (0):
Пользователи онлайн (0):
x
x
Закрыть


Просмотр
0 / 0
Close
Debug Console [Select Text]