Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XSLT: Parsing HTML embedded in XML?

Tags:

html

xslt

On my web site I have XMLs with my page contents (automatically generated from my DB) - which are displayed using XSLT. The problem is this: I'd like to have some formatting within some of the XML tags. For instance, if I have an XML containing an article in a format like this:

<article>
  <header>Cool article</header>
  <author>Me!</author>
  <content>
    This is an article. It's <b>HUGE</b>, and here's a <a href="http://Www.foo.com">link</a>.
  </content>
</article>

However, if I simply get the contents using this: <xsl:value-of select="content" /> all the HTML formatting is ignored/lost. I guess it's mistaken for XML child nodes, and not actual data residing in the content node.

What's the preferred way of achieving formatting like what described here?

Thanks in advance.

like image 467
Hallgeir Avatar asked Jul 09 '09 16:07

Hallgeir


People also ask

How can XSLT transform XML into HTML explain with example?

Transform XML to HTML Write the Java class to transform the XML file data to HTML using XSLT file. We have put both XML and XSLT files under classpath and finaly transforms the XML data into HTML output. We write the output to the HTML file called books. html under the project's root directory.

How XML is converted into HTML format using XSLT?

The standard way to transform XML data into other formats is by Extensible Stylesheet Language Transformations (XSLT). You can use the built-in XSLTRANSFORM function to convert XML documents into HTML, plain text, or different XML schemas. XSLT uses stylesheets to convert XML into other data formats.

Does anyone use XSLT anymore?

XSLT is very widely used. As far as we can judge from metrics like the number of StackOverflow questions, it is in the top 30 programming languages, which probably makes it the top data-model-specific programming language after SQL. But XSLT isn't widely used client-side, that is, in the browser.

What is XSL how it can be used to present XML?

XSL gives a developer the tools to describe exactly which data fields in an XML file to display and exactly where and how to display them. Like any style sheet language, XSL can be used to create a style definition for one XML document or reused for many other XML documents.


3 Answers

<xsl:value-of select="content" /> 

outputs the value of a node. And the value of your <content> node actually is:

This is an article. It's HUGE, and here's a link

What you probably need is to copy the entire node:

<xsl:copy-of select="content" /> 

This is largely a guess since I don't know how your system works.

like image 125
Tomalak Avatar answered Oct 31 '22 06:10

Tomalak


<xsl:value-of
select="..."
disable-output-escaping="yes"/>

This works on all browsers except Firefox.

like image 44
ZippyV Avatar answered Oct 31 '22 06:10

ZippyV


I think your problem is this:

 <xsl:output method="xml" doctype-public="-//W3C//DTD XHTML 1.0 Strict//EN"
          media-type="application/html+xml" encoding="utf-8" omit-xml-declaration="yes" indent="no"/>

make sure your output is of type html,

application/html
like image 41
Andrew Avatar answered Oct 31 '22 08:10

Andrew