Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Line breaks in XSLT formatted code documentation (xml)

Tags:

c#

.net

xml

xslt

Our WinForms based application takes documentation files that were generated by Visual Studio (Xml documentation), performs some XSLT transformation and displays the result in a WebBrowser control inside our form.

Problem is, We cannot seem to get line breaks to be displayed, for example for tags in the xml documentation.

For example:

        <member name="T:Genesys.AgentLoginData">
            <summary>
            This is some test summary <br />
            New line here
            </summary>
        </member>

When used with the XSL transformation, the summary text will be truncated into a single line.

For simplification, the transformation does this for selecting the summary text:

<xsl:template match="member" >
<xsl:value-of select="summary" disable-output-escaping="yes" />
</xsl:template>

How can we properly get new lines to be displayed in the WebBrowser control inside our application?

EDIT: Adding the contents of"View Source" from the WebBrowser control. What i'm after is a new line after "This is a method", and so on. The browser simply displays that in a single line.

<?xml version="1.0" encoding="utf-8"?><html xmlns="http://www.w3.org/1999/xhtml"><head><title>Action Help</title><style type="text/css">
          p  {text-indent:200px;}
          li {text-indent:180px;}
          h1 {color:navy;}
          h2 {color:blueviolet}
          h4 {color:navy;}
        </style></head><body><H2 xmlns="">AgentLogin</H2><H1 xmlns=""></H1><h3 xmlns="">
            This is a method 
            And its parameter 
            Check this out
            name</P></body></html>
like image 760
lysergic-acid Avatar asked Jun 25 '12 14:06

lysergic-acid


3 Answers

You're probably looking for xml:space="preserve" directive. See http://msdn.microsoft.com/en-us/library/aa468566.aspx for a decent article on how to handle XSL and whitespace on the .net stack.

Although I just noticed that you're displaying the resulting transform in a web browser control, which may mean that your problems actually with the produced HTML. Try viewing source on what you're displaying in the control and add that to your question.

Looking at the updated comments, and making some guesses, I think your problem is that you're using xsl:value-of, which just gets the text of the node, instead of xsl:copy-of, which would get the text of the node, along with the <br/> elements. Try replacing this:

<xsl:value-of select="summary" disable-output-escaping="yes" />

with

<xsl:copy-of select="summary/node()" />

Your other option would be to escape the <br/> so that it come through as text instead of a child node, and leave disable-output-escaping on.

like image 83
MNGwinn Avatar answered Nov 15 '22 10:11

MNGwinn


You need to learn the difference between <xsl:value-of> and <xsl:copy-of>.

<xsl:value-of> only copies to the output the string value of the specified in the select attribute expression. This excludes any elements (so <br/> is not present in the output).

On the other hand `<xsl:copy-of> copies to the result tree all nodes in the node-set specified in the select attribute.

Therefore the solution is simple:

Replace:

<xsl:template match="member" >
  <xsl:value-of select="summary" disable-output-escaping="yes" />
</xsl:template>

with:

<xsl:template match="member" >
  <xsl:copy-of select="summary/node()"/>
</xsl:template>
like image 29
Dimitre Novatchev Avatar answered Nov 15 '22 10:11

Dimitre Novatchev


Depending on how your generating your original xml, you may need to use html entities for these tags to be properly preserved.

http://en.wikipedia.org/wiki/List_of_XML_and_HTML_character_entity_references

try

    <member name="T:Genesys.AgentLoginData">
        <summary>
        This is some test summary &lt;br /&gt;
        New line here
        </summary>
    </member>
like image 22
ZnArK Avatar answered Nov 15 '22 09:11

ZnArK