1) Do you have practical experience working with XML? 2) Create an XML structure for task information (similar to JSON). 3) Explain in your own words what XSD is. 4) How would you validate an XML document against an XSD schema? 5) What is the difference between Simple Type and Complex Type in XSD?
sobes.tech AI
Answer from AI
-
Yes, I have practical experience working with XML, including creating, parsing, and validating documents.
-
An example XML structure for task information:
<Task>
<Id>123</Id>
<Title>Implement function</Title>
<Description>Task description</Description>
<Priority>High</Priority>
<DueDate>2024-07-01</DueDate>
<Assignee>Ivan Ivanov</Assignee>
</Task>
-
XSD (XML Schema Definition) is a language for describing the structure of XML documents. Using XSD, you can define which elements and attributes are allowed, their data types, order, whether they are mandatory, and constraints. This allows validating XML documents against a specified schema.
-
To check XML against an XSD, parsers with validation support are typically used. For example, in Java, you can use
javax.xml.validation.SchemaFactoryandValidator:
SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema schema = factory.newSchema(new File("schema.xsd"));
Validator validator = schema.newValidator();
validator.validate(new StreamSource(new File("document.xml")));
If the document does not conform to the schema, an exception will be thrown.
- Differences between Simple Type and Complex Type in XSD:
-
Simple Type describes elements that contain only text data without nested elements or attributes. For example, a string, number, or date.
-
Complex Type describes elements that can contain nested elements and/or attributes. These are more complex structures.
Example:
<xs:element name="Priority" type="xs:string"/> <!-- Simple Type -->
<xs:complexType name="TaskType">
<xs:sequence>
<xs:element name="Title" type="xs:string"/>
<xs:element name="Description" type="xs:string"/>
</xs:sequence>
<xs:attribute name="id" type="xs:int"/>
</xs:complexType> <!-- Complex Type -->