0

So I'm trying to write a function that does the following: I have about 20 or so XML files (someday I will have over a hundred) and in the header of each file is the name of a person who was a peer review editor <editor role="PeerReviewEditor">John Doe</editor>. I want to run through the directory where these files are stored and capture the name of the Peer-Review-Editor for that file. I want to end up with an variable $reviewEditorNames that contains all of the different names. (I will then use this to display a list of editors, etc.)

Here's what I've got so far. I'm worried about the last part. I feel like the attempt to turn $editorReviewName into $editorReviewNames is not going to combine the individuals for each file, but an array found within a given file (even if there is only one name in a given file, and thus it is an array of 1)

I'm grateful for your help.

function editorlist()
{
    $filename = readDirectory('../editedtranscriptions');
    foreach($filename as $file)
    {
        $xmldoc = simplexml_load_file("../editedtranscriptions/$file");
        $xmldoc->registerXPathNamespace("tei", "http://www.tei-c.org/ns/1.0");
        $reviewEditorName = $xmldoc->xpath("//tei:editor[@role='PeerReviewEditor']");

        return $reviewEditorNames[] = $reviewEditorName;
    }


}
1
  • 1
    build up the array in the foreach loop, return it after the foreach. Also... make a empty array before the loop... Commented Jul 29, 2011 at 8:53

2 Answers 2

2

I would put things more apart, that helps as well when you need to change your code later on.

Next to that, you need to check the return of the xpath, most likely you want to process only the first match (is there one editor per file?) and you want to return it as string.

If you put things into functions of it's own it's more easy to make a function to only do one thing and so it's easier to debug and improve things. E.g. you can first test if a editorFromFile function does what it should and then run it on multiple files:

/**
 * get PeerReviewEditor from file
 *
 * @param string $file
 * @return string
 */
function editorFromFile($file)
{
    $xmldoc = simplexml_load_file($file);
    $xmldoc->registerXPathNamespace("tei", "http://www.tei-c.org/ns/1.0");

    $node = $xmldoc->xpath("//tei:editor[@role='PeerReviewEditor'][1]");
    return (string) $node[0];
}

/**
 * get editors from a path
 *
 * @param string $path
 * @return array
 */
function editorlist($path)
{
    $editors = array();
    $files = glob(sprintf('%s/*.xml', $path), GLOB_NOSORT);
    foreach($files as $file)
    {
        $editors[] = editorFromFile($file);
    }
    return $editors;
}
Sign up to request clarification or add additional context in comments.

3 Comments

I like this, thanks very much. Is the %s/* another way of selecting all the files in a directory (different from the readdir function I usually use)?
Just tried it, works great. I think this will help me take one step forward, (maybe I should make this a new question) but what if I the want the resulting array $editors to skip null values (i.e. xpath queries that don't find the named element) Not all of my files have PeerReviewEditors yet. I think is probably something we could add to the editorFromFile function. Any ideas?
nice. I really appreciate your help
2

Just a little update:

function editorlist() {
  $reviewEditorNames = array(); // init the array

  $filename = readDirectory('../editedtranscriptions');
  foreach($filename as $file) {
    $xmldoc = simplexml_load_file("../editedtranscriptions/$file");
    $xmldoc->registerXPathNamespace("tei", "http://www.tei-c.org/ns/1.0");

    // add to the array
    $result = $xmldoc->xpath("//tei:editor[@role='PeerReviewEditor']");
    if (sizeof($result) > 0) {
      $reviewEditorNames[] = (string)$result[0];
    }
  }

  // return the array
  return $reviewEditorNames;
}

2 Comments

Thanks very much. This seems to work, but it took me awhile to realize that $reviewEditorNames is actually an array of arrays and I had to break it down twice. Any advice on to turn all the names into one array rather than array of arrays?
@Jeff: See my answer below, it deals with such issues and more.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.