XQuery/XMP data

From Wikibooks, open books for an open world
Jump to navigation Jump to search

Motivation[edit | edit source]

Adobe have introduced an XML format for image metadata called XMP. You want to display a photograph and some of the metadata.

Background[edit | edit source]

Matt Turner has an example of using MarkLogic to extract XMP data from a JPEG image [1].

eXist/ XQuery implementation[edit | edit source]

Here is the code using eXist XQuery extensions. Matt's " photograph of a boat has been stored in the XML Database as a binary file.

Show the full XMP XML[edit | edit source]

The binary image is retrieved from the database as a BASE64 encoded string, converted to a UTF-8 string, the metadata text extracted and turned back to XML using util:parse()


declare function local:extract-xmp ($jpeg as xs:string)  as item() {
let $binary := util:binary-doc($jpeg)
let $text := util:binary-to-string($binary)
let $xmp := substring-after($text,"<x:xmpmeta")
let $xmp := substring-before($xmp,"</x:xmpmeta>")     
let $xmp := concat("<x:xmpmeta",$xmp,"</x:xmpmeta>")
return util:parse($xmp) 
};

let $photo := request:get-parameter("photo",())
let $xmp := local:extract-xmp(concat("/db/Wiki/eXist/",$photo))
return
    $xmp

XMP XML

Some basic Dublin Core elements[edit | edit source]

Here we extract a few of the Dublin Core elements and create an HTML fragment containing the image and the meta-data.

declare namespace dc="http://purl.org/dc/elements/1.1/";

declare function local:extract-xmp ($jpeg as xs:string)  as item() {
 .....
};

declare option exist:serialize "method=xhtml media-type=text/html";

let $photo := request:get-parameter("photo",())
let $xmp := local:extract-xmp(concat("/db/Wiki/eXist/",$photo))
return
 <div>
    <img src="../{$photo}"/>
    <ul>
         <li> Format : {string($xmp//dc:format)}</li>
         <li>Title: {string($xmp//dc:title)}</li>
         <li>Creator: {string($xmp//dc:creator)}</li>
    </ul>
</div>

Basic Dublin Core elements