fb2 本から目次を抽出するには?

fb2 本から目次を抽出するには?

fb2形式の本があります。 「パート」、「チャプター」、「エピソード」などの名前と番号を含む目次を印刷したいです。

端末でこれを行う方法はありますか?一つある同様の質問ですが、epub形式の場合

fb2がxml形式であることを知っています。しかし、TOCだけを抽出するためのツールがありますか?ラベル<section>とに<title>あります<subtitle>

そうでない場合は、公式に従ってxslファイルを作成できると思います。FB2_to_txt.xsl文書。または多分電子書籍の切り替えこれは可能ですか?

私が書いている本の構造は次のとおりです。

<?xml version="1.0" encoding="utf8"?>
<FictionBook xmlns:l="http://www.w3.org/1999/xlink" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns="http://www.gribuser.ru/xml/fictionbook/2.0">
  <description>
    <title-info>
      <genre>fiction</genre>
      <author>
        <first-name>John</first-name>
        <last-name>Doe</last-name>
      </author>
      <book-title>Fiction Book</book-title>
      <annotation>
        <p>Hello</p>
      </annotation>
      <keywords>john, doe, fiction</keywords>
      <date value="2011-07-18">18.07.2011</date>
      <coverpage></coverpage>
      <lang>en</lang>
    </title-info>
    <document-info>
      <author>
        <first-name></first-name>
        <last-name></last-name>
        <nickname></nickname>
      </author>
      <program-used>Fb2 Gem</program-used>
      <date value="2011-07-18">18.07.2011</date>
      <src-url></src-url>
      <src-ocr></src-ocr>
      <id></id>
      <version>1.0</version>
    </document-info>
    <publish-info>
    </publish-info>
  </description>
  <body>
    <title>
      <p>John Doe</p>
      <empty-line/>
      <p>Fiction Book</p>
    </title>
    <section>
      <title>
        <p>Part 1</p>
        <p>Some name of Part 1</p>
      </title>
      <section>
        <title>
          <p>Chapter 1</p>
          <p>Some name of Chapter 1</p>
        </title>
        <subtitle>Episode 1</subtitle>
        <p>Line one of the first episode</p>
        <p>Line two of the first episode</p>
        <p>Line three of the first episode</p>
        <subtitle>Episode 2</subtitle>
        <p>Line one of the second episode</p>
        <p>Line two of the second episode</p>
        <p>Line three of the second episode</p>
      </section>
    </section>
    <section>
      <title>
        <p>Part 2</p>
        <p>Some name of Part 2</p>
      </title>
      <section>
        <title>
          <p>Chapter 3</p>
          <p>Some name of Chapter 3</p>
        </title>
        <subtitle>Episode 3</subtitle>
        <p>Line one of the third episode</p>
        <p>Line two of the third episode</p>
        <p>Line three of the third episode</p>
        <subtitle>Episode 4</subtitle>
        <p>Line one of the fourth episode</p>
        <p>Line two of the fourth episode</p>
        <p>Line three of the fourth episode</p>
      </section>
    </section>
  </body>
</FictionBook>

出力から以下を取得したいと思います。

Part 1
Some name of Part 1
Chapter 1
Some name of Chapter 1
Episode 1
Episode 2
Part 2
Some name of Part 2
Chapter 3
Some name of Chapter 3
Episode 3
Episode 4

ベストアンサー1

使用xmlstarlet:

xmlstarlet select --template \
    --value-of '//_:section/_:title/_:p | //_:subtitle' \
    -nl file.xml

または短いオプションを使用してください。

xmlstarlet sel -t \
    -v '//_:section/_:title/_:p | //_:subtitle' \
    -n file.xml

ここで使用されるXPathクエリは、以下の各ノードの値pだけでなく、すべてのノードの値も抽出します。titlesectionsubtitle

式では、各ノード名の前の接頭辞は、文書_:で使用される名前空間識別子の匿名プレースホルダーです。

サンプル文書によると、上記の2つのコマンドのうちの1つの出力は次のようになります。

Part 1
Some name of Part 1
Chapter 1
Some name of Chapter 1
Episode 1
Episode 2
Part 2
Some name of Part 2
Chapter 3
Some name of Chapter 3
Episode 3
Episode 4

本のタイトルも欲しいですか?次に、_:section式から制限を削除します(これを行うと、p書籍のタイトルのノードも一致します)。

よりきれいに見える(本のタイトルを除く)各セクションのタイトルとサブタイトルを取得する別の方法(サブタイトルがどこではなくセクションで選択されているかを示すため)は、最初に一致をパーツに制限することです。その後、その部分からデータを取得します。

xmlstarlet select --template \
    --match '//_:section' \
    --value-of '_:title/_:p | _:subtitle' \
    -nl file.xml

おすすめ記事