[epub] Reorder playorder in toc.ncx

Are you missing a feature? Request its implementation here.
rustic
Posts: 7
Joined: Sun Jun 12, 2011 10:38 pm

[epub] Reorder playorder in toc.ncx

Post by rustic »

If I decide to add a navpoint element in toc.ncx at the beginning of an existing toc.ncx in navmap, there is no way to reorder playOrder number but by hand. That could be really tedious if there are many navpoint elements.

Code: Select all


    <navPoint id="navPoint-1" playOrder="1">
<navLabel>
<text>1</text>
</navLabel>
<content src="Text/Section0002.xhtml"/>
</navPoint>
<navPoint id="navPoint-1" playOrder="1">
<navLabel>
<text>2</text>
</navLabel>
<content src="Text/Section0003.xhtml"/>
</navPoint>
george
Site Admin
Posts: 2095
Joined: Thu Jan 09, 2003 2:58 pm

Re: [epub] Reorder playorder in toc.ncx

Post by george »

You can pass the NCX file though a stylesheet that assigns playOrder sequentially, like the fixPlayorder.xsl below:

Code: Select all


<?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>
<!-- Always compute a sequential value for playOrder -->
<xsl:template match="@playOrder">
<xsl:attribute name="playOrder"><xsl:number count="*[@playOrder]" level="any"/></xsl:attribute>
</xsl:template>
</xsl:stylesheet>
Best Regards,
George
George Cristian Bina
rustic
Posts: 7
Joined: Sun Jun 12, 2011 10:38 pm

Re: [epub] Reorder playorder in toc.ncx

Post by rustic »

It's working and in a blink of an eye :wink: XSLT is very powerful indeed. Thank you for your help George.
newt
Posts: 38
Joined: Mon Jun 11, 2007 5:12 pm
Location: London
Contact:

Re: [epub] Reorder playorder in toc.ncx

Post by newt »

@George, that will fail when the NCX file contains the same path twice. We often do that when we have a book divided into parts. There is nothing in the content to indicate the path but we want some indication in the navigation:

Code: Select all

<navMap>
<navPoint id="chapter01" playOrder="1">

<navLabel><text>Ready in 30 or less</text></navLabel>
<content src="Chapter01.html"/>

<navPoint id="chapter01a" playOrder="1">
<navLabel><text>Thai red salmon curry</text></navLabel>
<content src="Chapter01.html"/>
</navPoint>

<navPoint id="chapter01b" playOrder="2">
<navLabel><text>Oven-baked Thai chicken rice</text></navLabel>
<content src="Chapter01.html#chapter01b"/>
</navPoint>

</navPoint>

<navPoint id="chapter02" playOrder="3">

<navLabel><text>Light and lovely</text></navLabel>
<content src="Chapter02.html"/>

<navPoint id="chapter02a" playOrder="3">
<navLabel><text>New potato and mince curry</text></navLabel>
<content src="Chapter02.html"/>
</navPoint>

<navPoint id="chapter02b" playOrder="4">
<navLabel><text>Easy-peasy lentil curry</text></navLabel>
<content src="Chapter02.html#chapter02b"/>
</navPoint>

</navPoint>
</navMap>
You script will 'correct' that to wrong :)
I just put in a feature request for the current archive name as an editor variable to let me do something very like this!


nic
Director, Corbas Consulting
XML and eBook consultancy and training
george
Site Admin
Posts: 2095
Joined: Thu Jan 09, 2003 2:58 pm

Re: [epub] Reorder playorder in toc.ncx

Post by george »

In that case the following XSLT 2.0 stylesheet will provide the correct values:

Code: Select all


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

<xsl:variable name="paths"
select="distinct-values(//ncx:content/normalize-space(@src))"/>

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

<xsl:template match="@playOrder">
<xsl:attribute name="playOrder">
<xsl:value-of
select="index-of($paths, parent::*/ncx:content/@src)"/>
</xsl:attribute>
</xsl:template>
</xsl:stylesheet>
Regards,
George
George Cristian Bina
virtual_ink
Posts: 2
Joined: Wed Jun 29, 2011 7:23 am

Re: [epub] Reorder playorder in toc.ncx

Post by virtual_ink »

I'm a newbie to epub creation and found this post in google while looking for a solution to the same problem. Some of our books have seventy chapters, and I don't want to rename them all by hand once I edit one element of the toc.

While I don't use oxygen as yet (I have heard of if it in some training videos I'm watching), I'm wondering if I could still use the above method as a fix? Or any other? I'm outputting my epubs from InDesign, then editing with a combination of TextWrangler and Sigil (and lots of trial and error!).

