Skip to content

Lesson: make terms that reference attributes on xml elements

jcoyne edited this page Oct 22, 2014 · 13 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 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

Now say we want to use the xml:lang attribute to track the language encoding of the title and we want to have an OM Term called title_language that lets us read and modify that attribute's value.

<fields>
  <title lang="eng">ZOIA! Memoirs of Zoia Horn, Battler for the People's Right to Know.</title>
</fields>

Step 2: Define the Term in your Terminology

Reopen fancy_book_metadata.rb and add a language nested inside the title Term

    t.title {
      t.language(path: { attribute: "lang" })
    }

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
fancybook.title = "Zoia!"
 => "Zoia!" 
fancybook.title.language = "eng"
puts fancybook.to_xml
<?xml version="1.0"?>
<fields>
  <title lang="eng">Zoia!</title>
</fields>
 => nil 

Next Step

Go on to Lesson: Make Terms that reference XML elements with certain attribute values or return to the Tame your XML with OM page.