XSLT中Id与refid校验问题:多rid值匹配异常求助
问题解决思路与代码修复
核心问题
原代码仅能处理rid为单个值的场景,当rid是空格分隔的多值字符串时,无法正确拆分并逐一校验每个id,导致匹配失效。
修复后的XSL实现
1. 校验xref的bibr/tbl类型rid有效性
<!-- 校验ref-type="bibr"的xref的所有rid是否存在于ref-list的ref节点中 --> <xsl:template match="xref[@ref-type='bibr']"> <xsl:variable name="current-rids" select="tokenize(@rid, '\s+')"/> <xsl:for-each select="$current-rids"> <xsl:if test="not(//ref-list/ref[@id = .])"> <error>无效的文献引用ID: <xsl:value-of select="."/></error> </xsl:if> </xsl:for-each> <!-- 保留原节点的输出逻辑 --> <xsl:copy-of select="."/> </xsl:template> <!-- 校验ref-type="tbl"的xref的所有rid是否存在于table-list的table节点中 --> <xsl:template match="xref[@ref-type='tbl']"> <xsl:variable name="current-rids" select="tokenize(@rid, '\s+')"/> <xsl:for-each select="$current-rids"> <xsl:if test="not(//table-list/table[@id = .])"> <error>无效的表格引用ID: <xsl:value-of select="."/></error> </xsl:if> </xsl:for-each> <!-- 保留原节点的输出逻辑 --> <xsl:copy-of select="."/> </xsl:template>
2. 校验ref/table的id是否被对应类型的xref引用
<!-- 校验ref节点的id是否被任意ref-type="bibr"的xref引用 --> <xsl:template match="ref-list/ref"> <xsl:variable name="ref-id" select="@id"/> <xsl:if test="not(//xref[@ref-type='bibr'][tokenize(@rid, '\s+') = $ref-id])"> <error>未被引用的文献ID: <xsl:value-of select="$ref-id"/></error> </xsl:if> <!-- 保留原节点的输出逻辑 --> <xsl:copy-of select="."/> </xsl:template> <!-- 校验table节点的id是否被任意ref-type="tbl"的xref引用 --> <xsl:template match="table-list/table"> <xsl:variable name="table-id" select="@id"/> <xsl:if test="not(//xref[@ref-type='tbl'][tokenize(@rid, '\s+') = $table-id])"> <error>未被引用的表格ID: <xsl:value-of select="$table-id"/></error> </xsl:if> <!-- 保留原节点的输出逻辑 --> <xsl:copy-of select="."/> </xsl:template>
关键说明
- 用
tokenize(@rid, '\s+')将空格分隔的rid字符串拆分为节点序列,实现多值的逐一校验。 - 双向校验逻辑:
- 对xref:拆分每个rid,检查是否存在于对应列表的id集合中。
- 对ref/table:检查自身id是否出现在对应类型xref的rid拆分序列里。
- 兼容单值场景:当rid仅含一个值时,tokenize返回单元素序列,不影响原有逻辑运行。
内容的提问来源于stack exchange,提问作者BWin
相关产品推荐
相关产品推荐

