2011-12-02 79 views
1

如果我与< anyattribute/>元素两个元素在我的模式是这样的:Xml架构与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> 

是否有可能只是延长另一个这些元素中的一个架构?假设我想仅为NodeType2添加属性。

回答

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> 

有了这个模式,下面的示例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"/> 

,如果我改变了

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

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

同一样本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. 

的S改变为一个数值,它将使XML有效。

对于“扩展”,这不是一件有趣的事吗?...