If you have the patience, could you please provide a complete novices' guide in step by step form for using this fix? I realise that's probably a big ask, so no pressure!
george
Site Admin
Posts: 2095
Joined: Thu Jan 09, 2003 2:58 pm

Re: [epub] Reorder playorder in toc.ncx

Post by george »

I am afraid you want something else. This stylesheet fixes the values for the playOrder attribute, it does not fix broken names. oXygen having EpubChek validation integrated will show you the broken links, if any, and you will be able to use oXygen to edit the files inside an EPUB to correct these problems.

Best Regards,
George
George Cristian Bina
virtual_ink
Posts: 2
Joined: Wed Jun 29, 2011 7:23 am

Re: [epub] Reorder playorder in toc.ncx

Post by virtual_ink »

Thanks for the response George. I might have made my post unclear. To clarify, I want something that fixes the playOrder attribute.

Eg, if I change playOrder="2" to playOrder="4" (manually), I want something that will rename all the other playOrder #s in relation to this.
george
Site Admin
Posts: 2095
Joined: Thu Jan 09, 2003 2:58 pm

Re: [epub] Reorder playorder in toc.ncx

Post by george »

The solution presented here takes a TOC file and it adds playOrder values in their natural/document order. It does not take into account any of the existing values. All you need is to apply the XSLT transformation on a NCX file with broken playOrder values and you will get as result the NCX file with the playOrder values fixed.

Best Regards,
George
George Cristian Bina
sofarocker
Posts: 5
Joined: Mon Jun 01, 2015 3:28 pm

Re: [epub] Reorder playorder in toc.ncx

Post by sofarocker »

Hi, i`am newbi and try to automate my ncx-file with the XSLT-2.0 stylesheet but i want work.

How can i use this XSLT 2.0 stylesheet in my epub-folder with oxygen. when i try, the stylesheet makes me just two entries. i was appreciated when somebody gives me a step by step introduction.
Costin
Posts: 833
Joined: Mon Dec 05, 2011 6:04 pm

Re: [epub] Reorder playorder in toc.ncx

Post by Costin »

Hi sofarocker,

First, you should just open the .ncx file from your Epub, then configure a transformation scenario which you could further apply to transform the document.

To set up the scenario you can either:
- open the ncx file and look in the "Transformation Scenarios" view (Window > Show View > Transformation Scenarios)
- open the ncx file and from the main menu invoke Document -> Transformation -> "Configure Transformation Scenario(s)" (there's a corresponding button in the toolbar)

In the "Configure Transformation Scenario(s)" dialog press "New" and select "XML transformation with XSLT" to create a new scenario:
1. Give it an appropriate name
2. Leave the "XML URL" field to its default(${currentFileURL})
3. In the XSL URL field browse for your stylesheet.
4. As you specified you have an XSLT 2.0 stylesheet choose from the transformer combo Saxon-PE or Saxon-EE.
5. You can further tune the "Output". Please note that the "Save as" field must refer a single file, NOT an output directory. Use the editor variables to compose a generic name instead of a fixed one.
e.g in the "Save as" field you can specify: ${cfd}/${cfn}-out.xml which translates into <current-file-directory>/<current-filename>-out.xml
6. Press OK in the editing dialog and "Save and close".

Going further, you can apply the transformation scenario you have just created by going into menu Document > Transformation > Apply Transformation Scenario(s), or by using the related shortcut button in the toolbar.

I hope this helps.

Regards,
Costin
Costin Sandoi
oXygen XML Editor and Author Support
Seasider47
Posts: 1
Joined: Sat Oct 15, 2022 4:51 pm

Re: [epub] Reorder playorder in toc.ncx

Post by Seasider47 »

This is clearly a wonderful script but if you'll forgive the newbie predicament, I have no understanding of how, under WIndows 10, to run it. I have put
logical.ncx
renumber.xml [the script]
In the same folder on my Dekstop. Can anyone tell me what I should do next? With thanks in anticipation.
Radu
Posts: 9057
Joined: Fri Jul 09, 2004 5:18 pm

Re: [epub] Reorder playorder in toc.ncx

Post by Radu »

Hi,
This forum thread was started about 11 years ago, in such cases maybe it's best to start a new thread.
Also this entire forum is for the Oxygen XML Editor application so the steps to run the XSLT stylesheet above should be taken inside the Oxygen application.
Regards,
Radu
Radu Coravu
<oXygen/> XML Editor
http://www.oxygenxml.com
Post Reply