问题描述
我有一个如下的XML文档:
I have an XML document like below:
<operation name="func">
<param name="a" type="string">up</param>
<param name="b" type="integer">5</param>
<param name="c" type="CustomList">
<list>
<idx>0</idx>
<name>ZZZZ</name>
<value>0.5464</value>
</list>
<list>
<idx>1</idx>
<name>YYYY</name>
<value>0.464</value>
</list>
....
</param>
</operation>
我写了一个XSD来限制它:
and I wrote an XSD to restrict it:
<xs:complexType name="CustomListNode">
<xs:sequence>
<xs:element name="idx" type="xs:integer" />
<xs:element name="name" type="xs:string" />
<xs:element name="value" type="xs:decimal" />
</xs:sequence>
</xs:complexType>
<xs:complexType name="CustomList">
<xs:sequence>
<xs:element name="list" type="CustomListNode" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
<xs:element name="operation">
<xs:complexType>
<xs:sequence>
<xs:element name="param" maxOccurs="unbounded">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:NOTATION">
<xs:attribute name="name" type="xs:string" use="required" />
<xs:attribute name="type" type="xs:integer" use="required" />
<xs:attribute name="type" type="xs:string" use="required" />
<xs:attribute name="type" type="CustomList" use="required"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="name" type="xs:string" use="required" />
</xs:complexType>
</xs:element>
当然,这是行不通的.有人建议我在XSD 1.1中使用 xs:alternative
.但是我现在只有libxml2.它不支持XSD 1.1.
Of course, this doesn't work. Someone suggested me to use xs:alternative
in XSD 1.1.But I only have libxml2 now. It doesn't support XSD 1.1.
如何在XSD 1.0中进行操作?还是不能完全在XSD 1.0中实现?
How can I do in XSD 1.0? Or it cannot be implemented in XSD 1.0 absolutely?
推荐答案
条件类型分配(xs:alternative
)需要XSD 1.1.
Conditional Type Assignment (xs:alternative
) requires XSD 1.1.
您无法在XSD 1.0中进行条件类型分配,但是如果您愿意调整XML设计,则可以使用xsi:type
来实现类似的效果:
You cannot do Conditional Type Assignment in XSD 1.0, but if you're open to adjusting your XML design, you could use xsi:type
to achieve a similar effect:
<?xml version="1.0" encoding="UTF-8"?>
<operation xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
name="func">
<a xsi:type="xs:string">up</a>
<b xsi:type="xs:integer">5</b>
<c xsi:type="CustomList">
<list>
<idx>0</idx>
<name>ZZZZ</name>
<value>0.5464</value>
</list>
<list>
<idx>1</idx>
<name>YYYY</name>
<value>0.464</value>
</list>
</c>
</operation>
XSD 1.0
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="operation">
<xs:complexType>
<xs:sequence>
<xs:any maxOccurs="unbounded" processContents="lax"/>
</xs:sequence>
<xs:attribute name="name" type="xs:string" use="required"/>
</xs:complexType>
</xs:element>
<xs:complexType name="CustomList">
<xs:sequence>
<xs:element name="list" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="idx" type="xs:integer"/>
<xs:element name="name" type="xs:string"/>
<xs:element name="value" type="xs:decimal"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:schema>
这篇关于XML模式:基于父属性“类型"的子元素类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!