首页 > 解决方案 > 如何告诉 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()) &gt; 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() &gt; 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>

标签: xslt

解决方案


很难从一个示例中对规范进行逆向工程,尤其是因为您甚至没有向我们展示预期的输出。产生一个体面的答案需要知道样式表应该如何处理与所示文档略有不同(或者实际上非常不同)的文档。您提供的代码似乎对内容的短暂细节非常敏感,例如粗体和斜体:但您比我们更了解您的内容,所以也许您可以依赖它。

此外,您的代码包含两个变量 $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>

推荐阅读