The situation
In my XML files, I could have peace of code to show inside the tag <code>
. But the indentation of my XML document is in conflict with the tabulation inside the <code>
section.
Minimal Working Example
The XML file
<article>
<code lang="c">
#include <stdio.h>
int main() {
// printf() displays the string inside quotation
printf("Hello, World!");
return 0;
}
</code>
</article>
The peace of XSLT
<xsl:template match="code">
<pre><xsl:value-of select="."/></pre>
</xsl:template>
The expected HTML rendering
<pre> #include <stdio.h>
int main() {
// printf() displays the string inside quotation
printf("Hello, World!");
return 0;
}</pre>
Explanations
As you see, the goal is to ignore the n first tabulations and the n last tabulation (if any) inside the tags, when n is equal to the number of tabulation before the opening tag <code>
. And also to ignore the first new line, and the last new line (the one just before the tabulations before the closing </code>
tag).
More explanations
According to @michael.hor257k suggestion to bring more clarifications, in other terms, the XSLT style sheet should treat the XML <code>
part shown above like if it was like this:
<article>
<code lang="c">#include <stdio.h>
int main() {
// printf() displays the string inside quotation
printf("Hello, World!");
return 0;
}</code>
</article>
As you see the tabs bellonging to the XML indentation should not be included in the final HTML <pre>
tag.
In more graphical way, we can say that the tabs corresponding to the tabs commented bellow should be ignored in the processing:
<article>
<code lang="c"><!--
-->#include <stdio.h>
<!-- -->int main() {
<!-- --> // printf() displays the string inside quotation
<!-- --> printf("Hello, World!");
<!-- --> return 0;
<!-- -->}<!--
--></code>
</article>
An this spaces, tabs, and new lines are corresponding to the XML indentation and not to the internal C code indentation.
Conclusion — Question
So, is it possible in my XSLT to parse the number of tabs before the opening <code>
tag in order to delete them from the beginning of each content’s line?