根據Schema寫出xsd xml限制文檔來,難點在于xml檔案如何引入schema限制,下面分享一個例子
<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.itheima.com/book"
elementFormDefault="qualified">
<element name='書架'>
<complexType>
<sequence maxOccurs='unbounded'>
<element name='書'>
<complexType>
<sequence>
<element name='書名' type='string' />
<element name='作者' type='string' />
<element name='售價' type='string' />
</sequence>
</complexType>
</element>
</sequence>
</complexType>
</element>
</schema>
xml中引入schema限制的步驟:
1、檢視schema文檔,找到根元素,在xml中寫出來
<?xml version="1.0" encoding="UTF-8"?>
<書架>
</書架>
2、根元素來自哪個名稱空間。使用xmlns指令來聲明
名稱空間是在schema中定義的,就是targetNamespace的值
<?xml version="1.0" encoding="UTF-8"?>
<itheima:書架 xmlns:itheima="http://www.itheima.com/book">
</itheima:書架>
3、引入的名稱空間和哪個xsd檔案對應?
使用schemaLocation來指定:兩個取值:第一個為名稱空間 第二個為xsd檔案的路徑
<?xml version="1.0" encoding="UTF-8"?>
<itheima:書架 xmlns:itheima="http://www.itheima.com/book"
schemaLocation="http://www.itheima.com/book book.xsd">
</itheima:書架>
4、schemaLocation哪裡來的?它來自一個标準的名稱空間
<?xml version="1.0" encoding="UTF-8"?>
<itheima:書架 xmlns:itheima="http://www.itheima.com/book"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.itheima.com/book book.xsd">
</itheima:書架>
5、隻要以上4部搞好了,對于子标簽eclipse就有提示了
右擊xml—–eclipse——Run Validation
1、xsd限制文檔
2、xsd執行個體對象
3、xsd執行個體對象裡面的限制引用規則
(1)名稱空間:一般是域名
(2)xsd檔案路徑:xsd檔案路徑是和空間名稱對應的,xsd檔案路徑包含空間名稱和xsd存放的路徑;
(3)schemalocation:xsd檔案的标準路徑