使用XSLT删除两个XML节点之间的新行

时间:2016-03-22 07:11:22

标签: xslt xhtml

我是XSL的新手。

我有一个XHTML文件,我只想要<section id="ch01lev2sec01">元素,并且该元素的第一个段落成为由UniCode代码点&#X2003;分隔的一行。

输入是:

<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>

所需的输出是:

<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>

1 个答案:

答案 0 :(得分:1)

假设您使用的是身份模板,则需要一个模板来匹配section元素之前p元素的子文本节点。

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

在此模板中,您可以输出额外的字符。

试试这个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>