[OPEN-ILS-DEV] A command to order the MARC XML

Bryan Baldus bryan.baldus at quality-books.com
Thu Dec 1 16:24:23 EST 2011


On Thursday, December 01, 2011 2:54 PM, Liam Whalen wrote:
>What I'm interested in doing is adding some 035 tags to some of our MARC records via SQL, but determining where to place the 035 in the MARC XML with SQL is not easy.  So, I was hoping I could just append a <datafield> tag at the end of the last <datafield> tag and then have a PostgreSQL function re-order the MARC XML for me.  Is that possible?

I'm unfamiliar with SQL programming, so I can't answer that portion of the question. As a cataloger, I would urge caution in reordering the tags as, at least theoretically, the order of some fields is purposefully out of tag order (5xx and 6xx fields--in 6xx, the subject headings are intended to be ordered in order of importance; in 5xx, a variety of practices exist, but catalogers try to put them in order of importance or in the order prescribed by cataloging rules). In many systems this is lost when the program reorders fields in tag order.

That said, in Perl, using MARC::Record (leaving aside the issues involved in converting the MARC XML to/from MARC::Record, which should be simple enough, but I don't work with XML enough to know it well), I use the code below for sorting 0xx fields.

I hope this helps,

Bryan Baldus
Cataloger
Quality Books Inc.
The Best of America's Independent Presses
1-800-323-4241x402
bryan.baldus at quality-books.com
eijabb at cpan.org
http://home.comcast.net/~eijabb/

####Begin sub


sub sort_0xx_fields {

        my $marc = shift;

        #create new MARC::Record object for sorted version of record
        my $sorted_marc = MARC::Record->new();
        #add leader from unsorted record
        $sorted_marc->leader($marc->leader());

        #declare hash to store 0xx fields for sorting by tag
        my %zeroxx_fields = ();
        #declare array to store the rest of the fields in their current order
        my @other_fields = ();

        foreach my $field ($marc->fields()) {
                my $tag = $field->tag();
                if ($tag =~ /^0[0-9][0-9]$/) {
                        push @{$zeroxx_fields{$tag}}, $field->clone();
                } #if 0xx field
                else {
                        push @other_fields, $field;
                } #else not 0xx field
        } #foreach field in record

        #go through stored 0xx, sort by tag, and add to sorted record
        foreach my $key (sort keys %zeroxx_fields) {
                $sorted_marc->append_fields(@{$zeroxx_fields{$key}});

        } #foreach 0xx field

        #add the rest of the fields in their original order
        $sorted_marc->append_fields(@other_fields);

        return $sorted_marc;

} #sort_0xx_fields

####End sub

#call:

my $sorted_record = MARC::QBI::Misc::sort_0xx_fields($record);


More information about the Open-ils-dev mailing list