xslt

Prevent <li> tag that shouldn't be there from coming over using XSLT transform


I'm having trouble getting my XSLT script to not pull over an <li> tag that's in my original input file. Please see red circled area in screenshot below from my output:

output file showing unwanted tag

The section of my script I'm having trouble with is below:

<xsl:template match="xhtml:ol[@property = 'ktp:questionSet']">
    <xsl:param name="path"/>
    <xsl:variable name="content-item-name"
        select="//xhtml:section/xhtml:span[@property = 'atom:content-item-name']/@data-value"/>      
    <li class="ktp-question" property="ktp:question" typeof="ktp:Question">                  
        <section class="ktp-question-set-meta">                                     
            <xsl:apply-templates
                select="(//xhtml:section[@class = 'ktp-meta' and not(@property = 'ktp:questionSetType')])[position() = 3 or position() = 4]"/>               
        </section>
        <section property="ktp:stimulus" typeof="ktp:Stimulus" class="ktp-stimulus">
            <xsl:apply-templates
                select="xhtml:li[@class = 'ktp-stimulus']/node()"/>
        </section>                      
        <section class="ktp-question-stem">
            <xsl:apply-templates
                select="//xhtml:section[@class = 'ktp-question-stem']/node()"/>
        </section>
        <ol class="ktp-answer-set">
            <xsl:apply-templates
                select="//xhtml:ol[contains(@class, 'ktp-answer-set')]/node()"/>
        </ol>
        <section property="ktp:explanation" typeof="ktp:Explanation"
            class="ktp-explanation">
            <xsl:apply-templates
                select="//xhtml:section[@class = 'ktp-explanation']/node()"/>
        </section>
    </li>
</xsl:template>

The input file I'm working is below:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta charset="utf-8" />
        <link href="../../assets/css/main.css" rel="stylesheet" title="default" type="text/css" />
        <title>ptalme03</title>
    </head>
    <body>
        <ol class="ktp-question-set" >
            <li><ol property="ktp:questionSet" typeof="ktp:QuestionSet" class="ktp-question-set">
                    <li class="ktp-question-set-meta"><section property="ktp:metadata"
                            class="ktp-meta">
                            <span property="atom:content-item-name" class="ktp-meta"
                                data-value="ptalme03"></span>
                        </section><section property="ktp:tags" class="ktp-meta">
                            <span property="ktp:questionSetType" class="ktp-meta"
                                >shared-stimulus</span>
                        </section></li>
                    <li property="ktp:stimulus" typeof="ktp:Stimulus" class="ktp-stimulus"><p>
                            <img src="../../img/alpha_images_pt1/SAT_PT_math2_Q3.svg" />
                        </p></li>
                    <li property="ktp:question" typeof="ktp:Question" class="ktp-question">
                        <span property="atom:content-item-name" class="ktp-meta" data-value="ptalme03"></span>
                        <section class="ktp-question-meta">
                            <section class="ktp-meta" property="ktp:metadata">
                                <span class="ktp-meta" data-value="ptalme03"
                                    property="atom:content-item-name"></span>                               
                            </section>
                            <section class="ktp-meta" property="ktp:tags">
                                <span class="ktp-meta" property="ktp:interactionType"
                                    >single-select</span>
                                <span class="ktp-meta" property="OLD QID">ftm02.03</span>                               
                            </section>
                        </section><section class="ktp-question-stem">
                            <p>What is the <i>x</i>-intercept of the graph shown? </p>
                        </section><ol class="ktp-answer-set">
                            <li property="ktp:answer" typeof="ktp:AnswerCorrect">(−1, 0)</li>                           
                        </ol><section property="ktp:explanation" typeof="ktp:Explanation"
                            class="ktp-explanation">
                            <section class="ktp-explanation-section" data-title="Feedback"
                                data-uuid="e955d9441cc051f966008c9d6da75adc"
                                property="ktp:explanation-section" typeof="ktp:feedback">
                                <p><b>Getting to the Answer:</b> The <i>x</i>-intercept is the point
                                    where the graph crosses the <i>x</i>-axis. The graph crosses the
                                        <i>x</i>-axis at (−1, 0). <b>(A)</b> is correct. </p>
                            </section>
                        </section>
                    </li>
                </ol>
            </li>
        </ol>
    </body>
</html>

Solution

  • Looking at the XSLT template you included in your question, the only li element Which it produces in its output is:

    <li class="ktp-question" property="ktp:question" typeof="ktp:Question">
    ...
    </li>
    

    In the image of the output which you posted, the above element is a child element of the unwanted <li> element which has no attributes. So the unwanted <li> element which you circled in the image could not have been produced by the template which you included in your question. It must be produced by some other template, which you did not include.

    Almost certainly that <li> in the output is a copy of the <li> which appears in the input file as the child element of a <ol class="ktp-question-set" > element, and is also the parent of an element which is elided by your stylesheet: the <ol property="ktp:questionSet" typeof="ktp:QuestionSet" class="ktp-question-set">. I'd guess that elision is caused by a template that matches that ol and fails to copy the element before applying templates to its children, so you end up with an li as a child of another li. If you actually do want to elide the ol, you will have to also elide its parent li.