# 10.2.1. 创作 Schema

创建与 Spring 的 IoC 容器一起使用的 XML 配置扩展首先要创建一个 XML Schema 来描述扩展。对于我们的示例，我们使用以下模式来配置`SimpleDateFormat`对象：

```xml
<!-- myns.xsd (inside package org/springframework/samples/xml) -->

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns="http://www.mycompany.example/schema/myns"
        xmlns:xsd="http://www.w3.org/2001/XMLSchema"
        xmlns:beans="http://www.springframework.org/schema/beans"
        targetNamespace="http://www.mycompany.example/schema/myns"
        elementFormDefault="qualified"
        attributeFormDefault="unqualified">

    <xsd:import namespace="http://www.springframework.org/schema/beans"/>

    <xsd:element name="dateformat">
        <xsd:complexType>
            <xsd:complexContent>
                <xsd:extension base="beans:identifiedType"> 
                    <xsd:attribute name="lenient" type="xsd:boolean"/>
                    <xsd:attribute name="pattern" type="xsd:string" use="required"/>
                </xsd:extension>
            </xsd:complexContent>
        </xsd:complexType>
    </xsd:element>
</xsd:schema>
```

指示的行包含所有可识别标签的扩展库（意味着它们具有`id`我们可以用作容器中的 bean 标识符的属性）。我们可以使用这个属性，因为我们导入了 Spring 提供的 `beans`命名空间。

上述模式允许我们使用`<myns:dateformat/>`元素直接在 XML 应用程序上下文文件中配置`SimpleDateFormat`对象，如以下示例所示：

```xml
<myns:dateformat id="dateFormat"
    pattern="yyyy-MM-dd HH:mm"
    lenient="true"/>
```

请注意，在我们创建了基础设施类之后，前面的 XML 片段本质上与以下 XML 片段相同：

```xml
<bean id="dateFormat" class="java.text.SimpleDateFormat">
    <constructor-arg value="yyyy-MM-dd HH:mm"/>
    <property name="lenient" value="true"/>
</bean>
```

前面两个片段中的第二个在容器中创建了一个带有几个属性集的 bean（由`SimpleDateFormat`类型的名称 `dateFormat` 标识）。

创建配置格式的基于模式的方法允许与具有模式感知 XML 编辑器的 IDE 紧密集成。通过使用正确编写的模式，您可以使用自动完成功能让用户在枚举中定义的多个配置选项之间进行选择。


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://doc.shiker.tech/spring-he-xin-gong-neng/10.-fu-lu/10.2.-zi-ding-yi-xml-schema/10.2.1.-chuang-zuo-schema.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
