XSLT基于group属性拆分XML文件时过滤无关课程数据问题
修正后可实现需求的XSLT代码
<?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/> <xsl:template match="contacts"> <!-- 提取所有不重复的分组编号,逐个生成对应文件 --> <xsl:for-each select="distinct-values(contact/cursus/@group)"> <xsl:variable name="target-group" select="."/> <xsl:result-document href="groep-{$target-group}.xml"> <contacts> <!-- 筛选所有归属当前分组的参与者 --> <xsl:apply-templates select="/contacts/contact[cursus/@group = $target-group]"> <!-- 通过隧道参数传递目标分组编号,无需逐层手动传参 --> <xsl:with-param name="target-group" select="$target-group" tunnel="yes"/> </xsl:apply-templates> </contacts> </xsl:result-document> </xsl:for-each> </xsl:template> <!-- 基础身份转换模板,保留无需修改的节点内容 --> <xsl:template match="@*|node()"> <xsl:param name="target-group" tunnel="yes"/> <xsl:copy> <xsl:apply-templates select="@*|node()"> <xsl:with-param name="target-group" select="$target-group" tunnel="yes"/> </xsl:apply-templates> </xsl:copy> </xsl:template> <!-- 过滤cursus节点,仅保留当前分组的课程信息 --> <xsl:template match="cursus"> <xsl:param name="target-group" tunnel="yes"/> <xsl:if test="@group = $target-group"> <xsl:copy> <xsl:apply-templates select="@*|node()"/> </xsl:copy> </xsl:if> </xsl:template> </xsl:stylesheet>
逻辑说明
- 替换原有按contact分组的逻辑,改为先提取所有唯一分组编号,确保归属多个分组的参与者会出现在所有对应分组的文件中
- 新增
节点的专属匹配模板,结合传递的目标分组编号,自动过滤不属于当前分组的课程信息,仅保留符合要求的课程节点
内容的提问来源于stack exchange,提问作者Robert E
相关产品推荐
相关产品推荐

