viktorkho
viktorkho

Reputation: 682

XSL to extract epub-metadata

I want to extract existing metadata from opf-files of epub-books with xsl.

Example of opf-file:

<?xml version="1.0" encoding="UTF-8" ?>
<package xmlns="http://www.idpf.org/2007/opf" xmlns:dc="http://purl.org/dc/elements/1.1/" unique-identifier="db-id" version="3.0">

<metadata>
    <dc:title id="t1">TITLE</dc:title>
    <dc:identifier id="db-id">ISBNUUID</dc:identifier>
    <dc:creator id="creator">CREATOR</dc:creator>
    <dc:language>LANGUAGE</dc:language>
    <meta property="dcterms:modified">DATETIME</meta>
    <dc:language>en</dc:language>
</metadata>

<manifest>
    <item id="toc" properties="nav" href="toc.xhtml" media-type="application/xhtml+xml" />
    <item id="ncx" href="toc.ncx" media-type="application/x-dtbncx+xml" />
    <item id="template_css" href="template.css" media-type="text/css" />
    <item id="hello" href="1_hello.xhtml" media-type="application/xhtml+xml" />
</manifest>

<spine toc="ncx">
    <itemref idref="hello" />
</spine>

</package>

I need to extract pairs <nodename>: <nodevalue> like this:

title: TITLE
identifier: ISBNUUID
...

But it seems like even can't to address this nodes properly. And I'm not shure about namespaces..

My xsl now looks like this:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="text"/>
  <xsl:template match="metadata">
    <xsl:value-of select="name(.)"/>
    <xsl:value-of select="."/>
  </xsl:template>
</xsl:stylesheet>

Xmlstarlet produces only node-values and alot of empty strings:

$ xmlstarlet tr select-opds-metadata.xsl ../s3opdsd/epubgen/var/epub-boilerplate/OEBPS/content.opf


    TITLE
    ISBNUUID
    CREATOR
    LANGUAGE
    DATETIME
    en

... truncated ...

I hope anyone can help with this..

Upvotes: 0

Views: 193

Answers (1)

michael.hor257k
michael.hor257k

Reputation: 117102

You have several mistakes:

  1. You are not taking into account the source XML's namespaces.
  2. You are processing the metadata element, instead of its children.
  3. You want punctuation, but you're not specifying any.

Try:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:o="http://www.idpf.org/2007/opf" >
<xsl:output method="text"/>

<xsl:template match="/o:package">
    <xsl:for-each select="o:metadata/*">
        <xsl:value-of select="local-name(.)"/>
        <xsl:text>: </xsl:text>
        <xsl:value-of select="."/>
        <xsl:text>&#10;</xsl:text>
    </xsl:for-each>
</xsl:template>

</xsl:stylesheet>

Upvotes: 1

Related Questions