Difference between revisions of "XML manipulation of the Abstract page using a Bazaar Module"

From EPrints Documentation
Jump to: navigation, search
(Created page with '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 Baz…')
 
Line 15: Line 15:
 
    
 
    
 
   my $string = '
 
   my $string = '
   <xml>
+
   <workflow xmlns="http://eprints.org/ep3/workflow" xmlns:epc="http://eprints.org/ep3/control">
 
         <flow>
 
         <flow>
 
                 <stage ref="divisions"/>
 
                 <stage ref="divisions"/>
Line 24: Line 24:
 
                 </component>
 
                 </component>
 
         </stage>
 
         </stage>
   </xml>
+
   </workflow>
 
   ';
 
   ';
 
    
 
    
Line 42: Line 42:
  
 
   my $citation = $repository->dataset( "eprint" )->citation( "summary_page" );
 
   my $citation = $repository->dataset( "eprint" )->citation( "summary_page" );
 
+
 
 
   my $filename = $citation->{filename};
 
   my $filename = $citation->{filename};
  
 
Then we specify our xml to add in '''postinst'''  
 
Then we specify our xml to add in '''postinst'''  
  
   $string = "
+
   $string = '
   <cite:citation xmlns='http://www.w3.org/1999/xhtml' xmlns:epc='http://eprints.org/ep3/control' xmlns:cite='http://eprints.org/ep3/citation'>
+
   <nowiki><cite:citation xmlns='http://www.w3.org/1999/xhtml' xmlns:epc='http://eprints.org/ep3/control' xmlns:cite='http://eprints.org/ep3/citation'></nowiki>
     <p style='margin-bottom: 1em'>
+
     <nowiki><p style='margin-bottom: 1em'></nowiki>
       <div align='center'>
+
       <nowiki><div align='center'></nowiki>
         <i>
+
         <nowiki><i></nowiki>
           <epc:foreach expr='copyright_holders' iterator='item'>
+
           <nowiki><epc:foreach expr='copyright_holders' iterator='item'></nowiki>
             <epc:print expr='\$item'/><br/>
+
             <nowiki><epc:print expr='\$item'/><br/></nowiki>
           </epc:foreach>
+
           <nowiki></epc:foreach></nowiki>
         </i>
+
         <nowiki></i></nowiki>
       </div>
+
       <nowiki></div></nowiki>
     </p>
+
     <nowiki></p></nowiki>
   </cite:citation>
+
   <nowiki></cite:citation></nowiki>
 +
  ';
  
 
Parse it and apply it.
 
Parse it and apply it.
Line 97: Line 98:
 
See if you can work out what this piece of XML will do, why not add it to your package and try it.
 
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'>
+
   <nowiki><cite:citation xmlns='http://www.w3.org/1999/xhtml' xmlns:epc='http://eprints.org/ep3/control' xmlns:cite='http://eprints.org/ep3/citation'></nowiki>
    <epc:set mode='replace' name='docs' expr='\$item.documents()'>Hello</epc:set>
+
    <epc:set mode='replace' name='docs' expr='\$item.documents()'>Hello</epc:set>
   </cite:citation>
+
   <nowiki></cite:citation></nowiki>
  
 
=Exercise 3=
 
=Exercise 3=

Revision as of 15:05, 1 December 2010

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.

Pre-Requisits

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">
       <flow>
               <stage ref="divisions"/>
       </flow>
       <stage name="divisions">
               <component type="Field::Subject">
                       <field ref="divisions" required="yes" />
               </component>
       </stage>
 </workflow>
 ';
 
 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'>
        <i>
          <epc:foreach expr='copyright_holders' iterator='item'>
            <epc:print expr='\$item'/><br/>
          </epc:foreach>
        </i>
      </div>
   </p>
 </cite:citation>
 ';

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;
       return($rc,$message);
 }

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".

Testing

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>
 </cite:citation>

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.