< XQuery
Motivation
Adobe have introduced an XML format for image metadata called XMP. You want to display a photograph and some of the metadata.
Background
Matt Turner has an example of using MarkLogic to extract XMP data from a JPEG image .
eXist/ XQuery implementation
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
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
Some basic Dublin Core elements
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>
This article is issued from Wikibooks. The text is licensed under Creative Commons - Attribution - Sharealike. Additional terms may apply for the media files.