Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Another approach, similar to Flynn's but staying with XSLT instead of adding a separate text processing layer, would be to transform the initial HTML output in the same stylesheet to collapse the adjacent elements of <code>&lt;b&gt;</code> or <code>&lt;i&gt;</code> into single elements.</p> <p>In other words, the stylesheet would first generate the initial HTML result tree, then pass that as input to a set of templates (using a special mode) that performed the collapsing operation.</p> <p><strong>Updated:</strong> Here is a working, 2-stage stylesheet, built on @empo's stage-1 stylesheet:</p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="xs w" xmlns:w="http://schemas.microsoft.com/office/word/2003/wordml" version="2.0"&gt; &lt;xsl:output method="html"/&gt; &lt;xsl:strip-space elements="*"/&gt; &lt;xsl:variable name="collapsibles" select="('i', 'b', 'u')"/&gt; &lt;!-- identity template, except we collapse any adjacent b or i child elements. --&gt; &lt;xsl:template match="*" mode="collapse-adjacent"&gt; &lt;xsl:copy&gt; &lt;xsl:copy-of select="@*"/&gt; &lt;xsl:for-each select="node()"&gt; &lt;xsl:choose&gt; &lt;xsl:when test="index-of($collapsibles, local-name()) and not(name(preceding-sibling::node()[1]) = name())"&gt; &lt;xsl:copy&gt; &lt;xsl:copy-of select="@*"/&gt; &lt;xsl:call-template name="process-niblings"/&gt; &lt;/xsl:copy&gt; &lt;/xsl:when&gt; &lt;xsl:when test="index-of($collapsibles, local-name())"/&gt; &lt;!-- do not copy --&gt; &lt;xsl:otherwise&gt; &lt;xsl:copy&gt; &lt;xsl:copy-of select="@*"/&gt; &lt;xsl:apply-templates mode="collapse-adjacent"/&gt; &lt;/xsl:copy&gt; &lt;/xsl:otherwise&gt; &lt;/xsl:choose&gt; &lt;/xsl:for-each&gt; &lt;/xsl:copy&gt; &lt;/xsl:template&gt; &lt;!-- apply templates to children of current element *and* of all consecutively following elements of the same name. --&gt; &lt;xsl:template name="process-niblings"&gt; &lt;xsl:apply-templates mode="collapse-adjacent"/&gt; &lt;!-- If immediate following sibling is the same element type, recurse with context node set to that sibling. --&gt; &lt;xsl:for-each select="following-sibling::node()[1][name() = name(current())]"&gt; &lt;xsl:call-template name="process-niblings"/&gt; &lt;/xsl:for-each&gt; &lt;/xsl:template&gt; &lt;!-- @empo's stylesheet (modified) follows. --&gt; &lt;xsl:template match="/"&gt; &lt;html&gt; &lt;body&gt; &lt;xsl:variable name="raw-html"&gt; &lt;xsl:apply-templates /&gt; &lt;/xsl:variable&gt; &lt;xsl:apply-templates select="$raw-html" mode="collapse-adjacent"/&gt; &lt;/body&gt; &lt;/html&gt; &lt;/xsl:template&gt; &lt;xsl:template match="w:document | w:body"&gt; &lt;xsl:apply-templates /&gt; &lt;/xsl:template&gt; &lt;!-- match paragraph --&gt; &lt;xsl:template match="w:p"&gt; &lt;p&gt; &lt;xsl:apply-templates select="w:r"/&gt; &lt;/p&gt; &lt;/xsl:template&gt; &lt;!-- match run with property --&gt; &lt;xsl:template match="w:r[w:rPr]"&gt; &lt;xsl:apply-templates select="w:rPr/*[1]"/&gt; &lt;/xsl:template&gt; &lt;!-- Recursive template for bold, italic and underline properties applied to the same run. Escape to paragraph text --&gt; &lt;xsl:template match="w:b | w:i | w:u"&gt; &lt;xsl:element name="{local-name(.)}"&gt; &lt;xsl:choose&gt; &lt;!-- recurse to next sibling property i, b or u --&gt; &lt;xsl:when test="count(following-sibling::*[1])=1"&gt; &lt;xsl:apply-templates select="following-sibling::* [index-of($collapsibles, local-name(.))]"/&gt; &lt;/xsl:when&gt; &lt;xsl:otherwise&gt; &lt;!-- escape to text --&gt; &lt;xsl:apply-templates select="parent::w:rPr/ following-sibling::w:t"/&gt; &lt;/xsl:otherwise&gt; &lt;/xsl:choose&gt; &lt;/xsl:element&gt; &lt;/xsl:template&gt; &lt;!-- match run without property --&gt; &lt;xsl:template match="w:r[not(w:rPr)]"&gt; &lt;xsl:apply-templates select="w:t"/&gt; &lt;/xsl:template&gt; &lt;!-- match text --&gt; &lt;xsl:template match="w:t"&gt; &lt;xsl:value-of select="."/&gt; &lt;/xsl:template&gt; &lt;/xsl:stylesheet&gt; </code></pre> <p>When tested again the sample input you gave, the above stylesheet yields</p> <pre><code>&lt;html&gt; &lt;body&gt; &lt;p&gt;&lt;b&gt;This is a bold &lt;i&gt;with a bit of italic&lt;/i&gt; paragraph&lt;/b&gt; with some non-bold in it too. &lt;/p&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>which looks like what you wanted.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload