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.
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.
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.
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.
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.
<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.
<xsl:value-of
select="..."
disable-output-escaping="yes"/>
This works on all browsers except Firefox.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With