Warm tip: This article is reproduced from stackoverflow.com, please click
xslt

How do I get certain text to appear when a condition is met using XSLT?

发布于 2020-04-11 11:47:03

I'm trying to get Incorrect and Correct to appear at the front of a paragraph when a condition is met.

In the HTML below, in <li property="ktp:answer"> when typeof="ktp:Answer is present I need the output for the explanation to start with 'Incorrect' and when typeof="ktp:AnswerCorrect is present the output for the explanation needs to start with 'Correct'.

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

Here's the part of my XSLT script that transforms this portion of the text. The Incorrect and Correct portion is not working correctly.

<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>
Questioner
mjwolff1
Viewed
37
Martin Honnen 2020-02-02 04:37

I would suggest to write template with match patterns that define the check you have given in your verbal description but in your example it seems instead of test="//xhtml:li[@typeof='ktp:Answer']" you want to check test="ancestor::xhtml:li[@typeof='ktp:Answer']".