Comments (5)
I am all for making Ox easier to use. That is part of the reason for the locate() method on Ox::Element.
My first reaction to your suggestion is that the proposed syntax is ambiguous in regard to finding a component of an XML document but I can see how in a large percentage of cases XML documents are structured to no hit the confusing edge cases where the syntax would be a problem. The example I am thinking of is
abc def Asking for foo.bar could return "abc", "def", or "xyz". Ox could get around that by taking defining the convention that the first element data is returned if it exists and if it does not then the attribute data is returned. After looking more carefully at what you asked for though I realised that it is just a shortcut to nodes.first. That would be very easy to do. What do you think of the idea that I thought you were asking for? On Sep 22, 2012, at 4:33 AM, Ryan Closner [email protected] wrote: > It may just be that I missed it when I was browsing the code base, but it would be nice implement a convenience method on Ox::Element that enables easy access to the Element content. > > For example, with XML like the following: > >bar
It would be fantastic if one could retrieve the content for foo without having to do something like:element.nodes.first
#=> "bar"
Would there be any chance to implement something like the following:element.content
#=> "bar"
Thoughts?—
Reply to this email directly or view it on GitHub.
from ox.
The fact that XML enables users to express properties of an item in more than one way is an unfortunate flaw, but I think we can get around it by clearly defining the role of the #content
method.
Specifically, I'd just like a more intelligent version of #nodes
because I find it unintuitive that I have to traverse two arrays to get the element content of something like the following XML:
<? xml version="1.0" ?>
<foo>bar</foo>
Ox.parse('<?xml version="1.0"?><foo>bar</foo>').nodes.first.nodes.first
#=> "bar"
A simplification of the #content
method might look like:
module Ox
class Element
def has_child_nodes?
nodes.size > 0 && nodes.all? {|node| node.is_a?(::Ox::Node) }
end
def content
if has_children?
nodes.first
else
nodes
end
end
end
end
I don't know that it's necessary we cover all the edge cases in which XML is abused. The example posted above is a terrible use of the language (although admittedly, not uncommon). I just think that Ox could do a little better job at recognizing the value of an element and returning it in more intuitive manner.
BTW, awesome job on Ox. Fast. as. hell.
from ox.
I need to leave the current APIs in place as they are needed to get at all the elements of an XML. Take the following as an example.
barbaz You need the nodes to get at all the parts of the document, including the comments and doctype. Of course most people don't care about that stuff but some do. The locate() method provides most of what you are asking for I think. If you want a simpler API I could allow access to data simply by typing doc.foo() Where doc is the parsed XML document. I think that is about as simple as I can get it though and it would only work predictably on XML structured to not have multiple elements of the same name. I could extend it a bit more and allow doc.foo[0] as well. What do you think?from ox.
Please check out the latest Ox release. I added an 'easy' API as well as the Ox::Element.text() method. I think it will make using an Ox::Document much easier. Please let me know what you think.
from ox.
That'll do the trick. Thanks!
from ox.
Related Issues (20)
- Various thoughts HOT 10
- Use "interned" (frozen and deduplicated) Strings in Ruby 3.0+ to minimize object allocations. HOT 3
- Become `Ractor`-safe to allow usage in non-main `Ractor`. HOT 1
- Bundle install failing HOT 7
- Parse error for comments containing special characters inside a DOCTYPE declaration HOT 1
- Single carriage return is not converted to line feed HOT 16
- Easy way to get backtrace information from the C extension? HOT 18
- Process XML with instructions longer than 1024 characters. HOT 9
- Ox 2.14.7 causes segfault HOT 14
- RESX XML parsing - support for xml:space="preserve" HOT 3
- Preserve newlines in attributes and bodies when parsing. HOT 2
- [BUG] Bus Error (crashes ruby when using more than 16458 characters in a field) HOT 5
- Request to include a new mode to load which prepends xml attributes with a given string or a default HOT 1
- Ox.load result changes after parsing specific xml. HOT 2
- Sax parser segfault in v2.14.7+ HOT 4
- `StringIO` in `Ox::Builder` HOT 2
- Sax parsing with default encoding set to UTF-8 breaks Ox::Sax#text HOT 2
- OpenSSL::SSL::SSLContext::DEFAULT_CERT_STORE is not shareable across ractors HOT 1
- Issue with gems on M1 that depends on ox HOT 3
- test/tests.rb fails with ruby3.2.0preview3 and onwards HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from ox.