GithubHelp home page GithubHelp logo

Comments (2)

note avatar note commented on June 14, 2024

Hey @howyp, thank you for your interest in xml-lens and reporting your problem. It's especially appreciated taking into account that this is early version of the lib.

I managed to make it work for your case. First let's define a general Prism[Element, Element] out of given predicate:

def being(predicate: Element => Boolean): Prism[Element, Element] =
    Prism[Element, Element]{ el =>
      if (predicate(el)) {
        Some(el)
      } else {
        None
      }
    }(identity)

Then you can define your query as:

val havingAttrValPredicate = ElementOptics.being(el => ElementOptics.attribute("example")
        .getOption(el)
        .exists(_ == "2"))
val fetch = (root \ "b" \ "c").composePrism(havingAttrValPredicate)

Now you should be able to use such defined fetch on concrete XmlDocument:

fetch.getAll(XmlParser.parse(xml).right.get)

Does it work? Yes. Is it comfortable? No, it's embarrassingly complicated and I will work to make it easier in future. In particular being method should be part of library. The general idea of improving xml-lens is first by defining a set of general lenses (namely pl.msitko.xml.optics package) and then to try to build on top of them a bunch of useful combinators (namely pl.msitko.xml.dsl package). I find the problem you reported very useful for both of objectives.

You can track the progress on this ticket: #39 if you have any other suggestion you can comment there.

Will close that ticket after above mentioned PR is merged.

from xml-lens.

prgsmall avatar prgsmall commented on June 14, 2024

I had a similar need and created a fork to work it into the app that I was creating.
Here is a link to the pull request: #44

from xml-lens.

Related Issues (16)

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.