Nikhil Ranjan
Nikhil Ranjan

Reputation: 138

Remove new line between two XML nodes using XSLT

I am very new with XSL.

I have one XHTML file and I want merely the <section id="ch01lev2sec01"> element and the first paragraph of that element to become one line that is separated by the UniCode codepoint &#X2003;.

The input is:

<section class="bodymatter" id="ch01body">
  <section id="ch01lev1sec01">
    <header>
      <h1 class="title">Assumptions Underlying Content Teaching</h1> 
    </header>
    <p>Most content area teachers assume it is their responsibility to cover their subject matter in a timely, accurate, and effective manner (<a class="biblioref" href="REF.xhtml#ch01bib033">Alvermann &amp; Moore, 1991</a>; <a class="biblioref" href="REF.xhtml#ch01bib034">Moore, 1996</a>). They also assume, for the most part, that textbooks are necessary for teaching and learning content (<a class="biblioref" href="REF.xhtml#ch01bib035">Wade &amp; Moje, 2000</a>). Finally, content area teachers tend to assume that by the time students enter middle and/or high school, they are strategic in their approach to reading and learning (<a class="biblioref" href="REF.xhtml#ch01bib036">Alvermann &amp; Nealy, 2004</a>). These assumptions influence teachers’ instructional decision making, their use of textbooks, and their perceptions of active and independent readers.</p>
    <section id="ch01lev2sec01">
      <header>
        <h1 class="title">Subject Matter</h1>
      </header>
      <p>The historical</p>
    </section>
  </section>
</section>

And the required output is:

<section class="bodymatter" id="ch01body">
  <section id="ch01lev1sec01">
    <header>
      <h1 class="title">Assumptions Underlying Content Teaching</h1>
    </header>
    <p>Most content area teachers assume it is their responsibility to cover their subject matter in a timely, accurate, and effective manner (<a class="biblioref" href="REF.xhtml#ch01bib033">Alvermann &amp; Moore, 1991</a>; <a class="biblioref" href="REF.xhtml#ch01bib034">Moore, 1996</a>). They also assume, for the most part, that textbooks are necessary for teaching and learning content (<a class="biblioref" href="REF.xhtml#ch01bib035">Wade &amp; Moje, 2000</a>). Finally, content area teachers tend to assume that by the time students enter middle and/or high school, they are strategic in their approach to reading and learning (<a class="biblioref" href="REF.xhtml#ch01bib036">Alvermann &amp; Nealy, 2004</a>). These assumptions influence teachers’ instructional decision making, their use of textbooks, and their perceptions of active and independent readers.</p>
    <section id="ch01lev2sec01">
      <header>
        <h1 class="title">Subject Matter</h1>
      </header>&#x2003;
      <p>The historical</p>
    </section>
  </section>
</section>

Upvotes: 0

Views: 622

Answers (1)

Tim C
Tim C

Reputation: 70638

Assuming you are using the identity template, you need a template to match the child text node of the section element before the first p element.

<xsl:template match="section[@id='ch01lev2sec01']/text()[not(preceding-sibling::p) and following-sibling::p]">

In this template you can then just output your extra character.

Try this XSLT

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:output method="xml" indent="no" />

    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="section[@id='ch01lev2sec01']/text()[not(preceding-sibling::p) and following-sibling::p]">
        <xsl:value-of select="normalize-space()" />
        <xsl:text disable-output-escaping="yes">&#x2003;</xsl:text>
    </xsl:template>
</xsl:stylesheet>

Upvotes: 1

Related Questions