Connecting Tech Pros Worldwide Forums | Help | Site Map

is XML what I want here?

John Salerno
Guest
 
Posts: n/a
#1: Mar 4 '06
Hi everyone. I thought I might do a little experiment with XML and type
up some rules for syntax formatting for a programming language. But I'm
a little confused about how to format the XML file. My first thought was
I might do this in HTML instead, and I think I sort of wrote the XML
file with HTML syntax in mind. Needless to say, nothing is nested properly:

<?xml version='1.0' encoding='utf-8'?>

<category>Code Layout</category>

<subcategory>Indentation</subcategory>
<rule>Use 4 spaces per indentation level.</rule>

<subcategory>Tabs or Spaces</subcategory>
<rule>Spaces-only are strongly recommended over tabs.</rule>

<subcategory>Maximum Line Length</subcategory>
<rule>Limit all lines to a maximum of 79 characters.</rule>
<rule>
For flowing long blocks of text (docstrings or comments), limiting the
length to 72 characters is recommended.
</rule>
<rule>
The preferred way of wrapping long lines is by using Python's implied
line continuation inside parentheses, brackets and braces. If necessary,
you can add an extra pair of parentheses around an expression, but
sometimes using a backslash looks better.
</rule>

My question is, how do I have text within an element like <category>,
which should actually be the entire parent node of the file? Am I not
supposed to have text in it? What would be the proper way to do this?

I am thinking of something like this:

<h1>Code Layout</h1>
<h2>Indentation</h2>
<p>Use 4 spaces per indentation level.</p>

But obviously that isn't the structure of XML, yet I'm after something
like that. I thought XML might be more general, and therefore a little
more flexible to use in a variety of ways, than HTML, but I can't get
the HTML structure out of my head when trying to write the XML file.

Thanks for any help.

steve_marjoribanks
Guest
 
Posts: n/a
#2: Mar 4 '06

re: is XML what I want here?


It depends what you are using it for really? Is it just like an HTML in
that it is purely for displaying data on a webpage or are you trying to
introduce more functionality?

Steve

John Salerno
Guest
 
Posts: n/a
#3: Mar 4 '06

re: is XML what I want here?


steve_marjoribanks wrote:[color=blue]
> It depends what you are using it for really? Is it just like an HTML in
> that it is purely for displaying data on a webpage or are you trying to
> introduce more functionality?
>
> Steve
>[/color]

Well, my original intent was more for display purposes, which is
probably why I thought of HTML first. But then I figured it might be
useful to write it in XML so I can name the nodes properly and can use
it to interact with programs that might need to read from it.

Is it possible to embed the XML into an HTML file? I'm not too familiar
with what, exactly, XHTML does, so I don't know if this is one option.
steve_marjoribanks
Guest
 
Posts: n/a
#4: Mar 4 '06

re: is XML what I want here?


Look here for an intorduction to XHTML
http://www.w3schools.com/xhtml/default.asp

For just display purposes then use XHTML (or HTML) but if you want to
display the data and have the potential for programs to access the data
within it then I'd recommend that you use XML and use an XSL stylesheet
to transform it for display purposes. There are tutorials for all of
this on that website http://www.w3schools.com

Steve

John Salerno
Guest
 
Posts: n/a
#5: Mar 4 '06

re: is XML what I want here?


steve_marjoribanks wrote:[color=blue]
> Look here for an intorduction to XHTML
> http://www.w3schools.com/xhtml/default.asp
>
> For just display purposes then use XHTML (or HTML) but if you want to
> display the data and have the potential for programs to access the data
> within it then I'd recommend that you use XML and use an XSL stylesheet
> to transform it for display purposes. There are tutorials for all of
> this on that website http://www.w3schools.com
>
> Steve
>[/color]

Thanks, I'll check that out. But one question: will this allow me to add
text to parent nodes? For example, if I have <subcategory></subcategory>
as the parent to several <rule> nodes, am I still able to put text
within the <subcategory> node? Or does it always just contain child nodes?
steve_marjoribanks
Guest
 
Posts: n/a
#6: Mar 5 '06

re: is XML what I want here?


You mean like this?

<subcategory>
Text here
<rule />
<rule />
</subcategory>

Yeah, this is possible, it's called a mixed content element.

Steve

Peter Flynn
Guest
 
Posts: n/a
#7: Mar 5 '06

re: is XML what I want here?


John Salerno wrote:[color=blue]
> Hi everyone. I thought I might do a little experiment with XML and type
> up some rules for syntax formatting for a programming language. But I'm
> a little confused about how to format the XML file. My first thought was
> I might do this in HTML instead, and I think I sort of wrote the XML
> file with HTML syntax in mind. Needless to say, nothing is nested properly:
>
> <?xml version='1.0' encoding='utf-8'?>
>
> <category>Code Layout</category>
>
> <subcategory>Indentation</subcategory>
> <rule>Use 4 spaces per indentation level.</rule>[/color]

This is generally counterproductive. XML works on the basis of
hierarchies and containers (think DIVs), so better would be

<subcategory>
<name>Indentation</name>
<rule>Use 4 spaces per indentation level.</rule>
</subcategory>

However, if you want to use the XML in processing, rather than just
document things, you need to phrase it so a machine can read it:

<subcategory>
<name>Indentation</name>
<rule class="textindent" lang="css" value="4" units="em"/>
</subcategory>

[...][color=blue]
> My question is, how do I have text within an element like <category>,[/color]

Just type it. But in practice, for an XML document going to be used as
data, mixing text and markup is A Bad Idea. It's normal for text
documents (eg HTML) but for XML-used-as-data is will only lead to tears
and recriminations.
[color=blue]
> which should actually be the entire parent node of the file? Am I not
> supposed to have text in it? What would be the proper way to do this?
>
> I am thinking of something like this:
>
> <h1>Code Layout</h1>
> <h2>Indentation</h2>
> <p>Use 4 spaces per indentation level.</p>[/color]

Not useful.

///Peter
--
XML FAQ: http://xml.silmaril.ie/
Peter Flynn
Guest
 
Posts: n/a
#8: Mar 5 '06

re: is XML what I want here?


John Salerno wrote:[color=blue]
> steve_marjoribanks wrote:[color=green]
>> Look here for an intorduction to XHTML
>> http://www.w3schools.com/xhtml/default.asp
>>
>> For just display purposes then use XHTML (or HTML) but if you want to
>> display the data and have the potential for programs to access the data
>> within it then I'd recommend that you use XML and use an XSL stylesheet
>> to transform it for display purposes. There are tutorials for all of
>> this on that website http://www.w3schools.com
>>
>> Steve
>>[/color]
>
> Thanks, I'll check that out. But one question: will this allow me to add
> text to parent nodes?[/color]

Not relevant. Whether or not you can add text to an element's content
depends on the rules (if any) you have established beforehand.

If you use one of the well-known sets of rules like DocBook, then your
ability to add text in any given element depends on whether it has been
made suitable for this or not. Paragraphs <para> can contain mixed
content, for example; list containers like <itemizedlist> can't (but
paragraphs within each <listitem> obviously can).
[color=blue]
> For example, if I have <subcategory></subcategory>
> as the parent to several <rule> nodes, am I still able to put text
> within the <subcategory> node? Or does it always just contain child nodes?[/color]

If you decide you can, you can, but as I pointed out earlier, it's
poor practice unless you have some very specific reason for wanting to
intermingle arbitrary text and rule elements.

///Peter
--
XML FAQ: http://xml.silmaril.ie/
Closed Thread