Emflix – Part Six – XML – Directors, Writers

Link to Part Five

 

So, directors and writers, the people of the movie biz! (yes this would later extend to actors, but in my initial creation of Emflix I didn’t include actors, and had never intended to)

This posed a real challenge for me, as I didn’t have any of the 700 fields in the exports I was given. All I really had to go on was the 245, subfield c.

The instruction was:

<xsl:apply-templates select="Heading2" mode="director"/>
<xsl:apply-templates select="Heading2" mode="writer"/>

and the matching templates are as follows:

<xsl:template match="Heading2" mode="writer">   
         <xsl:sequence
             select="functx:extract(normalize-space(.),('written, produced, directed by ','screen play, ','writer and director, ','written for the screen by ','written for the screen, produced and directed by ','produced, written, and directed by ','written &amp; produced by ','written for the screen &amp; directed by ','written, directed and edited by ','script by ','screenplay and directed by ','written and edited by ','screen story by ','screen story &amp; dialogue by ',' written, edited &amp; directed by ','writers and directors, ','writer, ','written, produced &amp; directed by ','screenplay &amp; dialogues, ','writers, ','screenplay by ','written by ','written and directed by ','written for the screen and directed by ','screen play and dialogue by ','screenplay writer, ','screenplay, ','written, produced, and directed by ','written, produced and directed by ','written, directed and produced by ','screen play by ','written and produced by ','written &amp; directed by ','writer-director, ','script, ','screenplay, music, and direction by '),'writer')"
         />
     </xsl:template>
 
     <xsl:template match="Heading2" mode="director">
         <xsl:sequence
             select="functx:extract(normalize-space(.),('written, directed and edited by ','director/writer, ','directed &amp; produced by','directors, ','direction by ','directed by ','director, ','directed and written by ','directed and produced by ','directed, written and produced by ','direction, '),'director')"
         />
     </xsl:template>

Explanation: Because I didn’t have the 700 fields, I had to somehow get the data out of the 245, but I only wanted the names of writers or directors. I wrote a function which accepted three parameters

  1. The 245 field
  2. A series of 1 or more strings
  3. The name of the element I wanted to create (notice that the final parameter in the director template is ‘director’ and the final parameter in the writer template is ‘writer’.)

This is that function:

<xsl:function name="functx:extract" as="element()*">
         <xsl:param name="input" as="xs:string"/>
         <xsl:param name="markers" as="xs:string*"/>
         <xsl:param name="element-name" as="xs:string"/>
         <xsl:analyze-string select="$input" regex="({string-join($markers, '|')})([^;]+)(\s;|$)">
             <xsl:matching-substring>
                 <xsl:analyze-string select="regex-group(2)"
                     regex="([^,]+?)(((\sand|,|\s&amp;)\s)|$)">
                     <xsl:matching-substring>
                         <xsl:element name="{$element-name}">
                             <xsl:if test="matches(regex-group(1),'^\w+\s\w+$')">
                                 <xsl:attribute name="sort">
                                     <xsl:value-of select="string-length(substring-before(regex-group(1),' ')) + 2"/>
                                 </xsl:attribute>
                             </xsl:if>
                             <xsl:value-of
                                 select="functx:substring-before-if-ends-with(normalize-space(regex-group(1)),'.')"/>
                         </xsl:element>
                     </xsl:matching-substring>
                 </xsl:analyze-string>
             </xsl:matching-substring>
             <xsl:fallback>
                 <xsl:element name="{$element-name}"/>
             </xsl:fallback>
         </xsl:analyze-string>
     </xsl:function>

Complicated, right?! Lemme break it down.

The function call performs in discrete steps:

  1. It joins together all those markers passed in the function call (I added each one as I encountered it in an actual movie record) separated by a vertical bar (which is the boolean ‘or’ in regex) as group 1, group 2 is one or more characters that are not a semicolon and group 3 is either a space following by a semicolon or the end of the string.
  2. It selects group 2 (which ideally is a person or people’s names!) up until “space semicolon” (the separator used in MARC 245s to separate roles)
  3. Of that string, it extracts everything up until the first comma, or first space-‘and’, or space-& or end of string. (Yes it repeats for multiple people’s names)
  4. Then, finally, it constructs an element of whatever the third parameter passed was and in addition it constructs the all important sort attribute for names that match the pattern of Characters-space-characters. I.e. it doesn’t match any people’s names that are 3 words. Those I had to add manually.
  5. Then finally it removes a trailing period, if there is one, using a function I wrote that is so heavily based on this one, that I’m leaving its creation as an exercise to the reader (jk if you want it i’d be happy to provide)

 

Some notes:

I started the markers with ones I just wrote myself “written by, ” “directed by, “, etc. But I found it was inefficient to try to make them up and just added them as I encountered them. Eventually I had so many which represented the vast majority of things that’d appear in a 245, I didn’t really need to worry about adding any more

The reason I only had the sort attribute added for two-word names is I couldn’t be sure in a three-word (or more) person which name would be the sort name! So those I had to do manually, after looking them up.

I know you’re probably wondering why I removed a trailing periods the final step rather than just excluding it from the matching strings in the regex! Well at first I had been doing that…until I encountered someone’s name with a period in it. Samuel L. Jackson, D.L. Hughley, etc. so it ended up being better to just grab the whole name, and if it had a period at the end, strip it there. Of course, the thing about names is that you never really can predict every variation or situation. I always recommend visually inspecting the work before moving on.

 

Whew! That was a complex one! Next time we’re going to look at the LC Special Topics and the Language. Stay tuned!

Advertisements

3 thoughts on “Emflix – Part Six – XML – Directors, Writers

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s