Output:
<html>
<body>
<p>Hi, there.</p>
<p>I found a foo tag enclosing 'some more text' with
bar and bam values of 'this' and 'that'...</p>
<p>That's all.</p>
</body>
</html>
Output:
<html>
<body>
<p>Hi, there.</p>
<p>I found a foo tag enclosing 'some more text' with
bar and bam values of 'this' and 'that'...</p>
<p>That's all.</p>
</body>
</html>
require 'rexml/document'
doc = REXML::Document.new( input )
doc.root.each_element( '//foo' ){ |e|
new_para = REXML::Element.new( 'p' )
new_para.text = "I found a foo tag enclosing '#{e.text}' with bar and
bam values of '#{e.attributes['bar']}' and '#{e.attributes['bam']}'..."
e.parent.replace_child( e, new_para )
}
puts doc
#=> <html>
#=> <body>
#=> <p>Hi, there.</p>
#=> <p>I found a foo tag enclosing 'some more text' with bar
and bam values of 'this' and 'that'...</p>
#=> <p>That's all.</p>
#=> </body>
#=> </html>
Output:
<html>
<body>
<p>Hi, there.</p>
<p>I found a foo tag enclosing 'some more text' with
bar and bam values of 'this' and 'that'...</p>
<p>That's all.</p>
</body>
</html>
require 'xml-split.rb'
tag = 'foo'
DATA.read.xml_split(tag).each {|stuff|
if stuff.class == String
print stuff
else
attr = stuff[0].xml_parse
puts "<p>I found a #{tag} tag enclosing '#{stuff[1]}' with"
print "#{attr.keys.join(' and ')} values of "
print "'#{attr.values.join("' and '")}'...</p>"
end
}
<html>
<body>
<p>Hi, there.</p>
<p>I found a foo tag enclosing 'some more text' with
bam and bar values of 'that' and 'this'...</p>
<p>That's all.</p>
</body>
</html>
_Why's Hpricot example worked perfectly for me, BTW.
So, a related question.
Suppose I wanted to "nest" macros of this kind. Something like:
<mac1 foo="1" bar="2>My name is
<mac2 baz="3" bam="4">seed-value</mac2>
today.</mac1>
Forgive the nonsense example.
Could XSLT handle this easily? Could Hpricot (_why)?
Thanks,
Hal
Yes, both techniques could handle nested elements, I don't know what XML
tools you are using, but many come with XSLT support built in. XSLT
allows any XML(XHTML) doc to be transformed into any other. At one time
it was slated to replace .CSS but that never seeemed to materialize.
Now days, it's mostly used in report generation and xml rpc filtering
but it ofcourse has many uses. The disadvantages of XSLT is that it can
be rather challenging to debug and it can grow to be very verbose in non
trivial transformations. The advantage is that it is a W3C standard and
practically every platform/language has support for it in one form or
another.
I have no experience of Hpricot but if you are already using Ruby as
your main processor then I would probably stick with Hpricot as the
solutions above look much cleaner than an XSLT solution Oh.. and
lastly, if you don't use XSLT/XPath on a regular basis, you can easily
forget it's symantics and have to keep referring back to the docs or at
least I have to.