10.5.  Nesting Depth of Bookmarks

Initial Situation

A company has a style-guide for PDF documents which allows a limited nesting depth for bookmarks.

Problem

How can you verify this requirement?

Solution Approach

The bookmarks are analyzed using XPath.

Solution

Extract all bookmarks in a PDF using the extraction program ExtractBookmarks. This is the file:

<?xml version="1.0" encoding="UTF-8"?>
<Bookmark>
  <Title Action="GoTo" Page="1 FitH 698" Style="bold" >Bookmark to first page
    <Title Action="URI" URI="http://www.wikipedia.org/" Color="0 0 1" >
      Link to Wikipedia
    </Title>
  </Title>
</Bookmark>

Now run a test containing an XPath expression which checks that there is no Title-node having two or more Title-nodes as predecessors.

<!--
  Testing bookmarks, one nested level allowed.
-->
<testcase name="hasBookmarks_LimitedNestedDepthTo1">
  <assertThat testDocument="bookmarks/bookmarksWithPdfOutline.pdf">
    <hasBookmarks>
      <matchingXPath expr="count(//Title[count(ancestor::Title) > 1]) = 0" />
    </hasBookmarks>
  </assertThat>
</testcase>

<!--
  Important hint:
  The value of the attribute 'expr' is used as a parameter of type String.
  So you have to use double quotes as outer quotes for the attribute. 
  Otherwise you get a compile error after the transformation from XML to Java.  
-->