is XML what I want here?

J

John Salerno

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.
 
S

steve_marjoribanks

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
 
J

John Salerno

steve_marjoribanks said:
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

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.
 
S

steve_marjoribanks

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
 
J

John Salerno

steve_marjoribanks said:
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

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?
 
S

steve_marjoribanks

You mean like this?

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

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

Steve
 
P

Peter Flynn

John said:
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>

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>

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

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.
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>

Not useful.

///Peter
 
P

Peter Flynn

John said:
Thanks, I'll check that out. But one question: will this allow me to add
text to parent nodes?

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 said:
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?

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,997
Messages
2,570,241
Members
46,831
Latest member
RusselWill

Latest Threads

Top