首页 > 解决方案 > 为什么我的 xsd 验证未声明的属性?

问题描述

我有一个 xml,我想在添加新属性后针对 xsd 进行验证。
我试图为 xsd 中的某些元素在本地声明属性,但不知道该怎么做。
一时兴起,我拿了一个已经验证过的 xml 并将新属性添加到一个元素,而不对 xsd 进行任何更改,并且验证成功通过。

属性(字母间距)仅通过了验证,它具有命名空间前缀(命名空间 'dptt' 在 xsd 中定义并出现在 xml 中):
通过验证的行示例:

<tt:style dptt:letter-spacing="4px" style="s01" tts:lineHeight="120px" xml:id="1"/>

未通过验证的行示例:

<tt:style letter-spacing="4px" style="s01" tts:lineHeight="120px" xml:id="1"/>

这是定义样式属性的 xsd 部分:

<xs:element name="style">
    <xs:complexType>
        <xs:sequence>
            <xs:element ref="tt:metadata" minOccurs="0"/>
        </xs:sequence>
        <xs:attribute ref="xml:id" use="required">
        </xs:attribute>
        <xs:attribute name="style" type="xs:IDREFS">
        </xs:attribute>
        <xs:attribute ref="tts:direction" >
        </xs:attribute>
        <xs:attribute ref="tts:fontFamily" >
        </xs:attribute>
        <xs:attribute ref="tts:fontSize" >
        </xs:attribute>
        <xs:attribute ref="tts:lineHeight" >
        </xs:attribute>
        <xs:attribute ref="tts:textAlign" >
        </xs:attribute>
        <xs:attribute ref="tts:color" >
        </xs:attribute>
        <xs:attribute ref="tts:backgroundColor" >
        </xs:attribute>
        <xs:attribute ref="tts:fontStyle" >
        </xs:attribute>
        <xs:attribute ref="tts:fontWeight" >
        </xs:attribute>
        <xs:attribute ref="tts:textDecoration" >
        </xs:attribute>
        <xs:attribute ref="tts:unicodeBidi" >
        </xs:attribute>
        <xs:attribute ref="tts:padding">
        </xs:attribute>
        <xs:attribute ref="ebutts:multiRowAlign"/>
    </xs:complexType>
</xs:element>

我希望未声明的属性不会通过验证。
我还想知道为什么添加命名空间对于验证至关重要。

标签: xmlxsdxsd-validation

解决方案


没有看到我们不可能知道的模式。我的猜测是包含元素的复杂类型包括xs:anyAttribute通配符。


推荐阅读