I've wonder if i had a xml content then den display it to mya lay-out html.is that possible ?could i used a script on it?like php or asp.

Dani AI

Generated

Short summary tied to the thread: wants to keep article content in XML and render it inside an HTML layout. There are three practical ways to do that: server-side parse into your HTML (SimpleXML or DOM), server-side XSLT transform (generates HTML from XML + XSL), or client-side transform in the browser (JS + XSLTProcessor). pointed toward XSLT — below are compact, actionable examples and a few troubleshooting tips.

Server-side: SimpleXML (quick template output)

<?php
$xml = simplexml_load_file('articles.xml');
foreach ($xml->article as $a) {
  echo '<article>';
  echo '<h2>' . htmlspecialchars((string)$a->title) . '</h2>';
  echo '<div>' . $a->body . '</div>';
  echo '</article>';
}
?>

Use this when you want simple PHP templating and control over output. Escape text with htmlspecialchars() unless the XML contains trusted HTML.

Server-side: XSLT transform (keeps layout in an XSL file)

<?php
$xml = new DOMDocument; $xml->load('articles.xml');
$xsl = new DOMDocument; $xsl->load('articles.xsl');
$proc = new XSLTProcessor(); $proc->importStylesheet($xsl);
echo $proc->transformToXML($xml);
?>

If this errors, ensure the PHP XSL extension is enabled. See PHP docs for XSLTProcessor and installation: XSLTProcessor class and XSL installation notes.

Client-side: browser XSLT (interactive, but mind SEO/CORS)

fetch('articles.xml').then(r=>r.text())
  .then(x=>new DOMParser().parseFromString(x,'application/xml'))
  .then(xml=>fetch('articles.xsl').then(r=>r.text()).then(xslt=>{
    const xslDoc = new DOMParser().parseFromString(xslt,'application/xml');
    const p = new XSLTProcessor(); p.importStylesheet(xslDoc);
    document.getElementById('content').appendChild(p.transformToFragment(xml, document));
  }));

Browser XSLT support and API details: XSLTProcessor — MDN. Remember same-origin/CORS rules and that server-side rendering is better for SEO and older clients.

Troubleshooting quick list:

  • If output is blank, enable libxml errors in PHP (libxml_use_internal_errors(true)) and inspect errors.
  • Verify file paths, MIME types (serve XML as application/xml or text/xml), and matching character encodings.
  • Sanitize untrusted XML/XSL to avoid injection; cache transformed HTML for performance.
  • If using a CMS, consider storing article content in a DB and generating XML only when needed.

These options give you immediate ways to render XML into your HTML layout; pick server-side XSLT for separation of content/template, SimpleXML for direct PHP templating, or client-side XSLT for browser-driven rendering.

Recommended Answers

All 6 Replies

Hi There,

Please clarify the question, I am not getting you,

Rahul

I wanted to display the content by xml file! and my question is how could i do it?

Do you want to display the content of an XML file as HTML? i.e. transform XML content to HTML..? You can do that with XSLT. Let me know if that's what you want to do; there's no point me talking about XSLT otherwise =P

The w3cschools minisite for XSLT;

yup i had seen xslt...is this possible to with php?

There is an XSLT extension for PHP:

I think it's just a frontend to an existing XSLT processor.

I don't think PHP itself is designed with XML processing in mind; but you could write PHP script to parse XML/transfer XML segments between files/etc; probably even to transform XML.

But, I'm still not entirely clear on what you want to do..

All I want is to display articles/content on my HTML lay-Out's by means of having script that used xm files and display it.

The display src would be coming up to the xml file.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.