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

标签 xml xsd

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

最佳答案

一般来说,人们并不限定属性;要在没有命名空间的情况下重用它们,特别是在列表中,您可以使用 attributeGroup 或作为公共(public)基本类型的一部分。

这是一个 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>

关于xml - 将相同的属性包含到多个元素中,无需复制粘贴,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16250904/

相关文章:

java - 找不到元素 'beans' 的声明。

java - 从java中的wsdl读取复杂类型

c# - 更新表适配器配置引用错误

Java XML 数字字符引用

java - 捕获异常后的xml解析和验证

xml - 错误: The target of 'replace value of' must be a non-metadata attribute or an element with simple typed content

c# - 使用 XSD 创建 XML 并发布到 URL 的步骤

jaxb - 如何更改 JAXB 生成的列表的名称

java - 设置 Activity 向上按钮

c# - 将此 XML 文档转换为我的对象的最简单方法是什么?