Comments (2)
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.
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)
- XmlParser should read XML version and encoding
- Normalization of XML HOT 1
- PrettyPrinter
- Reasonable equal implementation for Node
- Make AST complete
- Benchmarks HOT 1
- Scala.js io module HOT 1
- Interoperability with XML literals
- Performance tests mimicking real usage
- Add more options to PrinterConfig HOT 1
- Decide on using monocle-cats
- Ensure that no internal types are leaking into client
- Equivalent to `javax.xml.stream.isCoalescing` in `XmlParser`
- FYI xmlformat... xml ADT and encoder / decoder HOT 2
- `Element`s don't compare equal when attributes are in different order
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 xml-lens.