• Advertisement
Sign in to follow this  

Is this valid XML?

This topic is 2348 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I know you can have:

<name>Sam</name>

and

<name>
<nickname>Wooty</nickname>
<nickname>Yogi</nickname>
<nickname>Chi-Lite</nickname>
</name>

but is this valid/legal:

<name>Sam
<nickname>Wooty</nickname>
<nickname>Yogi</nickname>
<nickname>Chi-Lite</nickname>
</name>

Share this post


Link to post
Share on other sites
Advertisement
yes ... xml allows mixed content definitions for tags ... like this valid xhtml snippet

<div>And once he finally laid hands on <a href="daggers_of_ice.html">it</a> he felt a nearly irresistible compulsion to flee.</div>

it just isn't very desirable to do these types of formats when the focus is on data modelling / interchange vs markup.

Share this post


Link to post
Share on other sites
Just out of curiosity, what exactly are you doing with said XML?

(Just remember... XML is like violence. If it doesn't work, you just aren't using enough of it...)

Share this post


Link to post
Share on other sites
Well really this is for a FSM I'm working on.

Basically I wanted it to look like this:

<state>
<name>Assign Responsible</name>
<transition>Notify Responsible
<trigger_value>Open</trigger_value>
<trigger_value>Need Acknownledgement</trigger_value>
</transition>
<action>email</email>
</state>


Where name = state of current state, transition = name of the next state to move to, trigger_value = values needed to move to next state, action = any additional activity that must be performed while in current state.

Share this post


Link to post
Share on other sites

<transition>
<target>Notify Responsible</target>
<trigger_value>Open</trigger_value>
<trigger_value>Need Acknowledgement</trigger_value>
</transition>


Is this more "acceptable" or is the difference between yours and mine a stylistic one?

Share this post


Link to post
Share on other sites


<transition>
<target>Notify Responsible</target>
<trigger_value>Open</trigger_value>
<trigger_value>Need Acknowledgement</trigger_value>
</transition>


Is this more "acceptable" or is the difference between yours and mine a stylistic one?

There's no real difference. For short, single, simple types an attribute is valid, for child nodes, long values or multiple values of the same type, sub nodes are preferable. What are your design goals ? Manual edited xml files, error robust files, easy to read, fast to read, small memory footprints ?

One tip: consider to use xml schema from the beginning to make your xml files schema conform and get some automatically xml file validation.

Share this post


Link to post
Share on other sites

Parsing blended CDATA and tags is a bit cumbersome in most libraries I've used, if that weighs into your decision any.

I'm a beginner when it comes to XML. So though I've seen CDATA tags before, they won't be showing up in this XML... ever.

Share this post


Link to post
Share on other sites

[quote name='Alpha_ProgDes' timestamp='1316048061' post='4861823']

<transition>
<target>Notify Responsible</target>
<trigger_value>Open</trigger_value>
<trigger_value>Need Acknowledgement</trigger_value>
</transition>


Is this more "acceptable" or is the difference between yours and mine a stylistic one?

There's no real difference. For short, single, simple types an attribute is valid, for child nodes, long values or multiple values of the same type, sub nodes are preferable. What are your design goals ? Manual edited xml files, error robust files, easy to read, fast to read, small memory footprints ?

One tip: consider to use xml schema from the beginning to make your xml files schema conform and get some automatically xml file validation.
[/quote]
This is a file that I would like to be (or can be): manually editable, easy to read, fast to read (for a human), and easy to XPath.

Share this post


Link to post
Share on other sites
It's well-formed. If there is only one root element. The above snippets are well-formed.

Valid XML requires a schema.

XPath[/quote]
Needs a schema.

Share this post


Link to post
Share on other sites

.NET must be very lax then. Because I did XPath on XML that I wrote and it didn't scream at me.

Though, I'm pretty sure that I missed your point.


So few ever write valid XML that any real world parser needs to ignore validity and work with what it's got. Ideally, tools would reject all non-valid XML, but considering there were competing specs and standards, many of them pushed by heavyweights like IBM and Oracle, neither would cede and result in multiple exclusive yet standard-compliant and incompatible XML documents. People in the trenches got yelled when enterprise_big_arcane_soap_rfc service suddenly couldn't talk to another_enterprise_soap_provider and rigged them together into enterprise_grade_service_that_works_for_us_and_keeps_management_happy. Of course, when such company sold, there were now 3 incompatible XML providers...

This led to XML ecosystem being a complete mess and is generally being abandoned in favor of JSON by anyone who can do so.

XML makes sense if it's completely and correctly implemented. Otherwise it's just an unreadable tag soup, which is why it's universally hated, despite generally being a "Good Idea".

Similar idea. Ask any seasoned veteran and they'll agree it's a good idea, but the result in practice is just a useless mess that everyone abandoned.


IMHO - either implement XML fully and correctly so it passes all validations, including DTDs, published namespace and schema
or - use JSON.

JSON supports schemas or something similar, but parsers are allowed to be lax in what they accept. It does lack in the tooling department, but is not verbose, so complex tools aren't needed and grep gets you far.

Share this post


Link to post
Share on other sites

Goto W3Schools, they have alot on XML and other related tech. Simple straightforward examples. Great place to learn the basics.

Be careful about using W3Schools as a learning resource or reference, as there are a lot of problems with the information presented -- see "W3Fools" for further information. W3Schools can still be useful, but do take anything you read there with a grain of salt and be sure to check other sources.


Share this post


Link to post
Share on other sites


<transition>
<target>Notify Responsible</target>
<trigger_value>Open</trigger_value>
<trigger_value>Need Acknowledgement</trigger_value>
</transition>


Is this more "acceptable" or is the difference between yours and mine a stylistic one?

It's not really any more or less acceptable, just stylistic.


I might just stretch so far as to say that it's mine that may be more widely preferred, my intuition is that there are more guidelines out there something along the lines of:
Use an attribute over a nested element when:
* There's no more than 1 occurance permitted
* If it's mandatory
* When the content of the property is short and 'inline-able' like a number or a name.

The target property fits that description, but the trigger_value doesn't since you permit a variable number of them.

These are just ad-hoc rules though, it doesn't really make much difference. Some schemas allow you to specify a value either as an attribute or as a sub-element which allows the author to decide which is better.

Share this post


Link to post
Share on other sites

Be careful about using W3Schools as a learning resource or reference, as there are a lot of problems with the information presented -- see "W3Fools" for further information. W3Schools can still be useful, but do take anything you read there with a grain of salt and be sure to check other sources.


While they do point out a few issues, advice which lists: "Professional web developers do not recommend the use of WYSIWYG editors. " should be taken with a grain of salt as well.

The same crowd would scoff at anyone who ever in their career used anything but emacs or vi. The comments are way too similar to Ruby hipster crowd for my liking.

It reminds of various edge cases in C++. They make for endless debates, but are ultimately useless, because beginners will get stuck at linker error and MSVCRT redistributable problem.

Also, XML sections are not touched by W3Fools, since cool people don't touch XML. Which by default excludes anything even remotely related to any serious business or enterprise. If your font doesn't downgrade properly on IE8.1b2 nobody cares, but if your SOAPs are incompatible the servers will clog up and all your customers won't be able to pay. So, priorities...

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement