编写XSLT 1.0模板:基于JDF的RunList条件生成递增索引的GatheringParams
XSLT 1.0 实现:基于带EndOfDocument属性的RunList生成正确的BundleItemIndex
问题描述
给定的JDF文件中,RunList节点包含4个子RunList条目,其中2个带有EndOfDocument="true"属性。当前基于这些条目生成ResourcePool/GatheringParams子节点时,生成的BundleItemIndex为2和4,不符合预期的1和2,需要编写正确的XSLT 1.0模板来修正这个问题。
原始JDF片段
<RunList ID="RunList_1" NPage="1" PartIDKeys="Run" Status="Available" Class="Parameter"> <RunList Run="Asset0" DescriptiveName="000202KR"> <LayoutElement> <FileSpec MimeType="application/pdf" URL="1.pdf" /> </LayoutElement> </RunList> <RunList Run="Asset1" DescriptiveName="000203KR" EndOfDocument="true"> <LayoutElement> <FileSpec MimeType="application/pdf" URL="2.pdf" /> </LayoutElement> </RunList> <RunList Run="Asset2" DescriptiveName="00020a3KR"> <LayoutElement> <FileSpec MimeType="application/pdf" URL="3.pdf" /> </LayoutElement> </RunList> <RunList Run="Asset3" DescriptiveName="0asas00203KR" EndOfDocument="true"> <LayoutElement> <FileSpec MimeType="application/pdf" URL="4.pdf" /> </LayoutElement> </RunList> </RunList>
解决方案
核心思路是仅对带有EndOfDocument="true"的RunList节点按顺序计数,而非统计所有RunList节点的位置。以下是完整的XSLT 1.0实现模板:
<?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <!-- 身份模板:复制所有未被匹配的节点和属性,保留原有JDF结构 --> <xsl:template match="@*|node()"> <xsl:copy> <xsl:apply-templates select="@*|node()"/> </xsl:copy> </xsl:template> <!-- 匹配ResourcePool节点,添加正确的GatheringParams子节点 --> <xsl:template match="ResourcePool"> <xsl:copy> <xsl:apply-templates select="@*|node()"/> <!-- 遍历所有带EndOfDocument="true"的RunList节点 --> <xsl:for-each select="//RunList[@EndOfDocument='true']"> <GatheringParams> <!-- 计算正确的BundleItemIndex:统计当前节点之前所有符合条件的节点数 +1 --> <BundleItemIndex><xsl:value-of select="count(preceding-sibling::RunList[@EndOfDocument='true']) + 1"/></BundleItemIndex> <!-- 可按需添加关联信息,比如对应的Run标识 --> <Run><xsl:value-of select="@Run"/></Run> </GatheringParams> </xsl:for-each> </xsl:copy> </xsl:template> </xsl:stylesheet>
关键说明
- 身份模板保证JDF原有结构不被破坏,仅对需要修改的部分(生成GatheringParams)做处理。
- 生成
BundleItemIndex时,使用count(preceding-sibling::RunList[@EndOfDocument='true']) + 1统计当前节点之前已出现的目标节点数量,加1后得到预期的连续索引(1、2),而非所有RunList的位置序号(2、4)。 - 若需要关联更多RunList中的信息(如文件URL、描述名称),可在
GatheringParams节点下添加对应的<xsl:value-of>逻辑。
内容的提问来源于stack exchange,提问作者Marco
相关产品推荐
相关产品推荐

