如何将XSLT脚本生成的文本复制到输出文件的<spine>指定区域
你需要在<spine>节点下遍历所有<exhibit>节点,按照和<manifest>里完全一致的规则生成id值,填充到itemref的idref属性即可,修改方案如下:
修改步骤
- 替换原有XSLT中
<spine>块的内容,使用<xsl:for-each>遍历所有<exhibit>节点,按相同规则生成idref - 可以删除原有
exhibit模板中未使用的count变量,简化代码
完整修改后代码
<?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://www.idpf.org/2007/opf"> <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/> <xsl:template match="/"> <xsl:element name="package" namespace="http://www.idpf.org/2007/opf"> <xsl:attribute name="unique-identifier">pub-id</xsl:attribute> <xsl:attribute name="version">3.0</xsl:attribute> <manifest> <xsl:apply-templates select="//exhibit" /> </manifest> <spine> <!-- 遍历所有exhibit生成对应的itemref --> <xsl:for-each select="//exhibit"> <itemref> <xsl:attribute name="idref"> <xsl:text>ppi</xsl:text> <xsl:number format="0000" level="any"/> </xsl:attribute> </itemref> </xsl:for-each> </spine> </xsl:element> </xsl:template> <xsl:template match="exhibit"> <xsl:element name="item"> <xsl:attribute name="id"> <xsl:text>ppi</xsl:text> <xsl:number format="0000" level="any"/> </xsl:attribute> <xsl:attribute name="href"> <xsl:value-of select="@path" /> </xsl:attribute> <xsl:attribute name="media-type"> <xsl:text>application/xhtml+xml</xsl:text> </xsl:attribute> </xsl:element> </xsl:template> </xsl:stylesheet>
原理说明
两次遍历<exhibit>节点的顺序完全一致,<xsl:number>生成的序号也完全相同,因此<manifest>里的item的id和<spine>里itemref的idref可以一一对应,刚好符合你需要的输出结果。
内容的提问来源于stack exchange,提问作者pantherfan
相关产品推荐
相关产品推荐

