Skip to content

Lesson: make terms that reference attributes on xml elements

flyingzumwalt edited this page Apr 9, 2013 · 13 revisions

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

Goals

  • Define Terms in a Terminology that refer to XML attribute values rather than referring to the text within XML elements

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="given">Horn</namePart>
    <role>Author</role>
  </name>
  <name>
    <namePart type="given">Julius</namePart>
    <namePart type="given">Caesar</namePart>
    <role>Author</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.family_name(:path=>"namePart", :attributes=>{:type=>"family"})
      t.family_name(:path=>"namePart", :attributes=>{:type=>"given"})
      t.role
    }

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 = "author"
 => "author" 
fancybook.name(1).family_name = "Caesar"
 => "Caesar" 
fancybook.name(1).given_name = "Julius"
 => "Julius" 
fancybook.name(1).role = "Contributor"
 => "Contributor" 
puts fancybook.to_xml

Next Step

Go on to ]] or return to the [[Tame your XML with OM page.