You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用XSLT为pageTarget添加playOrder序列?

XSLT Solution to Add playOrder Attributes to pageTarget and navPoint

To generate a continuous, incrementing playOrder sequence for both navPoint and pageTarget elements in your DAISY 2005 NCX file, here's a practical XSLT implementation that aligns with your requirements:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:ncx="http://www.daisy.org/z3986/2005/ncx/"
                exclude-result-prefixes="ncx">

  <!-- Identity template: copy all existing content by default -->
  <xsl:template match="@* | node()">
    <xsl:copy>
      <xsl:apply-templates select="@* | node()"/>
    </xsl:copy>
  </xsl:template>

  <!-- Add playOrder to navPoint elements -->
  <xsl:template match="ncx:navPoint">
    <xsl:copy>
      <xsl:attribute name="playOrder">
        <!-- Calculate position in combined sequence of navPoint + pageTarget -->
        <xsl:value-of select="count(preceding::ncx:navPoint | preceding::ncx:pageTarget) + 1"/>
      </xsl:attribute>
      <xsl:apply-templates select="@* | node()"/>
    </xsl:copy>
  </xsl:template>

  <!-- Add playOrder to pageTarget elements -->
  <xsl:template match="ncx:pageTarget">
    <xsl:copy>
      <xsl:attribute name="playOrder">
        <xsl:value-of select="count(preceding::ncx:navPoint | preceding::ncx:pageTarget) + 1"/>
      </xsl:attribute>
      <xsl:apply-templates select="@* | node()"/>
    </xsl:copy>
  </xsl:template>

</xsl:stylesheet>

Key Details

  • Namespace Handling: The NCX namespace is declared with the ncx prefix to correctly target elements in your input XML. The exclude-result-prefixes attribute ensures this prefix doesn't clutter the output.
  • Identity Template: Preserves all original content (elements, attributes, text) so only the playOrder attributes are added—no unintended changes to your NCX structure.
  • Continuous Sequence: The count(preceding::ncx:navPoint | preceding::ncx:pageTarget) + 1 expression calculates each element's position in the combined document order of all navPoint and pageTarget nodes. This guarantees a seamless, incrementing sequence starting at 1.
  • Nested navPoint Support: Parent navPoint elements are counted before their children, which aligns with typical book playback order (where section headings precede their content).

How to Use

  • Save the XSLT as add-playorder.xsl.
  • Apply it to your NCX file using an XSLT processor like Saxon, Xalan, or a trusted online tool.
  • The output will be your original NCX with playOrder attributes added to every navPoint and pageTarget in the correct continuous sequence.

内容的提问来源于stack exchange,提问作者user9702614

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 09:46:35