Topic: xml: when to use attributes vs text (and doctype/validation) Pages that link to <a href="https://ozoneasylum.com/backlink?for=27871" title="Pages that link to Topic: xml: when to use attributes vs text (and doctype/validation)" rel="nofollow" >Topic: xml: when to use attributes vs text (and doctype/validation)\

 
Author Thread
chex
Obsessive-Compulsive (I) Inmate

From:
Insane since: Apr 2006

IP logged posted posted 05-01-2006 23:00 Edit Quote

warning: Confused.

1) Do I need a doctype for my xml documents? Right now I'm viewing the file in firefox to check that it is well-formed. I think there are two types of doctypes, is one newer/better or just another syntax?

After searching it looks like it's DTD, and schema. Should I focus on using schema? Or is there a disadvantage? It looks like Schema is more verbose, but is easier to use in bigger documents, and re-useable. (Or not?)

2) Is the doctype related to XSLT, or is XSLT for xml stylesheets XSL? After some reading it looks like XSL has to do with transforming the document and a stylesheet is a part of that? (And parsing/iterating xml files for output on a xhtml page?)

3) How do I know when something should be an attribute, or text? (CDATA?) Some values seem like they could be either. Here's a couple of examples. If i'm doing anything else wrong schematically tell me. Text might not be the right term, I'm talking about <element>text</element>. From API's, it seems like it's called text (attribute? or element?). Or maybe CDATA?

code:
<!-- todo1 -->
<TodoList>
	<item group="post" title="xml attribute vs text">
		<date year="2006" month="05" day="01"/>
		<text>
		Post this thread.
		</text>
	</item>
	<item group="foo" title="bar">
		<date year="2006" month="05" day="01"/>
		<text>
		Foobar.
		</text>
	</item>
</TodoList>

Vs. having group and/or title as text:

code:
<!-- todo2 -->
<TodoList>
	<item>
		<date year="2006" month="05" day="01"/>
		<title>xml attribute vs text</title>
		<group>post</group>
		<text>
		Post this thread.
		</text>
	</item>
	<item>
		<date year="2006" month="05" day="01"/>
		<title>bar</title>
		<group>foo</group>
		<text>
		Foobar.
		</text>
	</item>
</TodoList>

code:
<!-- bookmarks.test.xml, 'item' or 'Group' can be a child of another 'Group' element -->
<BookmarkList>
	<Group name="portal">
		<Item name="/.">
			<url>slashdot.org</url>
		</Item>

	</Group>
	<Group name="programming">

		<!-- item in programming, while programming has children of type 'group' -->
		<Item name="gamedev">
			<url>http://www.gamedev.net</url>
		</Item>

		<Group name="cpp">
			<Item name="c++ STL">
				<url>http://foo.com</url>
			</Item>
		</Group>
		<Group name="perl">
			<Item name="perlmonks">
				<url>perlmonks.foo</url>
			</Item>
		</Group>
	</Group>
</BookmarkList>

HZR
Paranoid (IV) Inmate

From: Cold Sweden
Insane since: Jul 2002

IP logged posted posted 05-02-2006 11:27 Edit Quote
quote:
1) Do I need a doctype for my xml documents?


No, absolutely not. A doctype declaration is needed for validation. If you don't need to validate your document, you don't need it.

quote:
After searching it looks like it's DTD, and schema. Should I focus on using schema? Or is there a disadvantage? It looks like Schema is more verbose, but is easier to use in bigger documents, and re-useable. (Or not?)


What you should choose depends. Schemas are much more expressive. If you want entitites you must use a DTD though, since schemas can't declare them.

quote:
2) Is the doctype related to XSLT, or is XSLT for xml stylesheets XSL? After some reading it looks like XSL has to do with transforming the document and a stylesheet is a part of that? (And parsing/iterating xml files for output on a xhtml page?)


XSLT is a part of XSL (the other being XSL-FO but don't care about that), and is used for transforming a document (not necessarily an XML document) into another.

quote:
3) How do I know when something should be an attribute, or text? (CDATA?) Some values seem like they could be either.


There is no rule, you choose. As a general rule, use attributes for things that describe some characteristics of the element, for metedata and stuff like that, and use text for things that are meant to be seen in a rendition.
For example, I would mix your todo lists. <title> should be an element, but group would be better as an attribute in my opinion.

code:
<!-- todo3 -->
<TodoList>
	<item group="post">
		<date year="2006" month="05" day="01"/>
		<title>xml attribute vs text</title>
		<text>
		Post this thread.
		</text>
	</item>
	<item group="foo">
		<date year="2006" month="05" day="01"/>
		<title>bar</title>
		<text>
		Foobar.
		</text>
	</item>
</TodoList>


(Date could possibly be an element also).

You also ask about CDATA (character data). You can mark a section as containing only CDATA. (In SGML you could declare an elements' content as CDATA, but this isn't possible in XML). CDATA sections will no be parsed for markup. For example

code:
<![CDATA[not an <element/> and not an &entity-reference;]]>



(Edited by HZR on 05-02-2006 11:38)

(Edited by HZR on 05-02-2006 11:50)



Post Reply
 
Your User Name:
Your Password:
Login Options:
 
Your Text:
Loading...
Options:


« BackwardsOnwards »

Show Forum Drop Down Menu