How to Insert HTML Content in Xml Document

Is it possible to insert HTML content in XML document?

You can include HTML content. One possibility is encoding it in BASE64 as you have mentioned.

Another might be using CDATA tags.

Example using CDATA:

<xml>
<title>Your HTML title</title>
<htmlData><![CDATA[<html>
<head>
<script/>
</head>
<body>
Your HTML's body
</body>
</html>
]]>
</htmlData>
</xml>

Please note:

CDATA's opening character sequence: <![CDATA[

CDATA's closing character sequence: ]]>

HTML tags within XML

Using the minimal example you give, the XML validates against the XSD snippet you provide (after adding an xs:schema as a root element.)

example.xsd:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="description">
<xs:complexType mixed="true">
<xs:sequence>
<xs:any namespace="http://www.w3.org/1999/xhtml"
processContents="lax"
minOccurs="0"
maxOccurs="unbounded" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>

test.xml:

<description>
<p xmlns="http://www.w3.org/1999/xhtml"> This course is an introduction to the information technologies required for secure, practical information systems for electronic commerce.
Topics will be chosen from areas such as document representation (XML, DTDs, XML Schema, XSLT, CSS),
</p> security (encryption, public key, symmetric key,
PKI, authentication); kinds of attack and vulnerabilities, electronic trading (spontaneous, deliberative, auctions), electronic document management
(metadata, search, digital libraries, management and processing), recent developments and maturation of the area,
such as web application frameworks, web services, the semantic web , mobile commerce </description>

Validation:

$ xmllint --noout --schema example.xsd test.xml 
test.xml validates

So I'm not sure what your problem is.

One problem that comes to mind is you might not be using xhtml. The HTML you include in the description element (or any HTML) must be valid xml (and hence xhtml, not the sloppy sgml-like html syntax), including making any xhtml named character entities (e.g.,  ) available to the schema validator. Pretty much any html you find on the web will absolutely not be valid xml.

If you want to make the html available to XML processors and validation, use an XHTML 1.0 xsd or (more flexibly) mix and match modules available from XHTML 1.1 and its XSDs.

If this is too much trouble for you and you are ok with treating your HTML content as a blob of text, just include it as text content and modify your xsd schema to expect xs:string or xs:normalizedString as the content of the description element.

Use HTML tags in XML file and display/recognize them later as HTML

It is still not clear what is your problem and what is your goal.

Let me try to do some attempt to help you.

First thing - you have data:

<description>Die <strong> Zweckbestimmung </strong> ist ein eleme Konsequenzen.</description>
<comment>All <br/><a href="/someLink" class="button">Erfahren Sie hier mehr</a></comment>

That data portion has html encoded characters. The point is to never mix data with real html or xml.

So when you do:

$this->topics[$page] = $thema->description;

your value still contain html encoded characters:

$this->topics[$page] === `Die <strong> Zweckbestimmung </strong> ist ein eleme Konsequenzen.`

Which is probably kind wrong from your point of view. But to me I don't see any problem yet at this moment.

Then you do:

 <div class='question'><p><strong>Frage {i.cycle}</strong>: 
{question.questiontext}</p>

Lets simplify and focus only on this part:

 <p>{question.questiontext}</p>

That one of things which are not clear in your data xml there is no field with name questiontext. So lets assume that is topic->question->comment.

With your value it means:

<p>All  <br/><a href="/someLink" class="button">Erfahren Sie hier mehr</a></p>

How can I include HTML in an XML document?

You can wrap the HTML in CDATA tags (character data) to indicate raw, uninterpreted/unparsed text.

<![CDATA[your markup]]>

The term indicates that a certain portion of the document is general character data, rather than non-character data or character data with a more specific, limited structure. ... In an XML document or external parsed entity, a CDATA section is a section of element content that is marked for the parser to interpret as only character data, not markup.

Add HTML Codes to XML file using PHP

You can use the PHP function createCDATASection to add CDATA to your xml file:

Exchange

$newGame -> appendChild($scores -> createElement("Header", $_POST['header']));

for

$h = $scores -> createElement("Header"); //Create a empty <Header> tag
$h -> appendChild($scores ->createCDATASection ($_POST['header'])); //Add a CDATA element to it
$newGame -> appendChild($h); //Then append it to the game tag

With the comments, it should be quite straight-forward.

XSLT Insert html content

Given this source:

<html>
<head/>
<body>
<content>
<h2>Header</h2>
<p><a href="...">some link</a></p>
<p><a href="...">some link1</a></p>
<p><a href="...">some link2</a></p>
</content>
</body>
</html>

This stylesheet will do what you want to do:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="/html/body/content/h2">
<xsl:copy>
<xsl:apply-templates/>
</xsl:copy>
<p><a href="...">your new link</a></p>
</xsl:template>
</xsl:stylesheet>


Related Topics



Leave a reply



Submit