tags:

views:

381

answers:

5

It has been suggested that I use CSS for displaying XML. I know in my heart that this is wrong, but cannot find the words to adequately convince others. Can anyone provide me with a list of pros/cons of using CSS and XSLT for displaying XML.

Thanks!

+3  A: 

CSS:

  • Styling and overall visual presentation

XSLT:

  • Styling and visual presentation (if desired)
  • Allows complete and radical changes of the actual tree that's rendered

If your XML maps pretty nicely to individual layout elements then use CSS, if you need more work in adapting it for display, then use XSLT. Things like arbitrary re-orderings of content or collapsing multiple lists of values into a table, etc. don't lend themselves well to CSS. CSS really only works nicely if your overall structure of the document is already the same you want to display.

Joey
If XML + CSS is not equal to XHTML + CSS it has basically no place in a browser and should be avoided to prevent major problems with rendering and many hours trying to get X to look and behave as BODY, or workaround the absence of IMG, @title hover semantics etc. (see my, sorry: rather extensive, answer for the some more drawbacks).
Abel
+1  A: 

CSS seems to fail for XML in IE8: http://stackoverflow.com/questions/1681606/how-to-apply-css-to-namespace-prefixed-elements-in-ie8

Yet another good reason not to revert to XML-only, nice one.
Abel
A: 

Did they mean a stylesheet? Any XML Stylesheet is different to a CSS stylesheet. XSL is sometimes refered to as a Stylesheet so you might both be right :)

Pete Duncanson
Huh? CSS looks pretty much the same for XML as for HTML. What kind of stylesheet you use for styling XML depends solely on the `type` parameter.
Joey
CSS is equal for either HTML or XML. "stylesheet" is used to refer to XSL (Xml Stylesheet Language) which has never made it and is now only around for creating controversy. XSL became XSLT (T for transformations) and XSL-FO (FO for Formatting Objects). The latter is the "stylesheet" part of the XML/XSLT/XSL-FO family of technologies. CSS is not XML and has no (real) place anymore, apart from giving a quick layout.
Abel
Abel: look at SVG and OpenDocument and say again that CSS has no place for XML :-)
Joey
haha, good point. But where did I say that CSS has no place for XML? (sorry my second sentence above, it should've read *"stylesheet" is used to refer to CSS and XSL ...*)
Abel
Btw, SVG is based on XML, OD is based on XML and XHTML is based on XML. CSS + XML makes little sense, because XML, by definition, is without semantics. SVG, OD, XHTML, SMIL etc gives these semantics and then CSS becomes apparent.
Abel
So we are all friend again right? :)
Pete Duncanson
I've always been, apologies if it looked or sounded different ;)
Abel
+6  A: 
Abel
When you downvote, please have the courtesy to explain why, so I can improve my answer for you and others, and learn something in the process.
Abel
I accidently clicked the upvote arrow and "devoted" you. SO would not let me upvote you again at the time. Now you have edited I have re-upvoted you. Thanks for your help, you have been most helpful.
pisswillis
+1  A: 

There is nothing inherently wrong with using CSS to style XML, it's just not done very often (although I've done it in the past as an experiment).

Take SVG as an example: it's XML, and it can be styled with CSS. Also, have a look at section 3.1, "Definitions", of CSS 2.1:

Source document

The document to which one or more style sheets apply. This is encoded in some language that represents the document as a tree of elements. Each element consists of a name that identifies the type of element, optionally a number of attributes, and a (possibly empty) content. For example, the source document could be an XML or SGML instance.

Document language

The encoding language of the source document (e.g., HTML, XHTML, or SVG). CSS is used to describe the presentation of document languages and CSS does not change the underlying semantics of the document languages.

(My emphasis).

The primary possibilities to consider in deciding whether whether to use CSS as opposed to XSLT strike me as being:

  1. Does it make sense to transform the XML document into a different structure (e.g. XHTML) the better to represent the semantics of the document?

  2. Is the semantic structure of the document sufficient in itself, such that only presentational styling needs to be applied?

If you have some pretty arbitrary and meaningless XML - something like <data><column><row><value>1</value><value>foo</value></row></column></data> then the XSLT route would make sense. If however you have a document that has its own clearly defined semantics (e.g. an SVG file, or one of any number of XML applications) and all you want to do is make the headings stand out and the font look nice, then CSS is fine.

Ultimately, do the simplest thing that could possibly work. CSS - at least from version 2 onwards - was specifically designed to be language-agnostic (that is, not tied to HTML) so there's no good reason not to use it when it makes sense.

NickFitz