void CScribeToNeoScribeXML::SegmentScribe2MEIXML(const CScribeReaderVisitable& scribe_data)
{
    std::unordered_set<std::string> rep_no_record;
    
    for (std::vector<scribe_part>::const_iterator part = scribe_data.GetScribeParts().begin(); part != scribe_data.GetScribeParts().end(); part++)
    {
        int i = 0;
        
        //save record of first part
        scribe_part first_part = *part;
        
        //create an instance of XML doc representation, etc.
        delete doc;
        doc = new MeiDocument();
        
        Mei *mei = new Mei;
        doc->setRootElement(mei);
        /*
            MEI
                - <meiHead>
                    - FileDescription
                    - EncodingDescription
                    - Work Description
                    - Revision Description
         
         */
        //create MEIhead to contain file, encoding, work and revision description
        MeiHead* mei_head = new MeiHead;
        mei->addChild(mei_head);
        
        AltId* altId = new AltId;
        mei_head->addChild(altId);
        if (scribe_data.GetType()==scribe_type::trecento)
        {
            altId->addAttribute("type", "repnum");
            altId->setValue(part->rep_num);
        } else if (scribe_data.GetType()==scribe_type::trecento)
        {
            altId->addAttribute("type", "cao");
            altId->setValue(std::to_string(part->cao_num));

        }
        //Create and link fileDesc
        FileDesc* fileDesc = Scribe2MEIFileDesc();
        mei_head->addChild(fileDesc);
        
        //Create and link encodingDesc
        EncodingDesc* encodingDesc = Scribe2MEIEncoderDesc();
        mei_head->addChild(encodingDesc);
        
        //Create and link workDesc
        WorkDesc* workDesc = Scribe2MEIWorkDesc();
        mei_head->addChild(workDesc);
        //</meiHead> - not really at this stage - other elements completed in main routine
        
        //music - contains all music data
        Music* music = new Music;
        mei->addChild(music);
        Mdiv* mdiv = new Mdiv; //for chant source this needs to be specified repeatedly, with n and type attributes
        music->addChild(mdiv);
        Score* score = new Score;
        mdiv->addChild(score);
        
        //start adding score definitions - child of score
        ScoreDef* scoredef = new ScoreDef;
        score->addChild(scoredef);
        StaffGrp* staffgrp = new StaffGrp;
        scoredef->addChild(staffgrp);
        staffgrp->setId("all");
        
        //skip along and collect parts - all have the same REPNUM
        
        std::string xml_file_name("");
        
        if (scribe_data.GetType()==scribe_type::trecento)
        {
            do
            {
                ++i;
                
                if (i==1)
                {
                    Scribe2MEIXMLFileData(fileDesc, *part);
                    Scribe2MEIXMLWorkData(workDesc, *part);
                }
                //add section - child of score
                Section* section = new Section;
                score->addChild(section);
                
                //handle staff and link to section
                Staff* staff = Scribe2MEIXMLStaff(scribe_data, *part, staffgrp, i);
                section->addChild(staff); //or TiXmlElement* layer;?
                
                part++;
                
            } while ( part->rep_num==first_part.rep_num && part != scribe_data.GetScribeParts().end());
            
            part--; // step back to last part in piece
            
            xml_file_name = first_part.rep_num;
            
            //find duplicates - doesn't find triplicates
            if (rep_no_record.find(part->rep_num) == rep_no_record.end())
            {
                rep_no_record.insert(part->rep_num);
            }
            else
            {
                xml_file_name += " copy";
            }
            
        }
        else if (scribe_data.GetType()==scribe_type::chant)
        {
            
            Scribe2MEIXMLFileData(fileDesc, *part);
            Scribe2MEIXMLWorkData(workDesc, *part);
            
                //add section - child of score
            Section* section = new Section;
            score->addChild(section);
                
                //handle staff and link to section
            Staff* staff = Scribe2MEIXMLStaff(scribe_data, *part, staffgrp, i);
            section->addChild(staff);
            
            xml_file_name = part->abbrev_ms + ZeroPadNumber(part->partID,4)+ " (" + std::to_string(first_part.cao_num) + ")";
            
        }
        
        //need to check if there already exists a file with the same name
        
        xml_file_name +=  ".xml";
        
        SaveMEIXML(xml_file_name);
    
    }
    
}
MeiDocument* CScribeToNeoScribeXML::Scribe2MEIXML(const CScribeReaderVisitable& scribe_data)
{
    
    Mei* mei = new Mei;
    doc->setRootElement(mei);

    //create MEIhead to contain file, encoding, work and revision description
    MeiHead* mei_head = new MeiHead; //"meiHead"
    mei->addChild(mei_head);
    
    //Create and link fileDesc
    FileDesc* fileDesc = Scribe2MEIFileDesc();
    mei_head->addChild(fileDesc);
    
    //Create and link encodingDesc
    EncodingDesc* encodingDesc = Scribe2MEIEncoderDesc();
    mei_head->addChild(encodingDesc);
    
    //Create and link workDesc
    WorkDesc* workDesc = Scribe2MEIWorkDesc();
    mei_head->addChild(workDesc);
    //</meiHead> - not really at this stage - other elements completed in main routine
    
    //music - contains all music data
    Music* music = new Music;
    mei->addChild(music);
    Mdiv* mdiv = new Mdiv; //for chant source this needs to be specified repeatedly, with n and type attributes
    music->addChild(mdiv);
    Score* score = new Score;
    mdiv->addChild(score);
    
    //start adding score definitions - child of score
    ScoreDef* scoredef = new ScoreDef;
    score->addChild(scoredef);
    StaffGrp* staffgrp = new StaffGrp;
    scoredef->addChild(staffgrp);
    staffgrp->setId("all");
    
    int i=0;
    
    for (std::vector<scribe_part>::const_iterator partit = scribe_data.GetScribeParts().begin(); partit!=scribe_data.GetScribeParts().end(); partit++)
    {
        ++i;
        if (i==1)
        {
            Scribe2MEIXMLFileData(fileDesc, *partit);
            Scribe2MEIXMLWorkData(workDesc, *partit);
        }
        
        //add section - child of score
        Section* section = new Section();
        score->addChild(section);
        
        //handle staff and link to section
        Staff* staff = Scribe2MEIXMLStaff(scribe_data, *partit, staffgrp, i);
        section->addChild(staff);
        
    }
    
    return doc;
}