XML manipulation of the Abstract page using a Bazaar Module

From EPrints Documentation
Revision as of 15:05, 1 December 2010 by DaveTarrant (talk | contribs)
Jump to: navigation, search

Before you can carry out the work outlined on this page you will need to prepare your package by following the steps on the XML manipulation of the EPrints Workflow using a Bazaar Package page up until the start of Exercise 1.

In this exercise we are going to manipulate the abstract page by changing the citation XML which is displayed on various parts of the rendered abstract page.


To do this exercise you will need:

  • A live EPrint record of type article. Training repositories should come set up with the test data.
  • A rights field added to the EPrints workflow and filled in for this eprint.

To add the rights field is simply a replication of the previous exercise (XML manipulation of the EPrints Workflow using a Bazaar Package), with the following code block to manipulate the workflow needing to be added to postinst:

 my $filename = $repository->config( "config_path" )."/workflows/eprint/default.xml";
 my $string = '
 <workflow xmlns="http://eprints.org/ep3/workflow" xmlns:epc="http://eprints.org/ep3/control">
               <stage ref="divisions"/>
       <stage name="divisions">
               <component type="Field::Subject">
                       <field ref="divisions" required="yes" />
 my $rc = 0;
 foreach my $child ( $node->getChildNodes() ) {
        $rc = EPrints::XML::add_to_xml( $filename,$child,"package_name" );

Don't forget the equivalent code block in prerm.

Exercise 1

We are going to directly manipulate the EPrints summary page to add the copyright holder data directly below the main citation. This could be done with the citation file in exactly the same way, we have just chosen to do the XML manipulation on the summary_page.xml instead.

Firstly we fetch the citation (code needed in both the postinst and prerm' methods.

 my $citation = $repository->dataset( "eprint" )->citation( "summary_page" );
 my $filename = $citation->{filename};

Then we specify our xml to add in postinst

 $string = '
 <cite:citation xmlns='http://www.w3.org/1999/xhtml' xmlns:epc='http://eprints.org/ep3/control' xmlns:cite='http://eprints.org/ep3/citation'>
   <p style='margin-bottom: 1em'>
      <div align='center'>
          <epc:foreach expr='copyright_holders' iterator='item'>
            <epc:print expr='\$item'/><br/>

Parse it and apply it.

 $xml = EPrints::XML::parse_string( undef, $string );
 $xml = EPrints::XML::_remove_blank_nodes($xml);
 $node = $xml->getFirstChild();
 foreach my $child ( $node->getChildNodes() ) {
     $rc = EPrints::XML::add_to_xml( $filename,$child,"citation_test" );
 $rc = EPrints::XML::add_to_xml($filename,$node,"citation_test");

Expiring your Abstract Pages

Due the fact we are adding to the abstract pages of an eprint record we need to expire them so that next time they are requested they are re-drawn. This code needs to be added to both the postinst and prerm methods.

 unless( $repository->expire_abstracts() )
       $message = "You need to regenerate abstracts";
       $rc = 0.5;

This is an example of returning a code which is not true of false. In this case the code means "I was successful but the user needs to do something extra".


This time to test you can just add some copyright holder metadata to an article and then refresh that articles abstract to see if it worked.

Exercise 2 - Overriding whole sections of the abstract page

This feature allows you to replace entire sections of the abstract page with your own code.

This is a simple enough operation to do, instead of simply matching XML elements and adding ones that aren't there like we have been doing up to this point, we now specify a flag on an element to begin the replacement.

See if you can work out what this piece of XML will do, why not add it to your package and try it.

 <cite:citation xmlns='http://www.w3.org/1999/xhtml' xmlns:epc='http://eprints.org/ep3/control' xmlns:cite='http://eprints.org/ep3/citation'>
   <epc:set mode='replace' name='docs' expr='\$item.documents()'>Hello</epc:set>

Exercise 3

Our copyright section doesn't really say what it is all about, how about adding a phrase, clue:

 <epc:phrase ref="myphrase_ref"/>

Exercise 4

Select any section on the abstract page to replace and see if you can replace it with something much prettier. Could you recognise the epub format and display this in a specific way which is different to other articles.