xml元素
一:什么是xml元素属性 XML 元素指的是从(且包括)开始标签直到(且包括)结束标签的部分。 元素可包含其他元素、文本或者两者的混合物。元素也可以拥有属性。 <bookstore> <book category="CHILDREN"> <title>Harry Potter</title> <author>J K. Rowling</author> <year>2005</year> <price>29.99</price> </book> <book category="WEB"> <title>Learning XML</title> <author>Erik T. Ray</author> <year>2003</year> <price>39.95</price> </book> </bookstore> 在上例中,<bookstore>和 <book> 都拥有元素内容,因为它们包含了其他元素。<author> 只有文本内容,因为它仅包含文本。 在上例中,只有<book> 元素拥有属性(category="CHILDREN")。
二:XML 命名规则 XML 元素必须遵循以下命名规则: 名称可以含字母、数字以及其他的字符 名称不能以数字或者标点符号开始 名称不能以字符“xml”(或者 XML、Xml)开始 名称不能包含空格 可使用任何名称,没有保留的字词。
三:xml的可扩展性 <note> <to>George</to> <from>John</from> <body>Don't forget themeeting!</body> </note> 创建了一个应用程序,可将 <to>、<from> 以及 <body> 元素提取出来,并产生以下的输出: MESSAGE To: George From: John Don't forget the meeting! 这个 XML 文档又向这个文档添加了一些额外的信息: <note> <date>2008-08-08</date> <to>George</to> <from>John</from> <heading>Reminder</heading> <body>Don't forget themeeting!</body> </note> 这个应用程序仍然可以找到 XML 文档中的 <to>、<from> 以及 <body> 元素,并产生同样的输出。 XML 的优势之一,就是可以经常在不中断应用程序的情况进行扩展。 四:xml的元素属性 <file type="gif">computer.gif</file> 1.属性必须使用单引号或者双引号 2. 如果属性值本身包含双引号,那么有必要使用单引号包围它,就像这个例子: <gangster name='George"Shotgun" Ziegler'> 或者可以使用实体引用: <gangster name="George"Shotgun" Ziegler"> 五.xml元素和属性pk 第一个例子中使用了 date 属性: <notedate="08/08/2008"> <to>George</to> <from>John</from> <heading>Reminder</heading> <body>Don't forget themeeting!</body> </note> 第二个例子中使用了 date 元素: <note> <date>08/08/2008</date> <to>George</to> <from>John</from> <heading>Reminder</heading> <body>Don't forget themeeting!</body> </note> 第三个例子中使用了扩展的 date 元素(最佳选择) <note> <date> <day>08</day> <month>08</month> <year>2008</year> </date> <to>George</to> <from>John</from> <heading>Reminder</heading> <body>Don't forget themeeting!</body> </note> 六:针对元数据的 XML 属性 有时候会向元素分配 ID 引用。这些 ID 索引可用于标识 XML 元素,它起作用的方式与 HTML 中 ID 属性是一样的。这个例子向我们演示了这种情况: <messages> <note id="501"> <to>George</to> <from>John</from> <heading>Reminder</heading> <body>Don't forget the meeting!</body> </note> <note id="502"> <to>John</to> <from>George</from> <heading>Re: Reminder</heading> <body>I will not</body> </note> </messages> 上面的 ID 仅仅是一个标识符,用于标识不同的便签。它并不是便签数据的组成部分。 在此我们极力向您传递的理念是:元数据(有关数据的数据)应当存储为属性,而数据本身应当存储为元素。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |