【问题标题】:Remove new line between two XML nodes using XSLT使用 XSLT 删除两个 XML 节点之间的新行
【发布时间】:2016-03-22 07:11:21
【问题描述】:

我是 XSL 的新手。

我有一个 XHTML 文件,我只希望 <section id="ch01lev2sec01"> 元素和该元素的第一段成为由 Unicode 代码点   分隔的一行。

输入是:

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

【问题讨论】:

  • 发布您到目前为止所尝试的内容。 SO 不是为您编写代码。
  • 无论哪种方式,XML 在语义上都是相同的。你确定你需要这样安排吗?这是XY Problem 的示例吗?
  • 是的,实际上我们已经导出了 XML 文件,然后在 InDesign 中流动。

标签: xslt xhtml


【解决方案1】:

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

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

【讨论】:

  • 非常感谢这个,如果我想在 id 中添加一些正则表达式,你能指导我吗?比如“match="section[@id='ch([0-9]+)lev2sec([0-9]+)']”。我希望它改变所有 lev2 部分。
  • 如果您使用的是 XSLT 2.0,那么您可以使用正则表达式,使用 matches 命令。所以你的比赛看起来像这样:match="section[matches(@id,'ch\d+lev2sec\d+')]
  • 我不知道为什么,但是当我添加&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;html xmlns:svg="http://www.w3.org/2000/svg" xmlns="http://www.w3.org/1999/xhtml" xmlns:m="http://www.w3.org/1998/Math/MathML" xml:lang="en" lang="en"&gt; &lt;head&gt; &lt;title&gt;Content Literacy and the Reading Process&lt;/title&gt; &lt;link rel="stylesheet" type="text/css" title="day" href="../css/m.css"/&gt; &lt;meta name="dcterms.conformsTo" content="ProductLevelReuse"/&gt; &lt;/head&gt; 然后它在删除时没有转换然后转换有什么问题?可能是由于 属性?
  • 这可能是因为您添加了默认命名空间xmlns="http://www.w3.org/1999/xhtml" ,这意味着XML 中所有未加前缀的元素都将位于该命名空间中。我的 xslt 解决方案只会匹配没有命名空间中的元素。
  • 那我该怎么办,来处理这种情况?
猜你喜欢
  • 1970-01-01
  • 2015-08-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多