xslt - 如何告诉 XSLT 脚本绕过特定文本?
问题描述
如何告诉我的 XSLT 脚本不要复制包含“策略:”的段落?
对不起所有的代码,但这应该包括给我问题的所有信息。
这是我的 XSLT 模板:
<xsl:when test="xhtml:li[@property = 'ktp:answer']">
<xsl:for-each select="xhtml:li[@property = 'ktp:answer']">
<xsl:variable name="answerNum" select="position()"/>
<xsl:variable name="feedbackPara"
select="../following-sibling::xhtml:section[@property = 'ktp:explanation']/xhtml:section[@property = 'ktp:explanation-section'][1]/xhtml:p[$answerNum]"/>
<xsl:variable name="feedbackParaText"
select="../following-sibling::xhtml:section[@property = 'ktp:explanation']/xhtml:section[@property = 'ktp:explanation-section'][1]/xhtml:p[$answerNum]/text()[string-length(normalize-space()) > 1][preceding-sibling::xhtml:span[contains(@class, 'mark-')[last()]] | preceding-sibling::xhtml:b[1]]"/>
<xsl:variable name="correctSpan"
select="../following-sibling::xhtml:section[@property = 'ktp:explanation']/xhtml:section[@property = 'ktp:explanation-section'][1]/xhtml:p[$answerNum]/xhtml:span"/>
<!--<xsl:variable name="tokenizefeedbackPara">
<xsl:value-of select="tokenize(normalize-space($feedbackPara), '\s')"/>
</xsl:variable>-->
<li>
<xsl:apply-templates select="@* | node()"/>
<xsl:choose>
<xsl:when
test="//xhtml:span[@property = 'ktp:interactionType'][node() = 'single-select' or node() = 'multiple-select']">
<section property="ktp:explanation" typeof="ktp:Explanation"
class="ktp-explanation jasper-exclude">
<xsl:attribute name="data-uuid">
<xsl:call-template name="assignID"/>
</xsl:attribute>
<section property="ktp:explanation-section"
typeof="ktp:feedback" data-title="Feedback"
class="ktp-explanation-section">
<xsl:attribute name="data-uuid">
<xsl:call-template name="assignID"/>
</xsl:attribute>
<p>
<xsl:attribute name="data-uuid">
<xsl:call-template name="assignID"/>
</xsl:attribute>
<xsl:choose>
<xsl:when
test="$feedbackPara[xhtml:b | xhtml:span | xhtml:i | xhtml:sup | xhtml:sub]">
<xsl:choose>
<xsl:when test="$feedbackPara[xhtml:i | xhtml:sup | xhtml:sub]">
<xsl:apply-templates
select="$feedbackPara/node()[position() > 2] except $correctSpan"
/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of
select="
substring-after($feedbackParaText, functx:substring-before-match(
$feedbackParaText, '[A-Z0-9]'))"
/>
</xsl:otherwise>
</xsl:choose>
</xsl:when>
<xsl:otherwise>
<xsl:value-of
select="substring-after($feedbackPara, ') ')"/>
</xsl:otherwise>
</xsl:choose>
</p>
</section>
</section>
</xsl:when>
<xsl:otherwise/>
</xsl:choose>
</li>
</xsl:for-each>
</xsl:when>
这是我得到的输出。对于答案选择1,它在“策略:”之后输出内容。我需要它来跳过策略段落,只包括以 1)、2)、3) 和 4) 开头的段落
<ol class="ktp-answer-set">
<li property="ktp:answer" typeof="ktp:Answer">Chef salad, crackers, and iced
tea.
<section property="ktp:explanation" typeof="ktp:Explanation" class="ktp-explanation jasper-exclude">
<section property="ktp:explanation-section" typeof="ktp:feedback" data-title="Feedback" class="ktp-explanation-section">
<p>Recall foods that are
high in protein.
</p>
</section>
</section>
</li>
<li property="ktp:answer" typeof="ktp:AnswerCorrect">Broiled fish, cream of tomato soup
topped with grated cheese, and custard.
<section property="ktp:explanation" typeof="ktp:Explanation" class="ktp-explanation jasper-exclude">
<section property="ktp:explanation-section" typeof="ktp:feedback" data-title="Feedback" class="ktp-explanation-section">
<p>A chef salad contains pieces of ham and cheese, which have protein. Crackers and iced
tea do not contain protein. The majority of foods selected do not contain protein.
</p>
</section>
</section>
</li>
<li property="ktp:answer" typeof="ktp:Answer">Peanut butter and jelly sandwich,
chips, and fruit drink.
<section property="ktp:explanation" typeof="ktp:Explanation" class="ktp-explanation jasper-exclude">
<section property="ktp:explanation-section" typeof="ktp:feedback" data-title="Feedback" class="ktp-explanation-section">
<p>All foods selected contain protein. Protein can be increased by adding skim milk to
appropriate foods, adding grated cheese to foods, using peanut butter as a spread
on fruits and vegetables, using yogurt as a topping for fruit and cake.
</p>
</section>
</section>
</li>
<li property="ktp:answer" typeof="ktp:Answer">Turkey sandwich with lettuce and
tomato, potato salad, and milk.
<section property="ktp:explanation" typeof="ktp:Explanation" class="ktp-explanation jasper-exclude">
<section property="ktp:explanation-section" typeof="ktp:feedback" data-title="Feedback" class="ktp-explanation-section">
<p>Peanut butter contains protein, but the other foods do not.</p>
</section>
</section>
</li>
</ol>
这是所需的输出,它将答案选择 1) 的解释置于正确的答案选择下:
<ol class="ktp-answer-set">
<li property="ktp:answer" typeof="ktp:Answer">Chef salad, crackers, and iced
tea.
<section property="ktp:explanation" typeof="ktp:Explanation" class="ktp-explanation jasper-exclude">
<section property="ktp:explanation-section" typeof="ktp:feedback" data-title="Feedback" class="ktp-explanation-section">
<p>A chef salad contains pieces of ham and cheese, which have protein. Crackers and iced tea do not contain protein. The majority of foods selected do not contain protein.
</p>
</section>
</section>
</li>
<li property="ktp:answer" typeof="ktp:AnswerCorrect">Broiled fish, cream of tomato soup
topped with grated cheese, and custard.
<section property="ktp:explanation" typeof="ktp:Explanation" class="ktp-explanation jasper-exclude">
<section property="ktp:explanation-section" typeof="ktp:feedback" data-title="Feedback" class="ktp-explanation-section">
<p>All foods selected contain protein. Protein can be increased by adding skim milk to appropriate foods, adding grated cheese to foods, using peanut butter as a spread on fruits and vegetables, using yogurt as a topping for fruit and cake.
</p>
</section>
</section>
</li>
<li property="ktp:answer" typeof="ktp:Answer">Peanut butter and jelly sandwich,
chips, and fruit drink.
<section property="ktp:explanation" typeof="ktp:Explanation" class="ktp-explanation jasper-exclude">
<section property="ktp:explanation-section" typeof="ktp:feedback" data-title="Feedback" class="ktp-explanation-section">
<p>Peanut butter contains protein, but the other foods do not.
</p>
</section>
</section>
</li>
<li property="ktp:answer" typeof="ktp:Answer">Turkey sandwich with lettuce and
tomato, potato salad, and milk.
<section property="ktp:explanation" typeof="ktp:Explanation" class="ktp-explanation jasper-exclude">
<section property="ktp:explanation-section" typeof="ktp:feedback" data-title="Feedback" class="ktp-explanation-section">
<p>Turkey and milk contain protein, but potato salad very little if any.</p>
</section>
</section>
</li>
</ol>
这是我只需要复制以 1)、2)、3) 和 4) 开头的文本并跳过策略段落的内容:
<section property="ktp:explanation-section" typeof="ktp:summary" data-title="Summary" class="ktp-explanation-section">
<p><b>Strategy:</b> Recall foods that are high in protein.
</p>
<p>1) A chef salad contains pieces of ham and cheese, which have protein. Crackers and
iced tea do not contain protein. The majority of foods selected do not contain protein.
</p>
<p>2) <b>CORRECT</b> — All foods selected contain protein. Protein can be increased by adding skim milk
to appropriate foods, adding grated cheese to foods, using peanut butter as a spread
on fruits and vegetables, using yogurt as a topping for fruit and cake.
</p>
<p>3) Peanut butter contains protein, but the other foods do not.</p>
<p>4) Turkey and milk contain protein, but potato salad very little if any.</p>
</section>
解决方案
很难从一个示例中对规范进行逆向工程,尤其是因为您甚至没有向我们展示预期的输出。产生一个体面的答案需要知道样式表应该如何处理与所示文档略有不同(或者实际上非常不同)的文档。您提供的代码似乎对内容的短暂细节非常敏感,例如粗体和斜体:但您比我们更了解您的内容,所以也许您可以依赖它。
此外,您的代码包含两个变量 $feedbackPara 和 $feedbackParaText,不清楚这些变量的值是什么。
我不知道我是否真的满足了你的要求,但我希望看到这样的东西:
<xsl:template match="x:section">
<xsl:apply-templates select="x:p"/>
</xsl:template>
<xsl:template match="x:p[contains(., 'Strategy')]"/>
<xsl:template match="x:p">
<xsl:copy-of select="."/>
</xsl:template>
推荐阅读
- ionic4 - Ionic 4 Angular NgIf 绑定
- javascript - 如何在 React 中添加多个不同类型的事件监听器?
- python - 硒刮;尝试将类添加到数据框
- linux - 检查 PBS 作业调度程序上正在运行的脚本的路径
- python - 如何从 FFT 中提取特征?
- c++ - Doxygen 和 C++:链接到两个模板化函数之一
- kotlin - 有没有办法让 TornadoFX 选项卡显示在屏幕底部?
- rust - 如何将 serde_json::error::Error 转换为 reqwest::error::Error?
- c# - 如何序列化堆栈
使用 System.Text.Json 到 JSON 而不反转堆栈? - ios - objective-c 嵌入式 UICollectionViewController 显示 0 项