XML Getting Started (3) - Defining Document Content

xiaoxiao2021-03-06  18

Overview

To date, you have learned the basic rules of XML documents in this tutorial; that's of course, you need to define elements that will be used to represent data. You will understand two ways to do this chapter.

One method is to use document type definition, or refer to DTD. DTD definitions can appear in the XML document, the order of these elements, how can they nescence and other details of the XML document structure. DTD is part of the initial XML specification and is very similar to SGML DTD. Another method is to use XML Schema. Mode can define all document structures you can use in DTD, which can also define data types and more complex rules than DTD. The W3C has developed an XML Schema specification several years after the initial XML specification.

Document type definition

DTD allows you to specify the basic structure of the XML document. The following few pages will study the DTD fragment. The first is the DTD of the basic structure of the address document example in this chapter.

What is XML?

:

This DTD defines all elements used in the sample document. It defines three elements:

element contains a , a , a , one and one . All of these elements must appear and must appear in this order. element contains an optional element (question mark indicating that the element is optional), followed by <first-name> and a <Last-Name> element. All other elements containing text. (#Pcdata represents the parsed character data; the other element cannot be included in these elements.)</p> <p>Although DTD is quite simple, it clearly shows what kind of elementary combination is rules. <Postal-Code> Elements Before the <state> element, the address document is not ruled, and there is no rule of documentation without the <last-name> element.</p> <p>Also, please note that the DTD grammar is different from a normal XML syntax. (Instead, the XML Schema document itself is XML, which leads to some interesting results). Although the grammar of the DTD is different, you can still put a normal note into the DTD.</p> <p>Symbol in DTD</p> <p>Several symbols in the DTD are used to indicate how many elements may occur (or whether it appears) in the XML document. Here are some examples and its meaning:</p> <p><! Element Address (Name, City, State)> <address> element must include a <name> element, a <city> element, and a <state> element, and conforming to the above order. All elements are required. Commerity representation list. <! Element Name (Title?, First-name, last-name "> This means that <name> element contains an optional <title> element, there must be a <first-name> and a <Last-Name > Element. Question mark indicates that this is optional; it can appear once or not. <! Element AddressBook (Address )> <addressbook> element contains one or more <address> elements. You can have any number of <address> elements, but must have at least one. The plus sign indicates that this must have at least once, but it can appear once. <! Element private-addresses (address *)> <private-address> element contains zero or more <address> elements. The asterisk means that this can appear once, including zero. <! Element Name (middle?, First-name, (middle-initial | middle-name)?, Last-name)> <name> element contains an optional <title> element, followed by <first-name > Elements, then there may be a <middle-initial> or <middle-name> element, then follow the <last-name> element. In other words, <middle-initial> and <middle-name> are optional, and you can only choose one of both. Vertical line symbol indicates a list of selection; you can only select one from the list. Also note that this example is packet with parentheses and then places you next to this group. <! ELEMENT NAME ((Title?, First-name, last-name) | This <name> element can contain one of two sequences: one optional < Title>, followed by <first-name> and a <last-name>; or a <Surname>, a <mothers-name> and a <given-name>. Flexibility</p> <p>Before proceeding, there is a quick description of the type of XML document for flexibility. Consider the previous sample name and address document type; Obviously I have written this document for US postal address habits. If you want to define the DTD or mode of the rule of the other address type, you must add more complicated things to it. There is a <state> element that is meaningful in Australia, but it is meaningless in the UK. Perhaps</p> <p>Document type definition</p> <p>The sample DTD is dealt with Canada's address, but adding <province> elements. Finally, I have to know a lot of places in the world, the concept of the title, the name and surname.</p> <p>Key points: To define the structure of an XML document, you should consider DTD or mode in advance as designed in the application of database modes or data structures. The more future needs you have in advance, the easier it is, the easier it is, the lower the cost. Define properties</p> <p>This introductory tutorial has not studied how DTD work well in detail, but here is to introduce a more basic concept: define attributes. You can define the properties of the elements that will appear in the XML document. With DTD, you can also:</p> <p>Definition What attributes are the default defined attribute defined attributes list all valid values ​​for a given property</p> <p>Suppose you want to change the DTD to make State become the properties of the <city> element. How to do this below:</p> <p><! Element</p> <p>City (#pcdata)> <! Attlist City State CData #Required>, before we define the <city> element, but the modified example also uses the AttList declaration to list the properties of the elements. Names in the Properties list tells the parser These properties are defined for the <city> element. Name State is the name of the property, the keyword cdata and #Required tell the parser State property contains text and it is necessary (if it is optional, with CDATA)</p> <p>#Implied can be).</p> <p>To define multiple properties of the elements, write Attlist as follows:</p> <p><! Element</p> <p>City (#pcdata)> <! attlist city state cdata #Required Postal-code CData #Required></p> <p>This example defines both state and postal-code as the attribute of the <city> element.</p> <p>Finally, the DTD allows you to define the attribute default value and sufficient all valid values ​​for properties:</p> <p><! Element</p> <p>City (#pcdata)></p> <p><! Attlist City State CData (AZ | CA | NV | OR | UT | WA) "CA"></p> <p>The above example shows that it only supports address from Arizona (AZ), California (CA), Nevada (NV), Oregon (OR), Utah (UT) and Washington (WA), and default It is California. This way, you can perform a very limited data verification. Although this is a useful function, it is only a small subset of work that you can do with XML mode (see XML mode).</p> <p>XML mode</p> <p>With XML mode, you will have more capabilities to define what kind of XML document is valid. They have several advantages compared to DTD:</p> <p>XML mode uses XML syntax. In other words, the XML mode is an XML document. This means that you can work like any other document. For example, you can write an XSLT style sheet that converts the XML schema into a web form with automatically generated JavaScript code, where JavaScript code can verify the data you entered. XML mode supports data types. Although DTD does support data types, it is obvious that these data types are developed from the perspective of publishing. The XML mode supports all raw data types in the DTD (such as types such as identification and identity reference). They also support integers, floating point, date, time, string, URL, and other data types for data processing and verification. XML mode is scalable. In addition to the data types defined in the XML schema specification, you can also create your own data type and can send new data types based on other data types. The XML model has a stronger expression ability. For example, you can define any <state> attribute values ​​with an XML schema must not exceed 2 characters, or define any <postal-code> elements must be used with regular expressions [0-9] {5} (- [0-9 ] {4})? You can't do these things with DTD. Sample XML mode</p> <p>The following is an XML schema that matches the original name and address DTD. It adds two constraints: <state> elements must just be two characters long, <postal-code> elements must be used with regular expressions [0-9] {5} (- [0-9] { 4})? Although this mode is much longer than DTD, it is more clearly to express effective documentation looks like. Below is the model:</p> <p><? xml</p> <p>Version = "1.0" eNCoding = "UTF-8"?></p> <p><xsd: schema xmlns: xsd = "http://www.w3.org/2001/xmlschema"></p> <p><XSD: Element Name = "Address"></p> <p><XSD: ComplexType></p> <p><xsd: sequence></p> <p><xsd: element ref = "name" /></p> <p><xsd: element ref = "street" /></p> <p><XSD: Element Ref = "City" /></p> <p><xsd: element ref = "state" /></p> <p><xsd: element ref = "postal-code" /></p> <p></ xsd: sequence></p> <p></ xsd: complexType></p> <p></ xsd: element></p> <p><XSD: Element Name = "Name"></p> <p><XSD: ComplexType></p> <p><xsd: sequence></p> <p><xsd: element ref = "title" minoccurs = "0" /></p> <p><XSD: Element Ref = "first-name" /></p> <p><xsd: element ref = "last-name" /></p> <p></ xsd: sequence></p> <p></ xsd: complexType></p> <p></ xsd: element></p> <p><xsd: element name = "title" type = "xsd: string" /> <xsd: element name = "first-name" type = "xsd: string" /></p> <p><XSD: Element Name = "Last-Name" type = "xsd: String" /></p> <p><xsd: element name = "street" type = "xsd: string" /></p> <p><XSD: Element Name = "City" type = "xsd: string" /></p> <p><XSD: Element Name = "State"></p> <p><XSD: SimpleType></p> <p><xsd: restriction base = "xsd: string"></p> <p><xsd: longeth value = "2" /></p> <p></ xsd: restriction></p> <p></ xsd: SimpleType></p> <p></ xsd: element></p> <p><XSD: Element Name = "Postal-Code"></p> <p><XSD: SimpleType></p> <p><xsd: restriction base = "xsd: string"></p> <p><xsd: pattern value = "[0-9] {5} (- [0-9] {4}?" /></p> <p></ xsd: restriction></p> <p></ xsd: SimpleType></p> <p></ xsd: element></p> <p></ xsd: schema></p> <p>Elements in definition mode</p> <p>Some of the XML mode in the sample XML mode is defined in <xsd: element> element.</p> <p>XML element. The first two defined elements (<address> and <name>) consist of other elements. <xsd: sequence> element defines a sequence of elements contained in these two elements. Below is an example:</p> <p><XSD: Element</p> <p>Name = "address"></p> <p><XSD: ComplexType></p> <p><xsd: sequence></p> <p><xsd: element ref = "name" /></p> <p><xsd: element ref = "street" /></p> <p><XSD: Element Ref = "City" /></p> <p><xsd: element ref = "state" /></p> <p><xsd: element ref = "postal-code" /></p> <p></ xsd: sequence></p> <p></ xsd: complexType></p> <p></ xsd: element></p> <p>As with the DTD version, XML mode example definitions <address> contains <name>, a <street>, a <city>, one <state>, and a <postal-code> element, and in accordance with the order above. Note that this mode actually defines a new data type with the <XSD: ComplexType> element.</p> <p>Most elements contain texts; definitions them are simple. As long as you declare a new element, then specify its data type to XSD: String: <xsd: Element</p> <p>Name = "Title" type = "xsd: string" /></p> <p><XSD: Element Name = "first-name" type = "xsd: string" /></p> <p><XSD: Element Name = "Last-Name" type = "xsd: String" /></p> <p><xsd: element name = "street" type = "xsd: string" /></p> <p><XSD: Element Name = "City" type = "xsd: string" /></p> <p>Define element content in the mode</p> <p>The sample mode defines the content of the two elements. The content of the <state> element must be two characters long, and the content of the <postal-code> element must be with regular expressions [0-9] {5} (- [ 0-9] {4})? Match. The following demonstrates how to do that:</p> <p><XSD: Element</p> <p>Name = "state"></p> <p><XSD: SimpleType></p> <p><xsd: restriction base = "xsd: string"></p> <p><xsd: longeth value = "2" /></p> <p></ xsd: restriction></p> <p></ xsd: SimpleType></p> <p></ xsd: element></p> <p><XSD: Element Name = "Postal-Code"></p> <p><XSD: SimpleType></p> <p><xsd: restriction base = "xsd: string"></p> <p><xsd: pattern value = "[0-9] {5} (- [0-9] {4}?" /></p> <p></ xsd: restriction></p> <p></ xsd: SimpleType></p> <p></ xsd: element></p> <p>For the <State> and <postal-code> elements, the mode is defined for new data types with constraints. The first case uses the <xsd: length> element, while the second case uses the <XSD: Pattern> element to define the regular expression that the element must match.</p> <p>The introduction of the summary here is only the surface of the XML mode capabilities; this topic is discussed with all books. For this introductory article, we are enough to explain that XML mode is a very powerful and flexible way to describe what kind of XML documentation is effective.</p></div><div class="text-center mt-3 text-grey"> 转载请注明原文地址:https://www.9cbs.com/read-48939.html</div><div class="plugin d-flex justify-content-center mt-3"></div><hr><div class="row"><div class="col-lg-12 text-muted mt-2"><i class="icon-tags mr-2"></i><span class="badge border border-secondary mr-2"><h2 class="h6 mb-0 small"><a class="text-secondary" href="tag-2.html">9cbs</a></h2></span></div></div></div></div><div class="card card-postlist border-white shadow"><div class="card-body"><div class="card-title"><div class="d-flex justify-content-between"><div><b>New Post</b>(<span class="posts">0</span>) </div><div></div></div></div><ul class="postlist list-unstyled"> </ul></div></div><div class="d-none threadlist"><input type="checkbox" name="modtid" value="48939" checked /></div></div></div></div></div><footer class="text-muted small bg-dark py-4 mt-3" id="footer"><div class="container"><div class="row"><div class="col">CopyRight © 2020 All Rights Reserved </div><div class="col text-right">Processed: <b>0.045</b>, SQL: <b>9</b></div></div></div></footer><script src="./lang/en-us/lang.js?2.2.0"></script><script src="view/js/jquery.min.js?2.2.0"></script><script src="view/js/popper.min.js?2.2.0"></script><script src="view/js/bootstrap.min.js?2.2.0"></script><script src="view/js/xiuno.js?2.2.0"></script><script src="view/js/bootstrap-plugin.js?2.2.0"></script><script src="view/js/async.min.js?2.2.0"></script><script src="view/js/form.js?2.2.0"></script><script> var debug = DEBUG = 0; var url_rewrite_on = 1; var url_path = './'; var forumarr = {"1":"Tech"}; var fid = 1; var uid = 0; var gid = 0; xn.options.water_image_url = 'view/img/water-small.png'; </script><script src="view/js/wellcms.js?2.2.0"></script><a class="scroll-to-top rounded" href="javascript:void(0);"><i class="icon-angle-up"></i></a><a class="scroll-to-bottom rounded" href="javascript:void(0);" style="display: inline;"><i class="icon-angle-down"></i></a></body></html><script> var forum_url = 'list-1.html'; var safe_token = 'BNxifglsZxrwu10DFtfU_2FMe0L_2FhDUtz6aJvgJj08vT0hMjG4QT0rfR3Mb1AieYXdzM8iZUPtJLaOuXuakiTNzA_3D_3D'; var body = $('body'); body.on('submit', '#form', function() { var jthis = $(this); var jsubmit = jthis.find('#submit'); jthis.reset(); jsubmit.button('loading'); var postdata = jthis.serializeObject(); $.xpost(jthis.attr('action'), postdata, function(code, message) { if(code == 0) { location.reload(); } else { $.alert(message); jsubmit.button('reset'); } }); return false; }); function resize_image() { var jmessagelist = $('div.message'); var first_width = jmessagelist.width(); jmessagelist.each(function() { var jdiv = $(this); var maxwidth = jdiv.attr('isfirst') ? first_width : jdiv.width(); var jmessage_width = Math.min(jdiv.width(), maxwidth); jdiv.find('img, embed, iframe, video').each(function() { var jimg = $(this); var img_width = this.org_width; var img_height = this.org_height; if(!img_width) { var img_width = jimg.attr('width'); var img_height = jimg.attr('height'); this.org_width = img_width; this.org_height = img_height; } if(img_width > jmessage_width) { if(this.tagName == 'IMG') { jimg.width(jmessage_width); jimg.css('height', 'auto'); jimg.css('cursor', 'pointer'); jimg.on('click', function() { }); } else { jimg.width(jmessage_width); var height = (img_height / img_width) * jimg.width(); jimg.height(height); } } }); }); } function resize_table() { $('div.message').each(function() { var jdiv = $(this); jdiv.find('table').addClass('table').wrap('<div class="table-responsive"></div>'); }); } $(function() { resize_image(); resize_table(); $(window).on('resize', resize_image); }); var jmessage = $('#message'); jmessage.on('focus', function() {if(jmessage.t) { clearTimeout(jmessage.t); jmessage.t = null; } jmessage.css('height', '6rem'); }); jmessage.on('blur', function() {jmessage.t = setTimeout(function() { jmessage.css('height', '2.5rem');}, 1000); }); $('#nav li[data-active="fid-1"]').addClass('active'); </script>