Xml 将相同的属性包含到多个元素中,而无需复制粘贴

Xml 将相同的属性包含到多个元素中,而无需复制粘贴,xml,xsd,Xml,Xsd,我有笔记本电脑、个人电脑、打印机等。它们都应该具有相同的属性,例如,价格和型号。是否可以只声明一次属性列表并将其包含到3个元素中?一般来说,人们不限定属性;要在不使用名称空间的情况下重用它们,尤其是在列表中,可以使用attributeGroup或作为公共基类型的一部分 这是一个XSD,显示了您拥有的不同可能性: <?xml version="1.0" encoding="utf-8" ?> <!-- XML Schema generated by QTAssistant/XSD

我有笔记本电脑、个人电脑、打印机等。它们都应该具有相同的属性,例如,价格和型号。是否可以只声明一次属性列表并将其包含到3个元素中?

一般来说,人们不限定属性;要在不使用名称空间的情况下重用它们,尤其是在列表中,可以使用attributeGroup或作为公共基类型的一部分

这是一个XSD,显示了您拥有的不同可能性:

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

    <xsd:attributeGroup name="Priced">
        <xsd:annotation>
            <xsd:documentation>
                Allows reuse of common attributes in components 
                that are not of the same kind.
            </xsd:documentation>
        </xsd:annotation>
        <xsd:attribute name="price" type="xsd:decimal"/>
        <xsd:attribute name="model" type="xsd:string" use="required"/>
    </xsd:attributeGroup>

    <xsd:element name="Printer">
        <xsd:annotation>
            <xsd:documentation>             
                How to reference an attribute group.
                Here Printer is considered in a different
                type hierarchy, so it shares the Priced attributes
                only.
            </xsd:documentation>
        </xsd:annotation>
        <xsd:complexType>
            <xsd:sequence/>             
            <xsd:attributeGroup ref="Priced"/>
        </xsd:complexType>
    </xsd:element>

    <xsd:complexType name="Base">
        <xsd:annotation>
            <xsd:documentation>
                If Printer, PC, and Laptop are ultimately a
                "Base", and "Priced" is applicable to the "Base"
                type only, then you don't need an attribute                 
                group; reuse through base type.
            </xsd:documentation>
        </xsd:annotation>
        <xsd:sequence/>         
        <xsd:attribute name="price" type="xsd:decimal"/>
        <xsd:attribute name="model" type="xsd:string" use="required"/>
    </xsd:complexType>

    <xsd:complexType name="AnotherBase">
        <xsd:annotation>
            <xsd:documentation>
                Another base type; here reuse the attribute group.              
            </xsd:documentation>
        </xsd:annotation>
        <xsd:sequence/>         
        <xsd:attributeGroup ref="Priced"/>
    </xsd:complexType>

    <xsd:element name="Laptop">
        <xsd:annotation>
            <xsd:documentation>
                How to reuse a base type.               
            </xsd:documentation>
        </xsd:annotation>
        <xsd:complexType>
            <xsd:complexContent>
                <xsd:extension base="Base">
                    <xsd:sequence/>                     
                </xsd:extension>
            </xsd:complexContent>
        </xsd:complexType>
    </xsd:element>

    <xsd:element name="PC">
        <xsd:complexType>
            <xsd:complexContent>
                <xsd:extension base="Base">
                    <xsd:sequence/>
                </xsd:extension>
            </xsd:complexContent>
        </xsd:complexType>
    </xsd:element>
</xsd:schema>

允许重用组件中的公共属性
那不是同一类的。
如何引用属性组。
在这里,打印机被认为是另一种形式
类型层次结构,因此它共享定价属性
只有
如果打印机、个人电脑和笔记本电脑最终成为
“基准”和“定价”适用于“基准”
仅键入,则不需要属性
组;通过基类型重用。
另一种基本类型;这里重用属性组。
如何重用基类型。