私はXSD
を学ぶのが初めてです。私はXSDファイルとXMLファイルとXSDに対してXMLを検証するプログラムを書いた。私がプログラムを実行すると、Invalid content was found starting with element 'id'. One of '{id}' is expected
というエラーが表示されます。誰かが私が間違っているところを説明してください。xsdに対してxmlを検証できません
XSDファイル:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.edureka.co/Student"
xmlns:st="http://www.edureka.co/Student"
>
<xs:element name="student" type="st:student"></xs:element>
<xs:complexType name="student">
<xs:sequence>
<xs:element name="id" type="xs:int"/>
<xs:element name="name" type="xs:string"/>
<xs:element name="language" type="xs:string"/>
<xs:element name="expertise" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
とXMLファイル:
<?xml version="1.0" encoding="UTF-8"?>
<student xmlns="http://www.edureka.co/Student"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.edureka.co/Student studentRule.xsd"
>
<id>1234</id>
<name>Pradeep</name>
<language>Sanskrit</language>
<expertise>Beginner</expertise>
</student>
XMLファイル検証するJavaプログラム:
import java.io.File;
import java.io.IOException;
import javax.xml.XMLConstants;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Validator;
import org.xml.sax.SAXException;
public class ValidatorDemo {
public static void main(String[] args) {
System.out.println("student.xml validates against studentRule.xsd? "+validateXMLSchema("studentRule.xsd", "student.xml"));
}
public static boolean validateXMLSchema(String xsdPath, String xmlPath){
try {
SchemaFactory factory =
SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema schema = factory.newSchema(new File(xsdPath));
Validator validator = schema.newValidator();
validator.validate(new StreamSource(new File(xmlPath)));
} catch (IOException | SAXException e) {
System.out.println("Exception: "+e.getMessage());
return false;
}
return true;
}
}
ご回答いただきありがとうございます。 'unqualified'を使用している場合、私のXML文書はどのように見えますか? – zilcuanu
元のドキュメントは「修飾」形式です。答えのドキュメントは「非修飾」形式です(グローバル要素「student」で使用される接頭辞、ローカル要素に接頭辞がないこと、デフォルトがないことに注意してください)名前空間)。私はいくつかのさらなる説明で答えを更新しました。 – teppic