如何使用XSLT为pageTarget添加playOrder序列?
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
ncxprefix to correctly target elements in your input XML. Theexclude-result-prefixesattribute ensures this prefix doesn't clutter the output. - Identity Template: Preserves all original content (elements, attributes, text) so only the
playOrderattributes are added—no unintended changes to your NCX structure. - Continuous Sequence: The
count(preceding::ncx:navPoint | preceding::ncx:pageTarget) + 1expression calculates each element's position in the combined document order of allnavPointandpageTargetnodes. This guarantees a seamless, incrementing sequence starting at 1. - Nested
navPointSupport: ParentnavPointelements 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
playOrderattributes added to everynavPointandpageTargetin the correct continuous sequence.
内容的提问来源于stack exchange,提问作者user9702614
相关产品推荐
相关产品推荐

