XML Format"

From Documentation
m (correct highlight (via JWB))
 
(2 intermediate revisions by 2 users not shown)
Line 1: Line 1:
{{ZKDevelopersGuidePageHeader}}
 
 
 
{{ZKDevelopersGuidePageHeader}}
 
{{ZKDevelopersGuidePageHeader}}
  
Line 78: Line 76:
  
 
== Special Character Must Be Replaced ==
 
== Special Character Must Be Replaced ==
XML use <tt><''element-name''></tt> to denote an element, so you have to replace special characters. For example, you have to use <tt>&amp;lt;</tt> to represent the <tt><</tt> character.
+
XML use <code><''element-name''></code> to denote an element, so you have to replace special characters. For example, you have to use <code>&amp;lt;</code> to represent the <code><</code> character.
  
  
Line 115: Line 113:
 
| Required only if use it in a XML attribute's value
 
| Required only if use it in a XML attribute's value
 
|}
 
|}
Alternatively, you could tell XML parser not to interpret a piece of text by using <tt>CDATA</tt> as the following.
+
Alternatively, you could tell XML parser not to interpret a piece of text by using <code>CDATA</code> as the following.
  
 
<source lang="xml" >
 
<source lang="xml" >
Line 128: Line 126:
 
</source>
 
</source>
 
   
 
   
It is suggested to always add <tt><![CDATA[    ]]></tt> inside your <tt><zscript> </zscript></tt>. Thus you don't have to worry about escape sequences for special characters like "&", "<". In addition, the code is much easier to read and maintain.
+
It is suggested to always add <code><![CDATA[    ]]></code> inside your <code><zscript> </zscript></code>. Thus you don't have to worry about escape sequences for special characters like "&", "<". In addition, the code is much easier to read and maintain.
  
 
== Attribute Values Must Be Specified and Quoted ==
 
== Attribute Values Must Be Specified and Quoted ==
Line 152: Line 150:
 
|}
 
|}
 
== Comments ==
 
== Comments ==
A comment is used to leave a note or to temporarily disable a block of XML code. To add a comment in XML, use <tt><nowiki><!--</nowiki></tt> and <tt><nowiki>--></nowiki></tt> to mark the comment body.
+
A comment is used to leave a note or to temporarily disable a block of XML code. To add a comment in XML, use <code><nowiki><!--</nowiki></code> and <code><nowiki>--></nowiki></code> to mark the comment body.
  
 
<source lang="xml" >
 
<source lang="xml" >
Line 177: Line 175:
 
<button label="click2" onClick="alert(&quot;hello)&quot;">
 
<button label="click2" onClick="alert(&quot;hello)&quot;">
 
</source>
 
</source>
 
{{ ZKDevelopersGuidePageFooter}}
 
 
  
 
{{ ZKDevelopersGuidePageFooter}}
 
{{ ZKDevelopersGuidePageFooter}}

Latest revision as of 10:36, 19 January 2022

Stop.png This documentation is for an older version of ZK. For the latest one, please click here.


Overview

This section provides the most basic concepts of XML to work with ZK. If you are familiar with XML, you could skip this section. If you want to learn more, there are a lot of resources on Internet, such as http://www.w3schools.com/xml/xml_whatis.asp and http://www.xml.com/pub/a/98/10/guide0.html.

XML is a markup language much like HTML but with stricter and cleaner syntax. It has several characteristics worth to notice.

Elements Must Be Well-formed

First, each element must be closed. They are two ways to close an element as depicted below. They are equivalent.


Description
Code
Close by an end tag:
<window></window>
Close without an end tag:
<window/>


Second, elements must be properly nested.


Result
Code
Correct:
<window>
	<groupbox>
		Hello World!
	</groupbox>
</window>
Wrong:
<window>
	<groupbox>
		Hello World!
	</window>
</groupbox>

XML treats every tag as a node in a tree. A node without a parent node is a root component, and it is the root of a tree. In each zul file, only ONE tree is allowed.

For example, for being a whole zul file, the following is allowed, for it has only one root component.

<button/>

And for being a whole zul file, the following is not allowed, for it has more than one root component.

<button/>
<button/>

You can solve the problem by simply adding a tag enclosing the whole zul file to serve as the parent node, so the zul file has one single tree again.

<window>
	<button />
	<button />
</window>

Special Character Must Be Replaced

XML use <element-name> to denote an element, so you have to replace special characters. For example, you have to use &lt; to represent the < character.


Special Character
Replaced With
<
&lt;
>
&gt;
&
&amp;
"
&quot;
'
&apos;
\t (TAB)
&#x09;
Required only if use it in a XML attribute's value
\n (Linefeed)
&#x0a;
Required only if use it in a XML attribute's value

Alternatively, you could tell XML parser not to interpret a piece of text by using CDATA as the following.

 <zscript>
 <![CDATA[
 void myfunc(int a, int b) {
     if (a < 0 && b > 0) {
         //do something
     }
 ]]>
 </zscript>

It is suggested to always add <![CDATA[ ]]> inside your <zscript> </zscript>. Thus you don't have to worry about escape sequences for special characters like "&", "<". In addition, the code is much easier to read and maintain.

Attribute Values Must Be Specified and Quoted

Result
Code
Correct:
  
width="100%"
checked="true"
Wrong:
  
width=100%
checked

Comments

A comment is used to leave a note or to temporarily disable a block of XML code. To add a comment in XML, use <!-- and --> to mark the comment body.

 <window>
 <!-- this is a comment and ignored by ZK -->
 </window>

Character Encoding

It is, though optional, a good idea to specify the encoding in your XML such that the XML parser can interpret it correctly. Note: it must be on the first line of the file.

 <?xml version="1.0" encoding="UTF-8"?>

In addition to specify the correct encoding, you have to make sure your XML editor supports it as well.

Quiz

Fix the following code, make it runnable.

<!- Typo is common, this is a comment ->
<button label=click onClick="alert("hello")"/>
<button label="click2" onClick="alert(&quot;hello)&quot;">



Last Update : 2022/01/19

Copyright © Potix Corporation. This article is licensed under GNU Free Documentation License.