title: Copy text and replace character in XSL
body: <p>I'm transforming a DITA document to a simplified, formatting-based XML to be used as an import into Adobe InDesign. My transformation is going really well, except for one element which omits the text in the output. The element is <code>codeblock</code>. When I don't have a template specifying it at all, the element and any child elements are passed through to the new XML document, but none of the text is passed through. This element should be passed through with text <strong>and</strong> child elements like every other element in my document for which a specific template is not defined. There's nothing anywhere else in the XSL stylesheet that specifies <code>codeblock</code> or any of its attributes. I am completely stumped and cannot figure out what's going on here.</p>
  <p>It is also worth noting that a number of inline elements (<code>cmdname</code>, <code>parmname</code>, <code>userinput</code>, etc.) are converted to <code>bold</code> on output. The downstream XML is for formatting and does not need to know semantic context.</p>
  <p>This is what I'm trying to pass through:</p>
  <pre><code>&lt;codeblock&gt;This is the first line of my code block.
  This is my second line to prove that line feeds are preserved.
  This line proves that &lt;parmname&gt;child elements&lt;/parmname&gt; are passed through.&lt;/codeblock&gt;
  </code></pre>
  <p>With no template defined for <code>codeblock</code>, this is what I get as a result:</p>
  <pre><code>&lt;codeblock&gt;&lt;bold/&gt;&lt;/codeblock&gt;
  </code></pre>
  <p>The actual result I want is:</p>
  <pre><code>&lt;codeblock&gt;This is the first line of my code block.&amp;#8232;This is my second line to prove that line feeds are preserved.&amp;#8232;This line proves that &lt;bold&gt;child elements&lt;/bold&gt; are passed through.&lt;/codeblock&gt;
  </code></pre>
  <p>I need the line feeds replaced with character entities because InDesign sees any new line that does not start with an element as a column break. My goal was to simply replace the line feed character with <code>&amp;#8232;</code> with the following template:</p>
  <pre><code>&lt;xsl:template match=&quot;codeblock//text()&quot;&gt;
    &lt;xsl:analyze-string select=&quot;.&quot; regex=&quot;(&amp;#10;)&quot;&gt;
      &lt;xsl:matching-substring&gt;
        &lt;xsl:choose&gt;
          &lt;xsl:when test=&quot;regex-group(1)&quot;&gt;&amp;#8232;&lt;/xsl:when&gt;                
        &lt;/xsl:choose&gt;
      &lt;/xsl:matching-substring&gt;
    &lt;/xsl:analyze-string&gt;
  &lt;/xsl:template&gt;
  </code></pre>
  <p>But what I get is:</p>
  <pre><code>&lt;codeblock&gt;&amp;#8232;&lt;bold/&gt;&amp;#8232;&lt;/codeblock&gt;
  </code></pre>
  <p>I was finally able to pass the text through using this template:</p>
  <pre><code>&lt;xsl:template match=&quot;codeblock//text()&quot;&gt;
    &lt;xsl:copy/&gt;
  &lt;/xsl:template&gt;
  </code></pre>
  <p>Success! Incidentally, I have to match at any level under codeblock so it includes the text of the child parmname element too. Since I was able to successfully pass it through with <code>&lt;xsl:copy&gt;</code>, I tried this to pass the text through while replacing the line feed at the same time:</p>
  <pre><code>&lt;xsl:template match=&quot;codeblock//text()&quot;&gt;
    &lt;xsl:copy&gt;
      &lt;xsl:analyze-string select=&quot;.&quot; regex=&quot;(&amp;#10;)&quot;&gt;
        &lt;xsl:matching-substring&gt;
          &lt;xsl:choose&gt;
            &lt;xsl:when test=&quot;regex-group(1)&quot;&gt;&amp;#8232;&lt;/xsl:when&gt;                
          &lt;/xsl:choose&gt;
        &lt;/xsl:matching-substring&gt;
      &lt;/xsl:analyze-string&gt;
    &lt;/xsl:copy&gt;
  &lt;/xsl:template&gt;
  </code></pre>
  <p>But now it won't replace the new line feed. Instead, I get this (which is what I would expect to get without any template defined):</p>
  <pre><code>&lt;codeblock&gt;This is the first line of my code block.
  This is my second line to prove that line feeds are preserved.
  This line proves that &lt;bold&gt;child elements&lt;/bold&gt; are passed through.&lt;/codeblock&gt;
  </code></pre>
  <p>I know this is a long and somewhat convoluted question. I just feel like if I could resolve the issue of why it's not passing the text through in the first place, the rest would be fairly straightforward. And I'm sorry, I can't provide my source XML or XSL as it's under NDA, but if you need more, let me know and I'll try to provide it. (My XSL stylesheets are made up of 12 different files, so there's no way for me to provide all of it, even if genericized.)</p>
  <p>Any suggestions for what I might look for in my stylesheet that might explain why the text is coming through or any suggestions for how to force it through as I did with <code>&lt;xsl:copy&gt;</code> while still replacing the line feeds will be <strong>greatly</strong> appreciated!</p>
  <p><strong>Edited to add:</strong> It has occurred to me that the reason it's not doing the replacement is that it looks like it's not actually a line feed character. It's more like a new line in the code than a line feed character (or hard return) in the text. I think I might need to normalize the text while inserting the <code>&amp;#8232;</code> character at the end of each line. Still investigating, but suggestions are welcome!</p>
  <p><strong>Edited with update:</strong> Thanks to the post <a href="https://stackoverflow.com/questions/14093485/how-to-detect-line-breaks-in-xslt">How to detect line breaks in XSLT</a>, I have gotten closer, but still not quite where I need to be. With this code, I'm able to detect line feeds in the XML and insert the line break character for InDesign:</p>
  <pre><code>&lt;xsl:template match=&quot;codeblock//text()&quot;&gt;
    &lt;xsl:for-each select=&quot;tokenize(., '\n?')[.]&quot;&gt;
      &lt;xsl:sequence select=&quot;.&quot;/&gt;
      &lt;xsl:text&gt;&amp;#8232;&lt;/xsl:text&gt;
    &lt;/xsl:for-each&gt;
  &lt;/xsl:template&gt;
  </code></pre>
  <p>However, it also inserts the line feed character at the end of the string, even if it's not the end of the line. For instance, I now get:</p>
  <pre><code>&lt;codeblock&gt;This is the first line of my code block.&amp;#8232;This is my second line to prove that line feeds are preserved.&amp;#8232;This line proves that &amp;#8232;&lt;bold&gt;child elements&amp;#8232;&lt;/bold&gt; are passed through.&amp;#8232;&lt;/codeblock&gt;
  </code></pre>
  <p>I don't want the line feed character in front of the 'bold' start and end tags or the <code>codeblock</code> end tag. I just want it to appear where there's an actual new line. I tried replacing <code>\r</code> but that just ignored the new lines and just put it in front of the tags. Does anyone know of another escape character that would work here?</p>