"XML Programming and Application Tutorial" Chapter 2 Exercise Answers

1. Fill in the blanks

  1. Every XML document is divided into two parts: preamble and document elements .
  2. <?xml version="1.0" encoding="gb2312"?> is an **XML document declaration**.
  3. The main part of the contents of XML documents, usually by the root element , child elements , attributes , comments, and content.
  4. It represents an element start delimiter is called a start tag , which represents an element ending delimiter is called end mark .
  5. There are four types of XML document elements are empty element , the element contains only text -only element child element , the element containing child elements, text or mixed elements.

Two, multiple choice questions

  1. The attribute () is used to indicate the character set used in the XML document.
    A. version B. encoding C. standalone

  2. XML() provides a way to avoid element naming conflicts.
    A. Namespace B. DTD C. XSD D. XSL

  3. Which of the following code describes an empty element? ( C )

A.
<title>gone with the wind</title>
<format>movie</format>
<genre>classic</genre>
B.
<DVD id="1">
<title>gone with the wind</title>
<format>movie</format>
• 40 • XML 编程与应用教程(第 2 版)
<genre>classic</genre>
</DVD>
C.
 <book/>
  1. In an XML document containing Chinese characters, the encoding attribute value should be set to ().
    A. BIG5 B. GB2312 C. UTF-8

  2. Entity reference is a legal XML name, preceded by a symbol ().
    A. & B.; C. +

Three, short answer questions

  1. Write rules for well-formed XML documents.
    Rule 1: There must be a declaration statement; Rule 2: Pay attention to capitalization; Rule 3: All MML documents must have and only one root element; Rule 4: Attribute values ​​must use quotation marks ""; Rule 5: All tags must have corresponding Rule 6: All empty tags must also be closed; Rule 7: Tags must be nested correctly; Rule 8: Handling blank characters; Rule 9: Handling special characters.

  2. What is the naming convention for XML elements?
    In XML, there are basically no reserved words, so we can use any word as the element name as we want, but ⅪThe naming of the element must comply with the following specifications: 1. The name of the element can contain characters, numbers and other characters.
    2. The name of the element cannot start with numbers or punctuation. 3. The name of the element cannot start with XML (or xml, Xml, xMl...).
    4. The name of the element cannot contain spaces. 5. Try to avoid using "-" and "." as it may cause confusion.
    6. The naming of elements should follow the principle of simple and easy to read, for example: <book_title> is a good name, while <the_title_of_the_book> is verbose.
    7. XML documents often correspond to data tables. We should try our best to keep the naming of the fields in the database consistent with the naming of the elements in the corresponding ⅫM document, so as to facilitate data transformation.
    8. Non-English/character/string can also be used as the name of the ⅪM element, such as <song>, <article>, etc., which are completely legal names. However, some software does not support this kind of naming well, so we'd better use English letters for naming. 9. Don't use ":" in ⅫM element naming, because Ⅺ namespace needs to use this very special.

Four, computer problems

  1. Imagine such a book.
    Title: XML Guide
    Chapter One Introduction to XML
    Section 1.1 What is HTML
    Section 1.2 What is XML
    Chapter Two XML Syntax
    Section 2.1 XML elements must have closing tags
    Section 2.2 XML elements must be nested correctly
    Try to use XML documents for description, And realized on the machine.
<?xml version="1.0" encoding="UTF-8"?>
<book>
	<title>XML指南</title>
	<chapter>XML入门简介
		<para>什么是HTML</para>
		<para>什么是XML</para>
	</chapter>
	<chapter>XML语法
		<para>XML元素必须有结束标签</para>
		<para>XML元素必须正确地嵌套</para>
	</chapter>
</book>
  1. Create a well-formed XML document to store employee information, including: employee ID (attribute), name (element), age (element), sex (element), address (element). Realize on the computer and view it in the browser.
<?xml version="1.0" encoding="UTF-8"?>
<employees>
	<employee>
		<id>1</id>
		<name>张三</name>
		<age>32</age>
		<sex>男</sex>
		<address>上海</address>
	</employee>

	<employee>
		<id>2</id>
		<name>李四</name>
		<age>22</age>
		<sex>男</sex>
		<address>北京</address>
	</employee>
</employees>

Insert picture description here

  1. Create a well-formed XML document to store the student's score information, including: student ID number (attribute), name (element), and score (element). Realize on the computer and view it in the browser.
<?xml version="1.0" encoding="UTF-8"?>
<students>
	<student>
		<number>1</number>
		<name>tom</name>
		<socre>100</socre>
	</student>
	<student>
		<number>2</number>
		<name>mary</name>
		<socre>98</socre>
	</student>
</students>

Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_44895666/article/details/108684876