如何使用XSLT在toc.ncx中添加playOrder?

时间:2018-04-26 09:13:06

标签: xslt

如果我决定在navmap中现有toc.ncx的开头在toc.ncx中添加一个navpoint元素,则无法手动重新排序playOrder编号。如果有许多导航元素,那可能真的很乏味。

输入

 <?xml version="1.0" encoding="UTF-8"?>
<ncx version="2005-1" xmlns="http://www.daisy.org/z3986/2005/ncx/">
  <head>
    <meta name="dtb:uid" content="9781315348674" />
    <meta name="dtb:depth" content="1" />
    <meta name="dtb:totalPageCount" content="144" />
    <meta name="dtb:maxPageNumber" content="144" />
  </head>
  <docTitle>
    <text>Making Choices for Health Care</text>
  </docTitle>
  <navMap>
<navPoint id="nav-1">
<navLabel>
<text>Cover</text>
</navLabel>
<content src="xhtml/A01_cover.xhtml"/>
</navPoint>
<navPoint id="nav-2">
<navLabel>
<text>Half Title</text>
</navLabel>
<content src="xhtml/A02_halftitle.xhtml"/>
</navPoint>
</navMap>
</ncx>

假设输出像:

<navPoint id="nav-1" playOrder="1">
      <navLabel>
        <text>1</text>
      </navLabel>
      <content src="Text/Section0002.xhtml"/>
    </navPoint>
    <navPoint id="nav-2" playOrder="2">
      <navLabel>
        <text>2</text>
      </navLabel>
      <content src="Text/Section0003.xhtml"/>
    </navPoint>

XSLT代码:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="1.0">
    <!-- Recursive copy template -->    
    <xsl:template match="node() | @*">
        <xsl:copy>
            <xsl:apply-templates select="node() | @*"/>
        </xsl:copy>
    </xsl:template>
<xsl:template match="navPoint">
  <xsl:copy>
    <xsl:attribute name="playOrder">1</xsl:attribute>
    <xsl:apply-templates />
  </xsl:copy>
</xsl:template>
    <xsl:template match="@playOrder">
        <xsl:attribute name="playOrder"><xsl:number count="*[@playOrder]" level="any"/></xsl:attribute>
    </xsl:template>
</xsl:stylesheet>

此代码无效,请您告诉我正确的代码

2 个答案:

答案 0 :(得分:0)

使用此:

<xsl:attribute name="playOrder"><xsl:number count="navPoint" level="any"/>/xsl:attribute>

而不是

<xsl:attribute name="playOrder">1</xsl:attribute>

并删除模板

   <xsl:template match="@playOrder">
    <xsl:attribute name="playOrder"><xsl:number count="*[@playOrder]" level="any"/></xsl:attribute>
</xsl:template>

请参阅https://xsltfiddle.liberty-development.net/94hvTyV/2

处的转化

答案 1 :(得分:0)

假设你只想要正常的1,2,3编号,你可以count()在节点之前。

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <!-- Recursive copy template -->    
    <xsl:template match="node() | @*">
        <xsl:copy>
            <xsl:apply-templates select="node() | @*"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="navPoint">
      <xsl:copy>
        <xsl:attribute name="playOrder">
          <xsl:value-of select="count(preceding-sibling::navPoint) + 1" />
        </xsl:attribute>
        <xsl:apply-templates />
      </xsl:copy>
    </xsl:template>

</xsl:stylesheet>