XML——合法的文档构建XSD

XML——合法的文档构建XSD

前言

  我们看一下我们经常在哪里使用过XSD,我们web项目中的web.xml和spring的配置文件经常能够看到其存在,下面是spirng配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"      
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd  
           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

介绍

  上一篇介绍了如何通过DTD实现xml文件的文档构建,XSD是什么呢? 它跟DTD的区别又是什么呢?
  XSD(xml schema definition)是通过xml编写的,作用是用来描述和定义XML文档结构。
  它比DTD更完善,功能更强大,并且支持扩展。XML Schema 最重要的能力之一就是对数据类型的支持。

实现XSD

编写xsd文件
<?xml version="1.0" encoding="utf-8" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
           targetNamespace="http://www.chen.com"
           xmlns="http://www.chen.com"
           elementFormDefault="qualified">
    <!--xmlns引用命名空间 xs为前缀-->
    <!-- targetNamespace定义元素命名空间 -->
    <!-- elementFormDefault指出任何 XML文档使用该xsd的必须在schema 中声明命名空间限定 -->

    <xs:element name="root">
        <!--添加文档注释-->
        <xs:annotation>
            <xs:documentation>这是根节点</xs:documentation>
        </xs:annotation>
        <!--复合类型-->
        <xs:complexType>
            <xs:sequence><!--xsd指示器以指定的顺序声明元素-->
                <xs:element name="node1">
                    <xs:complexType>
                        <!--只有复合类型才有属性-->
                        <xs:attribute name="id" type="xs:int" use="required"/>
                    </xs:complexType>
                </xs:element>
                <xs:element name="node2">
                    <xs:simpleType>
                        <!--对值进行限定0-100-->
                        <xs:restriction base="xs:int">
                            <xs:minInclusive value="0"/>
                            <xs:maxInclusive value="100"/>
                        </xs:restriction>
                    </xs:simpleType>
                </xs:element>
                <xs:element name="node3" type="property">

                </xs:element>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
    <!--公共的元素内容限定-->
    <xs:simpleType name="property">
        <xs:restriction base="xs:string">
            <xs:whiteSpace value="replace"/>
        </xs:restriction>
    </xs:simpleType>
</xs:schema>
编写xml文件
<?xml version="1.0" encoding="UTF-8" ?>
<root xmlns="http://www.chen.com"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://www.chen.com /xml/xsd/my.xsd">
    <node1 id="1"/>
    <node2>1</node2>
    <node3>2019-11-26</node3>
</root>

后言

这里讲了基本的使用,xml schema的东西还有很多,可以去菜鸟教程上继续深入

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

陈大侠在江湖

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值