Skip to content

Instantly share code, notes, and snippets.

@efigarolam
Created October 13, 2013 03:50
Show Gist options
  • Save efigarolam/6957960 to your computer and use it in GitHub Desktop.
Save efigarolam/6957960 to your computer and use it in GitHub Desktop.
AttrDecl Nokogiri
require 'nokogiri'
# First I create an XML example.
# The Nokogiri::XML::AttributeDecl represents an attribute declaration in a DTD
# The following example has 3 attributes declarations each one represented
# by <!ATTLIST element-name attribute-name attribute-type attribute-value>
# you can read about DTD and XML for further references.
xml = <<eostr
<?xml version="1.0"?><?TEST-STYLE PIDATA?>
<!DOCTYPE test SYSTEM "test.dtd" [
<!ATTLIST img width CDATA "500">
<!ATTLIST p length CDATA "100">
<!ATTLIST user type (admin|moderator|member) "member">
]>
<root />
eostr
# I create a new Nokogiri XML document with the previous XML.
doc = Nokogiri::XML(xml)
# Instead of creating new instances of Nokogiri::XML::AttributeDecl
# lets use the ones that were created when I set up the Nokogiri::XML
# a.k.a doc object
attributes = doc.internal_subset.children
# Each object below will be an instance of Nokogiri::XML::AttributeDecl
# And we will getting the information for each method you asked for
# The attribute-type will return 1 for CDATA and 9 for enumerable
# Keep in mind this format:
# <!ATTLIST element-name attribute-name attribute-type attribute-value>
attributes.each do |attrdcl|
puts "The attribute #{attrdcl.inspect} has the following settings"
puts "Type: #{attrdcl.attribute_type}"
puts "Default value: #{attrdcl.default}"
puts "Possible values: #{attrdcl.enumeration.to_s}"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment