Skip to content

Lesson: make terms that reference xml elements with certain attribute values

jcoyne edited this page Oct 22, 2014 · 8 revisions

This Tutorial is known to work with om version 3.0.4.
Please update this wiki to reflect any other versions that have been tested.

Goals

  • Define Terms in a Terminology that refer to XML elements with specific attribute values

Explanation

Steps

Step 1: Think about what the XML is going to look like

<fields>
  <title>ZOIA! Memoirs of Zoia Horn, Battler for the People's Right to Know.</title>
  <name>
    <namePart type="given">Zoia</namePart>
    <namePart type="family">Horn</namePart>
    <role>
      <roleTerm type="text">Author</roleTerm>
      <roleTerm type="code">AUT</roleTerm>
    </role>
  </name>
  <name>
    <namePart type="given">Julius</namePart>
    <namePart type="family">Caesar</namePart>
    <role>
      <roleTerm type="text">Contributor</roleTerm>
      <roleTerm type="code">CON</roleTerm>
    </role>
  </name>
</fields>

Step 2: Modify the Terminology

Reopen fancy_book_metadata.rb and modify the family_name and given_name terms

    t.name_ {
      t.namePart
      t.family_name(path: "namePart", attributes: { type: "family" })
      t.given_name(path: "namePart", attributes: { type: "given" })
      t.role {
              t.text(path: "roleTerm", attributes: { type: "text" })
              t.code(path: "roleTerm", attributes: { type: "code" })
          }
    }

Save the file.

Step 3: Try modifying an OM Document based on the Terminology

Restart the console

bundle console

Require the FancyBookMetadata class definition.

require "./fancy_book_metadata"
fancybook = FancyBookMetadata.new

Now set the family_name and given_name just as you did in the previous lesson. We didn't change the Term names in our terminology, so the method names stay the same but the resulting XML is structured differently.

fancybook.name.given_name = "Zoia"
 => "Zoia" 
fancybook.name.family_name = "Horn"
 => "Horn" 
fancybook.name.role.text = "author"
 => "author" 
fancybook.name.role.code = "AUT"
 => "AUT" 
fancybook.name(1).family_name = "Caesar"
 => "Caesar" 
fancybook.name(1).given_name = "Julius"
 => "Julius" 
fancybook.name(1).role.text = "Contributor"
 => "Contributor" 
fancybook.name(1).role.code = "CON"
 => "CON" 
puts fancybook.to_xml
<?xml version="1.0"?>
<fields>
  <name><namePart type="given">Zoia</namePart><namePart type="family">Horn</namePart><role><roleTerm type="text">author</roleTerm><roleTerm type="code">AUT</roleTerm></role></name>
  <name><namePart type="family">Caesar</namePart><namePart type="given">Julius</namePart><role><roleTerm type="text">Contributor</roleTerm><roleTerm type="code">CON</roleTerm></role></name>
</fields>
 => nil 

Next Step

Go on to Lesson: Make your Terminology aware of XML Namespaces or return to the Tame your XML with OM page.