【发布时间】:2021-12-11 23:34:06
【问题描述】:
与In XSD I want to specify that an element can only have whitespace content 和In XSD how do I allow only whitespace in an element's content? 相关,我有已为其创建XSD 文件的XML 数据文件。在生成 XSD 文件并针对输入进行测试后,我发现传入的数据文件通常具有如下模式,其中包含不带文本的元素:
<source
id="UGCStrain"
name="The Strain Complex"
abbrev="The Strain">
</source>
目前,我的 XSD 有很多元素,如下所示,它们具有属性,有时还有子元素,但不使用嵌入文本:
<xs:element name="source">
<xs:complexType>
<xs:attribute name="id" use="required" type="uniqueID"/>
<xs:attribute name="name" use="required" type="xs:string"/>
<xs:attribute name="abbrev" type="xs:string" default=""/>
<xs:attribute name="description" type="xs:string" default=""/>
</xs:complexType>
</xs:element>
其他人有我想要保留的文本(在某些情况下,这是必需的)。例如,需要添加表示某些标记元素的表达式:
<enmasse
stage="init">
component.Skill
</enmasse>
带有相应的 XSD:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
<xs:element name="autotag">
<xs:complexType>
<xs:attribute name="group" use="required"/>
<xs:attribute name="tag" use="required"/>
</xs:complexType>
</xs:element>
<xs:element name="enmasse">
<xs:complexType mixed="true">
<xs:sequence minOccurs="0">
<xs:element maxOccurs="1" ref="autotag"/>
</xs:sequence>
<xs:attribute name="stage" use="required"/>
</xs:complexType>
</xs:element>
</xs:schema>
根据这两个链接的问题,可以创建一个只允许空白文本而不会引发错误的类型,但它要求每个这样的元素都被赋予该类型。有没有办法让它适用于每个元素,如果它是一个没有'mixed =“true”'的复杂类型,它允许空白“文本”?
如果相关,我正在使用 Python xmlschema 库进行 XSD 验证。
【问题讨论】:
-
您需要更具体地了解要求。你真的是说没有mixed=true的any复杂类型应该被这样对待吗?或者您是否想到了复杂类型的特定模式(可能没有任何子标签)?您提供了一些示例,但我没有看到任何这种“可忽略”空格的示例。
-
re: '但它要求每个这样的元素都被赋予那种类型'......你的评论到底是什么意思?您是否正在寻找某种方法来避免为输入 XML 中的每个标签创建元素声明?
-
@kimbert:没错。我希望将只有空格的元素视为空元素,而不必为每个元素定义显式处理它。空白对于没有空白的项目很重要,所以我认为我不能使用选项来消除它。我想如果它只是用于 XSD 评估它会起作用,但在与其他人共享它时也会变得尴尬(数据格式的创建者从未提供过 XSD 模式,尽管格式已记录)。