The CMS I use uses the Xalan XSLT processor which is a XSLT version 1.0 processor. Editors who use WYSIWYG fields within the CMS can save content that will look like either:
<story>
<p>Story Text which may have <em>formatting</em>.</p>
</story>
or:
<story>Story Text which may have <em>formatting</em>.</story>
or even:
<story>
<div>Story Text which may<br /> have <em>formatting</em>.</div>
</story>
In order to handle those varying cases I've been using:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html"/>
<xsl:template match="/system-index-block">
<xsl:for-each select="story">
<xsl:choose>
<!-- If wrapped in an element -->
<xsl:when test="./p|./div">
<!-- copy what is inside story, allow for text outside of elements -->
<xsl:copy-of select="./text()|./*"/>
</xsl:when>
<!-- wrap the content in a paragraph tag -->
<xsl:otherwise>
<p>
<!-- copy text and tags from inside story -->
<xsl:copy-of select="./text()|./*"/>
</p>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
Is there a better way of producing well formed HTML from this data? Is there a way to simply this?
<story>
elements you could simply replace those with<div>
s. If it's more complicated it would be useful to see an example (for example, could there be<story>
s within<story>
s?) \$\endgroup\$<story>
elements. The<story>
element contains the post processed output of a TinyMCE instance and in most cases the editors are restricted from using the "Edit HTML Source" button within that editor. \$\endgroup\$