下面的schema为撒不起到验证作用,尤其是unique那块???

allen1981813 2004-08-02 03:19:39
....................................................................
schema代码如下
.....................................................................

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

<!--Define the type of company name which base on string -->
<xs:simpleType name="ComNameType">
<xs:restriction base="xs:string"/>
</xs:simpleType>

<!--Define the type of company CEO which base on string maybe complex in the furture-->
<xs:simpleType name="ComCEOType">
<xs:restriction base="xs:string"/>
</xs:simpleType>

<!--Define a company structor which contain name of it and a CEO-->
<xs:complexType name="CompanyType">
<xs:sequence>
<xs:element name="Name" type="ComNameType"/>
<xs:element name="CEO" type="ComCEOType"/>
</xs:sequence>
<xs:attribute name="PID" type="xs:string"/>
</xs:complexType>

<!--Combin the companies -->
<xs:complexType name="CompaniesType">
<xs:sequence maxOccurs="unbounded">
<xs:element name="Company" type="CompanyType">
<xs:unique name="CompanyID">
<xs:selector xpath="Companies/Company"/>
<xs:field xpath="@PID"/>
</xs:unique>
</xs:element>
</xs:sequence>
</xs:complexType>

<!--Initialize root element-->
<xs:element name="Companies" type="CompaniesType"/>
</xs:schema>

..........................................................
xml代码如下
..........................................................

<?xml version="1.0" encoding="UTF-8"?>
<Companies xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="D:\MyCompany\CompanySch.xsd">
<Company PID="c02">
<Name>Microsoft</Name>
<CEO>Bill Gates</CEO>
</Company>

<Company PID="c02">
<Name>TCL</Name>
<CEO>ShiHong Wu</CEO>
</Company>

<Company PID="c02">
<Name>SUN</Name>
<CEO>James Gosling</CEO>
</Company>
</Companies>


...全文
101 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
allen1981813 2004-08-03
  • 打赏
  • 举报
回复
3 x up
zzzz76 2004-08-03
  • 打赏
  • 举报
回复
Yeah, I looked at the book, it is a sample, so I think your code is wrong with Attribute unique, I copy this sample of book, I think you will clear~~ =)

<xs:element name="customer">
<xs:complexType>
......
<xs:attribute name="customerID" type="xs:ID"/>
<xs:unique name="uniqueCustomer">
<xs:selector xpath="tc:customer"/>
<xs:field xpath="@customerID"/>
</xs:unique>
......
</xs:complexType>
</xs:element>

pls test your code again
allen1981813 2004-08-03
  • 打赏
  • 举报
回复
我把schema改了改,也改动了楼上说的地方,
但是还是不可以,重复出现id的时候似乎验证没起作用,顺利通过了
..................................................................................
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:complexType name="CompanyType">
<xs:sequence>
<xs:element name="Name">
<xs:simpleType name="ComNameType">
<xs:restriction base="xs:string"/>
</xs:simpleType>
</xs:element>
<xs:element name="CEO">
<xs:simpleType name="ComCEOType">
<xs:restriction base="xs:string"/>
</xs:simpleType>
</xs:element>
</xs:sequence>
<xs:attribute name="PID" type="xs:string"/>
</xs:complexType>
<!--Combin the companies -->
<xs:complexType name="CompaniesType">
<xs:sequence maxOccurs="unbounded">
<xs:element name="Company" type="CompanyType">
<xs:unique name="CompanyID">
<xs:selector xpath="."/>
<xs:field xpath="@PID"/>
</xs:unique>
<!--<xs:key name="IDKey">
<xs:selector xpath="Companies/Company" />
<xs:field xpath="@PID" />
</xs:key>-->
</xs:element>
</xs:sequence>
</xs:complexType>
<!--Initialize root element-->
<xs:element name="Companies" type="CompaniesType"/>
</xs:schema>
zzzz76 2004-08-03
  • 打赏
  • 举报
回复
<xs:selector xpath="Companies/Company"/>
是不是这里应该不用写全路径阿?

写成<xs:selector xpath="."/>

就可以了?因为你上面不是已经定到element为Company了么?

大家讨论

8,906

社区成员

发帖
与我相关
我的任务
社区描述
XML/XSL相关问题讨论专区
社区管理员
  • XML/XSL社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