0

我有以下一组 XML 列表的代码,我正在使用以下 XSLT 来提取内容,但它不起作用。第一个和最后一个工作正常,但中间一个会产生问题。任何帮助请......

XML:

    <cl:sect1 identifier="ZQRRUG590702676" number="nonumber">
    <cl:complex-meta>
    <cl:title identifier="PLHOOC548347957">Questions for Review and Reflection</cl:title>
    </cl:complex-meta>
    <cl:quiz identifier="UUHXZX749864214">
    <cl:metadata-wrapper>
    <cl:descriptive-meta>
    <cl:broad-term>Chapter exercises</cl:broad-term>
    </cl:descriptive-meta>
    </cl:metadata-wrapper>
    <cl:question-section identifier="LMUGHP665075315">
    <cl:short-answer-section identifier="PDWZPT096484135">
    <cl:quiz-meta>
    <cl:numbering role="style" target-node="question">arabic</cl:numbering>
    </cl:quiz-meta>
    <cl:sa-item identifier="DHUJEX954944222">
<cl:question identifier="ZXPROB877164447"><cl:para identifier="UVPIYI046459379">Choose one of the theoretical perspectives on the family, and discuss how you might use it to understand something about life in <cl:style identifier="XWHGPG736884704" styles="italic">your</cl:style> family.</cl:para></cl:question>
    </cl:sa-item>
    <cl:sa-item identifier="XLIHNY049827846">
    <cl:question identifier="WBNMFT023816273"><cl:para identifier="MVVFTE506398129">Choose a magazine photo and analyze its content from one of the perspectives de-scribed in this chapter. Then analyze the photo from another theoretical perspective. How do your insights differ depending on which theoretical perspective is used?</cl:para></cl:question>
    </cl:sa-item>
    <cl:sa-item identifier="KHOISZ206843547">
    <cl:question identifier="YIRLTY425252173"><cl:para identifier="DEAOVV858913951">Discuss why science is often considered a better way to gain knowledge than is personal experience alone. When might this not be the case?</cl:para></cl:question>
    </cl:sa-item>
    <cl:sa-item identifier="UZUXJL108947882">
    <cl:question identifier="NOBHFC190777617"><cl:para identifier="CISHBM348415557">Think of a research topic, then review the data-gathering techniques described in this chapter to decide which of these you might use to investigate your topic.</cl:para></cl:question>
    </cl:sa-item>
    <cl:sa-item identifier="ZITOCE439682847">
    <cl:question identifier="MCRAPB388676722"><cl:para identifier="RTRSPD372632657"><cl:key-term-entry identifier="JTBDZT820091835"><cl:key-term identifier="VSNZVL072560329">Policy Question</cl:key-term></cl:key-term-entry>. What aspect of family life would it be helpful for policy makers to know more about as they make law and design social programs? How might this topic be researched? Is it controversial?</cl:para></cl:question>
    </cl:sa-item>
    </cl:short-answer-section>
    </cl:question-section>
    </cl:quiz>
    </cl:sect1>

XSLT:

<xsl:template match="cl:closer/cl:sect1/cl:quiz/cl:question-section/cl:short-answer-section/cl:sa-item[1]/cl:question[1]/cl:para">
                <xsl:element name="ParagraphStyleRange">
                    <xsl:attribute name="AppliedParagraphStyle">ParagraphStyle/EOCNLF</xsl:attribute>
                    <xsl:apply-templates/>
                </xsl:element>
</xsl:template>
<xsl:template match="cl:closer/cl:sect1/cl:quiz/cl:question-section/cl:short-answer-section/cl:sa-item[not(position() = last())]/cl:question[not(position()=last())]/cl:para">
                <xsl:element name="ParagraphStyleRange">
                    <xsl:attribute name="AppliedParagraphStyle">ParagraphStyle/EOCNLM</xsl:attribute>
                    <xsl:apply-templates/>
                </xsl:element>
</xsl:template>
<xsl:template match="cl:closer/cl:sect1/cl:quiz/cl:question-section/cl:short-answer-section/cl:sa-item[position()=last()]/cl:question[position()=last()]/cl:para">
                <xsl:element name="ParagraphStyleRange">
                    <xsl:attribute name="AppliedParagraphStyle">ParagraphStyle/EOCNLL</xsl:attribute>
                    <xsl:apply-templates/>
                </xsl:element>
</xsl:template>
4

1 回答 1

0

您的所有sa-item元素都只有一个question孩子,因此

cl:sa-item[not(position() = last())]/cl:question[not(position()=last())]

永远不会匹配任何东西(因为在question谓词中两者都position()last()始终为 1)。您可能只需要删除question步骤上的谓词,即让您的模板只检查 的索引sa-item,而不是question元素。最干净的方法是优先考虑:

<xsl:template priority="3" match="cl:sa-item[1]/cl:question/cl:para">
  <!--...-->
</xsl:template>

<xsl:template priority="2" match="cl:sa-item[last()]/cl:question/cl:para">
  <!--...-->
</xsl:template>

<xsl:template priority="1" match="cl:sa-item/cl:question/cl:para">
  <!--...-->
</xsl:template>

这样,第一个模板将拾取第一个,第二个sa-item拾取last()(假设它不是第一个,如果只有一个项目),第三个将拾取第一个未消耗的任何其他模板两个更高优先级的模板。short-answer-section

于 2013-06-10T15:02:30.363 回答