繁体   English   中英

如何使用 XSD 允许任意数量的任何属性?

[英]How to allow any number of any attributes with XSD?

如何实现简单的<table>标签与 XSD 匹配?

以下不起作用:

<xs:element name="table">
  <xs:complexType>
    <xs:sequence>
      <xs:element name="tr" minOccurs="0" maxOccurs="unbounded">
        <xs:complexType>
          <xs:sequence>
            <xs:element name="td" 
                        minOccurs="0" 
                        maxOccurs="unbounded" 
                        type="MixedTemplateContentContainer"/>  
          </xs:sequence>
        </xs:complexType>
      </xs:element>
    </xs:sequence>
    <xs:anyAttribute/>
  </xs:complexType>
</xs:element>

如果我在 table 标签中使用任何属性,它会失败:

<table ccc="table" bgcolor="#ffffff">
<tr align="center"><td>

属性 'ccc' 不允许出现在元素 'table' 中。

我用 Saxon 模式处理器运行了这个,并得到了错误消息

test.xml 第 1 行的验证错误:XSD99999:与属性 @ccc 匹配的<xs:anyAttribute>需要严格验证,但没有可用的属性声明

本质上, <xs:anyAttribute>有一个processContents属性,默认值是严格的,这意味着你可以使用任何你喜欢的属性,前提是模式中有一个匹配的属性声明。 您可能想要 processContents="lax" 或 "skip"。 值 lax 意味着如果模式包含一个匹配的属性声明,则该属性将根据匹配的属性声明进行验证,否则无论如何都被接受。 值跳过意味着任何属性都被接受而无需验证。

你的问题是什么? 是吗

如何使用 XSD 允许任意数量的任何属性?

如何用XSD实现简单的标签匹配?

因为它们看起来完全不同的问题。

第一个的答案是使用xs:anyAttribute

<xs:anyAttribute>元素的processContents属性设置为skip

<xs:anyAttribute processContents="skip" />
                 ^^^^^^^^^^^^^^^^^^^^^^

制作完整的 XSD:

<xs:element name="table">
  <xs:complexType>
    <xs:sequence>
      <xs:element name="tr" minOccurs="0" maxOccurs="unbounded">
        <xs:complexType>
          <xs:sequence>
            <xs:element name="td" 
                        minOccurs="0" 
                        maxOccurs="unbounded" 
                        type="MixedTemplateContentContainer"/>  
          </xs:sequence>
        </xs:complexType>
      </xs:element>
    </xs:sequence>
    <xs:anyAttribute processContents="skip"/>
  </xs:complexType>
</xs:element>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM