使用AnyAttribute的Xml Schema,将属性添加到特定元素

Kri*_*all 1 xsd

如果anyattribute我的架构中有两个带< />元素的元素,如下所示:

<xs:element name="NodeType1">
  <xs:complexType>
    <xs:anyAttribute />
  </xs:complexType>
</xs:element>

<xs:element name="NodeType2">
  <xs:complexType>
    <xs:anyAttribute />
  </xs:complexType>
</xs:element>
Run Code Online (Sandbox Code Playgroud)

是否可以在另一个模式中仅扩展其中一个元素?假设我只想添加一个属性NodeType2.

Pet*_*dea 7

元素不可扩展; 通常,如果您希望在其他任何位置使用扩展的基础,则必须创建一个命名(全局,在架构元素下)类型,在相同或另一个XML架构中.

你的问题非常有趣,因为它确实让人想知道扩展某些东西的目的是什么,无论如何,它可以匹配任何东西.为此,扩展的效果恰恰相反; 它为扩展中指定的属性创建约束.

那么,第一个XSD:

<?xml version="1.0" encoding="utf-8" ?>
<!--W3C Schema generated by QTAssistant/W3C Schema Refactoring Module (http://www.paschidev.com)-->
<xsd:schema targetNamespace="http://tempuri.org/XMLSchema.xsd"
    elementFormDefault="qualified"
    xmlns="http://tempuri.org/XMLSchema.xsd"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema">

    <xsd:element name="root" type="BaseType"/>

    <xsd:complexType name="BaseType">
        <xsd:anyAttribute processContents="lax" />
    </xsd:complexType>

    <xsd:complexType name="Extending">
        <xsd:complexContent>
            <xsd:extension base="BaseType">
                <xsd:attribute name="new1" type="xsd:int"/>
            </xsd:extension>
        </xsd:complexContent>
    </xsd:complexType>
</xsd:schema>
Run Code Online (Sandbox Code Playgroud)

使用此模式,以下示例XML完全有效:

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<!-- Sample XML generated by QTAssistant (http://www.paschidev.com) -->
<root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" new1="S" new2="2" xmlns="http://tempuri.org/XMLSchema.xsd"/>
Run Code Online (Sandbox Code Playgroud)

现在,如果我正在改变

    <xsd:element name="root" type="BaseType"/>
Run Code Online (Sandbox Code Playgroud)

    <xsd:element name="root" type="Extending"/>
Run Code Online (Sandbox Code Playgroud)

如果不再有效,则使用相同的示例XML:

Error occurred while loading [], line 3 position 61
The 'new1' attribute is invalid - The value 'S' is invalid according to its datatype 'http://www.w3.org/2001/XMLSchema:int' - The string 'S' is not a valid Int32 value.
Run Code Online (Sandbox Code Playgroud)

将S更改为数字,将使XML有效.

关于"延伸"这不是一件有趣的事吗?......