XSLT 3.0流模式下仅验证指定课程编号的开课匹配问题
XSLT 3.0流模式下指定课程校验修正方案
核心修正思路
要解决输出包含其他课程结果的问题,关键在于精准定位目标课程(编号00151744),仅对该课程的Instructor_Led_Webinar_Lesson_Data标题进行校验,同时遵循XSLT 3.0流模式的单遍读取规则。
修正后的XSLT代码
<?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="3.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <!-- 启用流处理模式 --> <xsl:mode streamable="yes"/> <!-- 累加器:缓存Offerings中所有开课的标题,供后续校验使用 --> <xsl:accumulator name="offering-titles" as="xs:string*" initial-value="()"> <xsl:accumulator-rule match="Offerings/Offering/Title" select="$value || string(.)"/> </xsl:accumulator> <!-- 主模板:匹配根节点,仅处理目标课程 --> <xsl:template match="/"> <ErrorReport> <!-- 精准定位课程编号为00151744的课程节点 --> <xsl:apply-templates select="AllCourses/Course[CourseNumber='00151744']/Instructor_Led_Webinar_Lesson_Data"/> </ErrorReport> </xsl:template> <!-- 处理目标课程的每个课程标题,校验是否存在对应开课记录 --> <xsl:template match="Instructor_Led_Webinar_Lesson_Data"> <xsl:for-each select="LessonTitle"> <xsl:variable name="current-lesson-title" select="string(.)"/> <!-- 对比缓存的开课标题,输出未匹配错误 --> <xsl:if test="not($current-lesson-title = accumulator-before('offering-titles'))"> <UnmatchedLesson> <CourseNumber>00151744</CourseNumber> <UnmatchedLessonTitle><xsl:value-of select="$current-lesson-title"/></UnmatchedLessonTitle> <ErrorInfo>该课程标题在Offerings中无对应开课记录</ErrorInfo> </UnmatchedLesson> </xsl:if> </xsl:for-each> </xsl:template> </xsl:stylesheet>
关键修正点说明
- 精准过滤目标课程:通过
Course[CourseNumber='00151744']直接定位目标课程,避免遍历所有课程节点,从根源排除其他课程的干扰。 - 流模式兼容处理:使用XSLT 3.0的累加器(
xsl:accumulator)缓存Offerings的开课标题,满足流模式下仅能单遍读取源文档的要求。 - 聚焦目标节点处理:仅对目标课程下的
Instructor_Led_Webinar_Lesson_Data节点应用模板,确保校验范围严格限定在目标课程内。
内容的提问来源于stack exchange,提问作者Chris Tucker 2016
相关产品推荐
相关产品推荐

