温馨提示:本文翻译自stackoverflow.com,查看原文请点击:其他 - How do I get certain text to appear when a condition is met using XSLT?
xslt

其他 - 使用XSLT满足条件时,如何显示某些文本?

发布于 2020-04-11 13:14:31

当出现条件时,我想使“不正确”和“正确”出现在段落的开头。

在下面的HTML中,在出现的<li property="ktp:answer">时候typeof="ktp:Answer,我需要输出以“不正确”开头,而在typeof="ktp:AnswerCorrect出现的时候,输出需要以“正确”开头。

<ol class="ktp-answer-set">               
               <li property="ktp:answer" typeof="ktp:Answer">“I cough in the morning, but it stops.”

                  <section property="ktp:explanation" typeof="ktp:Explanation" class="ktp-explanation jasper-exclude">

                     <section property="ktp:explanation-section" typeof="ktp:feedback" class="ktp-explanation-section" data-title="Feedback">

                        <p>Captopril is an angiotensin-converting enzyme inhibitor (ACE-I) that has a dry, hacking, persistent cough as an adverse effect. The client reports a morning cough that may or may not be associated with the medication. 
                        </p>                        
                     </section>                     
                  </section>                  
               </li>               
               <li property="ktp:answer" typeof="ktp:AnswerCorrect">“I am waiting to learn the results of my pregnancy test.”

                  <section property="ktp:explanation" typeof="ktp:Explanation" class="ktp-explanation jasper-exclude">

                     <section property="ktp:explanation-section" typeof="ktp:feedback" class="ktp-explanation-section" data-title="Feedback">

                        <p>ACE-Is are contraindicated in pregnancy. There is a black box warning that states ACE-I can cause injury and even death to a developing fetus. Since the client reports waiting for the results of a pregnancy test, this should be reported immediately to the health care provider. 
                        </p>                        
                     </section>                     
                  </section>                 
               </li>
</ol>

这是XSLT脚本的一部分,可以转换文本的这一部分。不正确和正确部分无法正常工作。

<section property="ktp:explanation-section" typeof="ktp:feedback" data-title="Feedback"
                class="ktp-explanation-section atom-exclude">
                <xsl:for-each select="//xhtml:section[@data-title='Feedback']">
                    <p class="atom-exclude">
                        <xsl:value-of select="position()"></xsl:value-of>
                        <xsl:text>)</xsl:text>
                        <xsl:choose>
                            <xsl:when test="//xhtml:li[@typeof='ktp:Answer']">

                                <span class="mark-false"><xsl:text> INCORRECT</xsl:text></span> – <xsl:value-of select="."/>          
                </xsl:when>
                         <xsl:otherwise>
                             <span class="mark-true"><xsl:text> CORRECT</xsl:text></span> – <xsl:value-of select="."/>
                         </xsl:otherwise>
            </xsl:choose>
                    </p>

                </xsl:for-each>
            </section>

查看更多

提问者
mjwolff1
被浏览
43
Martin Honnen 2020-02-02 04:37

我建议编写带有匹配模式的模板,该模板定义您在语言描述中给出的支票,但是在您的示例中,它似乎不是test="//xhtml:li[@typeof='ktp:Answer']"您想要的支票test="ancestor::xhtml:li[@typeof='ktp:Answer']"