现在的位置: 首页 > 综合 > 正文

webx—Schema使用

2014年09月05日 ⁄ 综合 ⁄ 共 3307字 ⁄ 字号 评论关闭

XML Schema 的作用是定义 XML 文档的合法构建模块,类似 DTD。

Schema的概念中比较难理解的是命名空间(Namespace):一个Schema可以看作是若干个元素、类型组成的集合,它们属于一个特定的命名空间。通俗来讲,命名空间相关于java中的package一样,虽然内部元素的名称可能一样,但是由于来自不同的Namespace,所以不会发生冲突。

webx3的Schema与xml中的Schema不同。webx3的包含两部分:一个是定义xml文档结构的XSD文件;另一个是解析Bean定义信息的BeanDefinitionParser,它是Spring BeanDefinition的一种表现形式,包含了Bean的定义信息,最终会被框架解析成BeanDefinition

------------------------——————————————————--------------xml schema-----------------------————————————————————————————--------------

book.xsd
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" 
xmlns="http://www.onlyone.org/book" 
targetNamespace="http://www.onlyone.org/book" 
elementFormDefault="qualified">

<xs:element name="book" type="bookType"/>
<xs:complexType name="bookType">
<xs:sequence>
<xs:element name="title" type="xs:string"/>
<xs:element name="author" type="xs:string"/>
</xs:sequence>  
</xs:complexType>
</xs:schema>
book.xml
<?xml version="1.0" encoding="GB2312"?>
<book xmlns="http://www.onlyone.org/book"   ①
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  ②
xsi:schemaLocation="http://www.onlyone.org/book  http://www.onlyone.org/book.xsd">  ③
<title>秘密</title>
<author>onlyone</author>
</book>

① 声明默认的名称空间(http://www.onlyone.org/book)。
② 声明XML Schema实例名称空间(http://www.w3.org/2001/XMLSchema-instance),并将xsi前缀与该名称空间绑定,这样模式处理器就可以识别xsi:schemaLocation属性。XML Schema实例名称空间的前缀通常使用xsi。
③ xsi:schemaLocation属性的值由一个URI引用对组成,两个URI之间以空白符分隔。第一个URI是命名空间的名字,第二个URI给出模式文档的位置,模式处理器将从这个位置读取模式文档,该模式文档的目标名称空间必须与第一个URI相匹配。例子使用xsi:schemaLocation属性指定名称空间http://www.onlyone.org/book和模式位置http://www.onlyone.org/book.xsd相关。要注意,在这个例子中,book.xsd中声明的目标名称空间要求是http://www.onlyone.org/book。


------------------------——————————————————--------------webx3-----------------------—————————--------———————————————————--------------

1.  扩展点(ConfigurationPoint):
在webx3中,允许框架的使用者对命名空间自行扩展,这些可以扩展的命名空间叫做扩展点,定义在classpath下的/META-INT/spring.configuration-points文件里

一个spring.configuration-points文件里可以包含多条扩展点定义的信息。

2. 捐献(Contribution)
有了扩展点,我们就可以在扩展点上进行扩展,这样的扩展在webx3称之为捐献。一个捐献包含一个xsd文件和一个BeanDefinitionParser。在webx3 Schema中,BeanDefinition最终表现为一个一个的捐献。
扩展点的捐献定义在classpath下的{configurationName}.bean-definition-parsers里面,其中configurationName是扩展点名称(其中“/”替换为“-”)。例如  services.bean-definition-parsers
services这个扩展点里面的一个捐献的定义:
person=com.alibaba.stonelab.webxsample.sample.helloworld.CustomServiceBeanDefinitionParser

其中:person 表示捐献的名称
com.alibaba.stonelab.webxsample.sample.helloworld.CustomServiceBeanDefinitionParser  表示对应的BeanDefinitionParser

xsd文件定义在classpath下的{configurationName}/{contributionName}.xsd文件里面。configurationName为扩展点的名称,contributionName为捐献的名称。

如图所示

------------------------------------------------------------------------------

person.xsd

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
    <xsd:element name="person">
        <xsd:complexType>
            <xsd:attribute name="msg" type="xsd:string" />
        </xsd:complexType>
    </xsd:element>
</xsd:schema>

CustomServiceBeanDefinitionParser.java

public class CustomServiceBeanDefinitionParser extends AbstractNamedBeanDefinitionParser<CustomService> {

    @Override
    protected void doParse(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) {
        SpringExtUtil.attributesToProperties(element, builder, "msg");
    }

    @Override
    protected String getDefaultName() {
        return "customService";
    }

}

代码下载地址:https://webx-schema.googlecode.com/svn/trunk/

抱歉!评论已关闭.