GithubHelp home page GithubHelp logo

Method to access element content about ox HOT 5 CLOSED

ohler55 avatar ohler55 commented on June 9, 2024
Method to access element content

from ox.

Comments (5)

ohler55 avatar ohler55 commented on June 9, 2024

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.

lovehandle avatar lovehandle commented on June 9, 2024

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.

ohler55 avatar ohler55 commented on June 9, 2024

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.

ohler55 avatar ohler55 commented on June 9, 2024

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.

lovehandle avatar lovehandle commented on June 9, 2024

That'll do the trick. Thanks!

from ox.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.