开发者

XSLT - output to html a specific XML element which contains formatted text - output should be same

开发者 https://www.devze.com 2023-03-22 15:03 出处:网络
I have an xml element which contains formatted text data : <MESSAGE> <TranslationReport> Translation Report

I have an xml element which contains formatted text data :

   <MESSAGE>
      <TranslationReport>
Translation Report
==================
Contains errors ? true
Contains warnings ? false
There are 9 entries in the report

I want the 开发者_运维技巧results of my xslt (output to html) to match the contents of TranslationReport EXACTLY. Everything I do just takes a data (all in one line - see below). This seems easy, but I have searched in all of my books and everywhere else...

Translation Report ================== Contains errors ? true Contains warnings ? false There are 9 entries in the report


Have you tried an <xsl:output method="text"/> tag and/or enclosing the literal text in <xsl:text>...</xsl:text> tags?

If you are rendering the results in HTML, the problem is that HTML does not render newlines without enclosing the output in a tag like <pre>. Output a <pre> tag wrapping your text output in your XSLT.


The problem you are running into may have to do with the fact that in XML, different forms of white space (linebreaks, spaces, etc.) are considered equivalent. See this answer: Is it "bad practice" to be sensitive to linebreaks in XML documents?


If you are going to render to html you have two options:

  • use pre tag to render your text in the browser exactly as is.
  • parse your text with some advanced XPath 2.0 function and handle each text line as required.

Here the first option silly example:

<xsl:stylesheet version="2.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:output method="html"/>

    <xsl:template match="MESSAGE/TranslationReport">
        <html>
            <body>
                <pre>
                    <xsl:value-of select="."/>
                </pre>
            </body>
        </html>
    </xsl:template>

</xsl:stylesheet>

In the second option we will parse your text with the XPath 2.0 function tokenize, splitting all lines and wrapping each one with the eanted tag.

This is the silly example:

<xsl:stylesheet version="2.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:output method="html"/>

    <xsl:template match="MESSAGE/TranslationReport">
        <html>
            <body>
                <xsl:for-each select="tokenize(.,'\n')
                    [not(position()=(1,last()))]">
                    <p class="TranslationReport">
                        <xsl:value-of select=".[position()]"/>
                    </p>
                </xsl:for-each>
            </body>
        </html>
    </xsl:template>

</xsl:stylesheet>

In the second case the output will be:

<html>
    <body>
        <p class="TranslationReport">Translation Report</p>
        <p class="TranslationReport">==================</p>
        <p class="TranslationReport">Contains errors ? true</p>
        <p class="TranslationReport">Contains warnings ? false</p>
        <p class="TranslationReport">There are 9 entries in the report</p>
    </body>
</html>
0

精彩评论

暂无评论...
验证码 换一张
取 消