Say I have these types defined in my XSD:
假设我在XSD中定义了这些类型:
<complexType name="NamedEntity">
<attribute name="ix" type="positiveInteger"></attribute>
<attribute name="sName" type="string"></attribute>
<attribute name="txtDesc" type="string"></attribute>
</complexType>
<complexType name="Node">
<complexContent>
<extension base="tns:NamedEntity">
</extension>
</complexContent>
</complexType>
<complexType name="Source">
<complexContent>
<extension base="tns:NamedEntity">
<attribute name="dt" type="dateTime"></attribute>
</extension>
</complexContent>
</complexType>
Now I want to express that a Node
element may have zero or more child elements that may be of the type Node
or Source
.
现在我想表达一个Node元素可能有零个或多个子元素,可以是Node或Source类型。
It would be OK if I had to somehow enumerate the allowed types for the children, but since I have more types that inherit from NamedEntity
, it would be neat if I could specify just the base type.
如果我不得不以某种方式枚举子节点的允许类型,那就没关系了,但由于我有更多类型从NamedEntity继承,如果我只能指定基类型,那将是很好的。
Edit: I'd rather not use xsi:type
in the document but have a unambigous relationship between element name and type. Quite a lot XML processing seems to depend on that, and I also find it a lot more readable.
编辑:我宁愿不在文档中使用xsi:type,但在元素名称和类型之间有明确的关系。相当多的XML处理似乎依赖于此,我也发现它更具可读性。
4 个解决方案
#1
Please don't use xsi:type
if you can avoid it. It's evil. Ok, maybe I exaggerate, but it does make it impossible to parse the document without intimate knowledge of the schema, which is bad enough in practice.
如果可以避免,请不要使用xsi:type。这是邪恶的。好吧,也许我夸大了,但它确实无法在没有对模式的深入了解的情况下解析文档,这在实践中已经足够糟糕了。
What will help you is: substitutionGroup
.
对你有帮助的是:substitutionGroup。
#2
In the schema, have the Node
element contain zero or more child elements of type NamedEntity
. In the actual document, use the xsi:type
attribute (xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
) to choose the subtype ("Node"
or "Source"
) for each one.
在模式中,让Node元素包含零个或多个NamedEntity类型的子元素。在实际文档中,使用xsi:type属性(xmlns:xsi =“http://www.w3.org/2001/XMLSchema-instance”)为每个属性选择子类型(“节点”或“源”) 。
#3
This may be beyond the capabilities of XSD. Have you considered doing extra validation using Schematron?
这可能超出了XSD的功能。您是否考虑过使用Schematron进行额外验证?
#1
Please don't use xsi:type
if you can avoid it. It's evil. Ok, maybe I exaggerate, but it does make it impossible to parse the document without intimate knowledge of the schema, which is bad enough in practice.
如果可以避免,请不要使用xsi:type。这是邪恶的。好吧,也许我夸大了,但它确实无法在没有对模式的深入了解的情况下解析文档,这在实践中已经足够糟糕了。
What will help you is: substitutionGroup
.
对你有帮助的是:substitutionGroup。
#2
In the schema, have the Node
element contain zero or more child elements of type NamedEntity
. In the actual document, use the xsi:type
attribute (xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
) to choose the subtype ("Node"
or "Source"
) for each one.
在模式中,让Node元素包含零个或多个NamedEntity类型的子元素。在实际文档中,使用xsi:type属性(xmlns:xsi =“http://www.w3.org/2001/XMLSchema-instance”)为每个属性选择子类型(“节点”或“源”) 。
#3
This may be beyond the capabilities of XSD. Have you considered doing extra validation using Schematron?
这可能超出了XSD的功能。您是否考虑过使用Schematron进行额外验证?
#4
I think you want a substitution group.
我想你想要一个替代组。