Question

I have the following XML:

<node><mtype>code</mtype><mtext>&lt;script&gt;
 // a Javascript example
 alert("Hello World");
 &lt;/script&gt;</mtext></node>

The same text in HTML would be written as follows:

<pre><code>&lt;script&gt;
 // a Javascript example
 alert("Hello World");
 &lt;/script&gt;</code></pre>

We can see that the actual text is HTML encoded, which is fine. What I want to do next is use the following XSLT to represent the above mtext as the <code> element in Word XML (with a specific style).

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" 
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:w="http://schemas.microsoft.com/office/word/2003/wordml"
  >

  <xsl:template match="/">
    <xsl:for-each select="/node">
    <xsl:if test="mtype = 'code'">
      <w:p>
        <w:pPr><w:jc w:val="left" /></w:pPr>
        <!--<xsl:text select="mtext/node()"/>-->
        <w:r><w:t><xsl:value-of select="mtext/node()" disable-output-escaping="yes"/></w:t></w:r>
      </w:p>
    </xsl:if>
    </xsl:for-each>
  </xsl:template>
</xsl:stylesheet>

If I run the XLST agains the defined XML I get the following:

$ xsltproc code.xslt code.xml
<?xml version="1.0"?>
<w:p xmlns:w="http://schemas.microsoft.com/office/word/2003/wordml"><w:pPr><w:jc w:val="left"/></w:pPr><w:r><w:t><script>
 // a Javascript example
 alert("Hello World");
 </script></w:t></w:r></w:p>

This isn't what I would like to achieve. The first thing are the actual special characters and the more important thing are the new lines that are stipped. Whatever I use in the <w:t> element is visible in one line only, but my <code> text has multiple lines, which is why I also need multiple lines.

I've heard that I could use <xsl:text> or <![CDATA[ ... ]]>, but I wasn't able to produce the XML that would actually create the multiline <code> sections in Microsoft Word.

Edited: Jakob, if I uses that I got no newlines. The ending XML is as follows, which doesn't print newlines:

<w:p wsp:rsidR="005963F5" wsp:rsidRDefault="00804A03" wsp:rsidP="00804A03" xml:space="preserve">
<w:pPr><w:jc w:val="left"/><w:pStyle w:val="VirisCode"/></w:pPr>
<w:r><w:t>
     &lt;script&gt;
     // a Javascript example
     alert("Hello World");
     &lt;/script&gt;
 </w:t></w:r>
</w:p>

Comment: I DON'T KNOW what the output should look like, since I'm not the XML guru; if I knew that I would probably be able to write that myself. So the XML can look whatever it likes as long as it works. By works I mean that it displays the <code> blocks the way I want. I want the code blocks to be displayed exactly as they appear in the <mtext> XML node, with the new lines and everything. In this case the exact output (once already opened with Word) should show the following:

<script>
// a Javascript example
alert("Hello World");
</script>

Note that the special characters are displayed correctly and the newlines are present. The problem is with the <w:t> element that displays everything in one line, which is what I don't want, but don't know what else to use.

No correct solution

OTHER TIPS

use the xml:space on the xsl:output, template, element or value-of lvl for preserving newlines/space.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top