xslt - How to just get plain text and line breaks using XSL -


with input

<?xml version="1.0" encoding="utf-8"?> <data>  senstence    sentence  <section>         <!--comment --><h2>my h2</h2>     <p>some paragraph</p>             <p>another paragraph</p>                      </section> </data> 

i need apply xsl style sheet obtain plain text, honor line breaks, , remove preceeding white space. so, after searching online few samples, tried this, not work me. sorry, im not familiar xsl , thought i'd ask.

attempted xsl, not work. ideas?

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/xsl/transform">     <xsl:output method="text" encoding="utf-8"/>     <xsl:strip-space elements="*" />          <xsl:template match ="@* | node()">             <xsl:copy>                 <xsl:apply-templates select="@* | node()"/>             </xsl:copy>             </xsl:template>          <xsl:template match="h1|h2">             <xsl:text>             </xsl:text>             <xsl:copy>                 <xsl:apply-templates select="@* | node()"/>             </xsl:copy>           </xsl:template> </xsl:stylesheet> 

this output after applying xsl. can see, 1 line, not carriage returns.

this sentence sentence m h2some paragraphtanother paragraph 

this output i'd get. text within h1|h2|h3 should have line break before , after.

this sentence  sentence   h2  paragraph paragraph 

you need xml:space="preserve" attribute maintain carriage return within xml:text, , need carriage return before , after content of h1 , h2 tags:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/xsl/transform">   <xsl:output method="text" encoding="utf-8"/>   <xsl:strip-space elements="*" />    <xsl:template match ="@* | node()">     <xsl:copy>       <xsl:apply-templates select="@* | node()"/>     </xsl:copy>   </xsl:template>    <xsl:template match="h1|h2">     <xsl:text xml:space="preserve"> </xsl:text>     <xsl:copy>       <xsl:apply-templates select="@* | node()"/>     </xsl:copy>     <xsl:text xml:space="preserve"> </xsl:text>   </xsl:template> </xsl:stylesheet> 

the initial text (this senstence, this sentence) output correctly on separate lines in case (using visual studio 2012 execute xslt).

you write h tags should have carriage return added - in sample some paragraph , another paragraph in p tags, no carriage returns added , output on same line.


Comments