<script type="text/javascript">function StorePage(){d=document;t=d.selection?(d.selection.type!='None'?d.selection.createRange().text:''):(d.getSelection?d.getSelection():'');void(keyit=window.open('http://www.365key.com/storeit.aspx?t='+escape(d.title)+'&u='+escape(d.location.href)+'&c='+escape(t),'keyit','scrollbars=no,width=475,height=575,left=75,top=20,status=no,resizable=yes'));keyit.focus();}</script> 其中我的XML文件为:
<?
xml version="1.0" encoding="UTF-8"
?>
< birthdate >
< month > January </ month >
< day > 21 </ day >
< year > 1983 </ year >
< name > aaa </ name >
</ birthdate >
< birthdate >
< month > January </ month >
< day > 21 </ day >
< year > 1983 </ year >
< name > aaa </ name >
</ birthdate >
对应的XSD文件为:
<?
xml version="1.0" encoding="UTF-8"
?>
< xs:schema xmlns:xs ="http://www.w3.org/2001/XMLSchema" >
< xs:element name ="birthdate" >
< xs:complexType >
< xs:sequence >
< xs:element name ="month" type ="xs:string" />
< xs:element name ="day" type ="xs:int" />
< xs:element name ="year" type ="xs:int" />
< xs:any minOccurs ="0" />
</ xs:sequence >
</ xs:complexType >
</ xs:element >
</ xs:schema >
< xs:schema xmlns:xs ="http://www.w3.org/2001/XMLSchema" >
< xs:element name ="birthdate" >
< xs:complexType >
< xs:sequence >
< xs:element name ="month" type ="xs:string" />
< xs:element name ="day" type ="xs:int" />
< xs:element name ="year" type ="xs:int" />
< xs:any minOccurs ="0" />
</ xs:sequence >
</ xs:complexType >
</ xs:element >
</ xs:schema >
我写的java程序为:
public
class
XMLParse
...
{
public static void main(String[] args)
throws SAXException, IOException, ParserConfigurationException ...{
SchemaFactory factory
= SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
File schemaLocation = new File("D:/tmp/test.xsd");
File xmlFile = new File("D:/tmp/test.xml");
Schema schema = factory.newSchema(schemaLocation);
Validator validator = schema.newValidator();
DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
domFactory.setNamespaceAware(true);
DocumentBuilder builder = domFactory.newDocumentBuilder();
Document doc = builder.parse(xmlFile);
DOMSource source = new DOMSource(doc);
DOMResult result = new DOMResult();
try ...{
validator.validate(source, result);
Document augmented = (Document) result.getNode();
}
catch (SAXException ex) ...{
System.out.println(ex.getMessage());
}
}
}
public static void main(String[] args)
throws SAXException, IOException, ParserConfigurationException ...{
SchemaFactory factory
= SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
File schemaLocation = new File("D:/tmp/test.xsd");
File xmlFile = new File("D:/tmp/test.xml");
Schema schema = factory.newSchema(schemaLocation);
Validator validator = schema.newValidator();
DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
domFactory.setNamespaceAware(true);
DocumentBuilder builder = domFactory.newDocumentBuilder();
Document doc = builder.parse(xmlFile);
DOMSource source = new DOMSource(doc);
DOMResult result = new DOMResult();
try ...{
validator.validate(source, result);
Document augmented = (Document) result.getNode();
}
catch (SAXException ex) ...{
System.out.println(ex.getMessage());
}
}
}
在执行的时候,总是提示:cvc-complex-type.2.4.c:严格输入了匹配通配符,但还是找不到元素“name”的声明。
但是我在XSD中,已经声明了一个 <xs:any minOccurs="0"/>的任意类型,支持扩展,但是确总是不正确.
后了解到,xsd中any虽然是便于xml文件的扩展,但是也是有条件的扩展,所扩展的内容需要由非本身的一个xsd文件来描述,而xml文件中的<xs:any>部分,也必须符合子xsd的格式限制.
故修改以上的xsd文件为:
<?
xml version="1.0" encoding="UTF-8"
?>
< xs:schema xmlns:xs ="http://www.w3.org/2001/XMLSchema" >
< xs:include schemaLocation ="D:/tmp/name.xsd" />
< xs:element name ="birthdate" >
< xs:complexType >
< xs:sequence >
< xs:element name ="month" type ="xs:string" />
< xs:element name ="day" type ="xs:int" />
< xs:element name ="year" type ="xs:int" />
< xs:any minOccurs ="0" />
</ xs:sequence >
</ xs:complexType >
</ xs:element >
</ xs:schema >
< xs:schema xmlns:xs ="http://www.w3.org/2001/XMLSchema" >
< xs:include schemaLocation ="D:/tmp/name.xsd" />
< xs:element name ="birthdate" >
< xs:complexType >
< xs:sequence >
< xs:element name ="month" type ="xs:string" />
< xs:element name ="day" type ="xs:int" />
< xs:element name ="year" type ="xs:int" />
< xs:any minOccurs ="0" />
</ xs:sequence >
</ xs:complexType >
</ xs:element >
</ xs:schema >
其中d:/tmp/name.xsd文件为对xml文件中的<name>元素进行的限定:
<?
xml version="1.0" encoding="UTF-8"
?>
< xs:schema xmlns:xs ="http://www.w3.org/2001/XMLSchema" >
< xs:element name ="name" type ="xs:string" />
</ xs:schema >
< xs:schema xmlns:xs ="http://www.w3.org/2001/XMLSchema" >
< xs:element name ="name" type ="xs:string" />
</ xs:schema >
进行这样的修改后,对于以上的xml文件的xsd校验就成功了.