Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

create csv to TEI taxonomy node script #13

Open
tcatapano opened this issue Apr 19, 2023 · 3 comments
Open

create csv to TEI taxonomy node script #13

tcatapano opened this issue Apr 19, 2023 · 3 comments
Assignees
Labels
enhancement New feature or request

Comments

@tcatapano
Copy link
Member

to go from google sheets/excel --> csv --> TEI taxonomy, where csv has form of

term, label, description
@tcatapano tcatapano added the enhancement New feature or request label Apr 19, 2023
@tcatapano tcatapano self-assigned this Apr 19, 2023
@tcatapano
Copy link
Member Author

@njr2128, where can I find either the spreadsheet or csv's we used for this?

@tcatapano
Copy link
Member Author

@NickLaiacona : see 801f0d9; i.e., https://github.com/cu-mkp/editioncrafter-data/blob/issue12/lib/csv2xml-taxonomy.js (Accidently linked to issue12.)

const csv = require('csvtojson');
const builder = require('xmlbuilder');
const fs = require('fs');

const csvFilePath = 'input.csv';
const xmlFilePath = 'output.xml';

// Map CSV columns to XML elements and attributes with custom names
const elementMappings = {
  'description': 'catDesc',
};
const attributeMappings = {
  'label': 'xml:id',
  'term': 'n'
};

// Convert CSV to JSON
csv()
  .fromFile(csvFilePath)
  .then((json) => {
    // Create root element
    const root = builder.create('taxonomy');

    // Loop through JSON data and create XML elements and attributes with custom names
    json.forEach((item) => {
      const element = root.ele('category');
      Object.keys(item).forEach((key) => {
        if (elementMappings.hasOwnProperty(key)) {
          element.ele(elementMappings[key], item[key]);
        } else if (attributeMappings.hasOwnProperty(key)) {
          element.att(attributeMappings[key], item[key]);
        }
      });
    });

    // Write XML to file
    const xml = root.end({ pretty: true });
    fs.writeFileSync(xmlFilePath, xml);
  });

tcatapano added a commit that referenced this issue Apr 19, 2023
tcatapano added a commit that referenced this issue Apr 19, 2023
@NickLaiacona
Copy link
Collaborator

Nice! Could you open a Pull Request that requests to merge this branch to the dev branch? I can then give my feedback on the PR and we can then merge it into dev as a single, squashed commit. My pattern is to make one commit per feature on the dev branch, and then merge all those commits (not squashed) into the main branch when we do a release.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants