1). What is a document type definition and what is its purpose in XML? Explain the difference between a well-formed and a valid XML document.
Answer: The purpose of a DTD is to define the legal building blocks of an XML document. It defines the document structure with a list of legal elements. A DTD can be declared inline in your XML document, or as an external reference. Two definitions:
-).A well-formed file is one that obeys the general XML rules for tags: tags must be properly nested, opening and closing tags must be balanced, and empty tags must end with '/>'
.
-). A valid file is not only well-formed, but it must also conform to a publicly available DTD that specifies which tags it uses, what attributes those tags can contain, and which tags can occur inside which other tags, among other properties.
2). External DTD:
<?xml version="1.0"?>
<!DOCTYPE note SYSTEM "note.dtd">
3). Minimal but Complete XSLT Stylesheet
<?xml version="1.0"?>
<
xsl:stylesheet
version="1.0"
xmlns:xsl="http://www.w3.org/1999/
XSL/Transform">
<
/xsl:stylesheet
>
4). Using XSLT, how would you extract a specfic attribute from an element in an XML document?
Ans: Extract attributes from XML data
<xsl:template match="element-name">
Attribute Value:
<xsl:value-of select="@attribute" />
<xsl:apply-templates/>
</xsl:template>
5). Templates--Controls which output is created from which input
--"
xsl:template
element form
--"
match
attribute contains an Xpath expression (Xpath expression identifies input node set it matches)
--"
For each node in the node set, the
template contents
(things between xsl:template tags) are instantiated and inserted into the output tree.
6). Attributes
In the DTD, XML element attributes are declared with an ATTLIST declaration. An attribute declaration has the following syntax:
<!ATTLIST element-name attribute-name attribute-type default-value>
|
As you can see from the syntax above, the ATTLIST declaration defines the element which can have the attribute, the name of the attribute, the type of the attribute, and the default attribute value.
The attribute-type can have the following values:
Value | Explanation |
---|
CDATA | The value is character data |
(eval|eval|..) | The value must be an enumerated value |
ID | The value is an unique id |
IDREF | The value is the id of another element |
IDREFS | The value is a list of other ids |
NMTOKEN | The value is a valid XML name |
NMTOKENS | The value is a list of valid XML names |
ENTITY | The value is an entity |
ENTITIES | The value is a list of entities |
NOTATION | The value is a name of a notation |
xml: | The value is predefined |
The attribute-default-value can have the following values:
Value | Explanation |
---|
#DEFAULT value | The attribute has a default value |
#REQUIRED | The attribute value must be included in the element |
#IMPLIED | The attribute does not have to be included |
#FIXED value | The attribute value is fixed |
Attribute declaration example
DTD example:
<!ELEMENT square EMPTY>
<!ATTLIST square width CDATA "0">
XML example:
<square width="100"></square> |
In the above example the element square is defined to be an empty element with the attributes width of type CDATA. The width attribute has a default value of 0.
Default attribute value
Syntax:
<!ATTLIST element-name attribute-name CDATA "default-value">
DTD example:
<!ATTLIST payment type CDATA "check">
XML example:
<payment type="check"> |
Specifying a default value for an attribute, assures that the attribute will get a value even if the author of the XML document didn't include it.
Implied attribute
Syntax:
<!ATTLIST element-name attribute-name attribute-type #IMPLIED> DTD example:
<!ATTLIST contact fax CDATA #IMPLIED>
XML example:
<contact fax="555-667788"> |
Use an implied attribute if you don't want to force the author to include an attribute and you don't have an option for a default value either.
Required attribute
Syntax:
<!ATTLIST element-name attribute_name attribute-type #REQUIRED> DTD example:
<!ATTLIST person number CDATA #REQUIRED>
XML example:
<person number="5677"> |
Use a required attribute if you don't have an option for a default value, but still want to force the attribute to be present.
Fixed attribute value
Syntax:
<!ATTLIST element-name attribute-name attribute-type #FIXED "value"> DTD example:
<!ATTLIST sender company CDATA #FIXED "Microsoft">
XML example:
<sender company="Microsoft"> |
Use a fixed attribute value when you want an attribute to have a fixed value without allowing the author to change it. If an author includes another value, the XML parser will return an error.
Enumerated attribute values
Syntax:
<!ATTLIST element-name attribute-name (eval|eval|..) default-value> DTD example:
<!ATTLIST payment type (check|cash) "cash">
XML example:
<payment type="check">
or
<payment type="cash"> |
posted on 2006-11-27 00:48
Sun River 阅读(256)
评论(0) 编辑 收藏 所属分类:
XML