4

I'm generally pretty happy with the native XML Viewer in Firefox.

It displays valid XML files (like the one below) clearly and helpfully.

Example of XML:

<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">

  <url>
    <loc>https://example.com/</loc>
    <lastmod>2020-03-17T15:57:23+00:00</lastmod>
  </url>

</urlset>

However, I've noticed that as soon as I add XHTML to the XML (using the correct XHTML Namespace - see below), the XML Viewers in both Firefox and Chrome revert to displaying the XML as plaintext:

XHTML Namespace and Element:

  • XHTML Namespace: xmlns:xhtml="http://www.w3.org/1999/xhtml"
  • XHTML Element: <xhtml:link />

Example of XML + XHTML:

<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
  xmlns:xhtml="http://www.w3.org/1999/xhtml">

  <url>
    <loc>https://example.com/</loc>
    <lastmod>2020-03-17T15:57:23+00:00</lastmod>
    <xhtml:link rel="alternate" hreflang="en" href="https://example.com/" />
    <xhtml:link rel="alternate" hreflang="de" href="https://example.com/de/" />
  </url>

  <url>
    <loc>https://example.com/de/</loc>
    <lastmod>2020-03-12T19:42:12+00:00</lastmod>
    <xhtml:link rel="alternate" hreflang="en" href="https://example.com/" />
    <xhtml:link rel="alternate" hreflang="de" href="https://example.com/de/" />
  </url>

</urlset>

For the longest time I thought I was introducing an error into my XML and invalidating it. But I have checked on numerous third-party XML validators and the XML is definitely valid. It's just that the browser-native viewers (apparently) can't cope with XHTML being included within the XML.

Is there anything I can do in this situation to help the native XML Viewers in Firefox and Chrome understand and parse the markup as XML or is there nothing to be done for now and 3rd-party software is the only answer?

0

1 Answer 1

3

Web browsers try to render anything, you throw at them, as HTML. Only, if they know for sure, that there is no HTML, they will render either as plain-text or XML, latter one in a treeview (as in your first example). The notable exceptions would be SVG and MathML, which get rendered differently.

Since the browser sensed HTML in your second example, it switched to the HTML view.

A little bit of an explanation:

As long the browser has styling information for elements, it will make use of them. This is the case for HTML (for which default styles are hardcoded into the browser). It will also be the case, when the browser finds CSS associated with any XML, either via the processing instruction

<?xml-stylesheet href="style.css"?>

or via inlined style (via the HTML style element)

<html:style type="text/css">
  @namespace url("http://www.example.org/ns/example");
  @namespace html url("http://www.w3.org/1999/xhtml");
  paragraph {display: block;}
  meta {display: none;}
  html|style {display:none;}
  html|script {display:none;}
</html:style>

In your second example, it found such styling information, since it found HTML, for which it has styles hardcoded and implied and thus rendered it styled.

When the browser renders the XML document, it will render all elements, which it does not have a default style for, as inline-content. This may be the reason, you assumed, that it displays the contents as plain-text.

If it would have found HTML script elements, it would have executed the Javascript therein. So, the tree-view is just a fall-back and not the default.

If you dislike this behavior, you could try to find, whether your web-browser supports a configuration switch, that changes this behavior, but I don't know of any.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .