Group Abstract Group Abstract

Message Boards Message Boards

CSS Selectors 3 for Symbolic XML

Posted 8 years ago
Attachments:
POSTED BY: Kevin Daily
10 Replies

Congrats on publishing! I got the bonus workflow #1 in your slides to work but am having an issue with #2, I think it should just be CSSTargets[doc, "body"][[1]] (not [[1,1]]), if I change these lines in your presentation's last slide:

body = styleDataCell["Notebook", Notebook,  CSSTargets[doc, "body"][[1]]]
h3 = styleDataCell["question", Cell, CSSTargets[doc, "h3"][[1]]]
h1 = styleDataCell["h1", Cell, CSSTargets[doc, "h1"][[1]]]
li = styleDataCell["li", Cell, CSSTargets[doc, "li"][[1]]]
p = styleDataCell["p", Cell, CSSTargets[doc, "p"][[1]]]
h2 = styleDataCell["h2", Cell, CSSTargets[doc, "h2"][[1]]]

I get the output on the left (which doesn't look exactly right):

enter image description here

POSTED BY: Michael Sollami
POSTED BY: Kevin Daily

Ah, I posted before seeing your beautiful tutorial (somehow the paclet documentation didn't index until I restarted the front-end). All the examples in 'paclet:CSSTools/tutorial/CSSTools' work just fine!

Here's the output of your tutorial example (I'm using chrome), it only misses minor things: the FontFamily looks wrong for "h2", the small-caps of the text in "question" cells are missing, and the color of hyperlinks are a bit off...

enter image description here

Two follow-up questions:

  1. Are there any methods in CSSTools to better capture these missing pieces of typographic information?
  2. When it fails on some important property e.g. border-radius, how could I add support for that property? Thanks!
POSTED BY: Michael Sollami
Posted 6 years ago

Hi @Kevin Daily please keep us posteed when it is released.

POSTED BY: Diego Zviovich

Hi Kevin, Nice post! Did you ever publish your CSSTools` package?

POSTED BY: Michael Sollami

The CSSTools package is soooo close to a solid first release. It makes this post almost obsolete since I've reworked the selector parsing to match the tokens from my CSS tokenizer, but that all should be hidden from the end-user. Slight name changes, though. In any case, I'm continuing to update the documentation for the CSSTools package with feedback from my colleagues. (Documentation never really feels complete, right?) Priorities are still the 12.1 release, but I'm going to push for it getting onto Wolfram's Github page after the release.

POSTED BY: Kevin Daily
POSTED BY: EDITORIAL BOARD
Posted 8 years ago

I like the package. Clean implementation too.

On a related note here are two other CSS selector methods, one using a real XML processing library in Java rather than any WL hacks: https://mathematica.stackexchange.com/a/183970/38205

This will be as robust as JLink is (i.e. very robust). It is object-oriented and so much, much nicer to work with than the standard Mathematica XML headaches.

And another one I wrote up that uses pure Graph methods to implement selectors in terms of a DFS: https://mathematica.stackexchange.com/a/184417/38205

This one still performs quite well, though, and for very complex queries is conceivably cleaner than a pure Cases/Positions method. Also it references a proper Graph and thus can also be object oriented and thus attribute and property lookup from nodes is nearly instantaneous.

POSTED BY: b3m2a1 ​ 

Very useful links, thanks. In my earlier implementation I had initially created a graph as well by first tagging all XMLElements with a unique ID:

parentXML //. XMLElement[el_, at_, chld_] :> XMLElement[el, at, chld, CreateUUID["XML-"]];

and then extracting the parent-child relations into a graph. I had kept the XMLElement nesting intact instead of making a flatter association of properties, though now I wonder why I was so keen on holding on to it...

But then I limited the scope of this project. I didn't need a full traversal of the DOM. At worst I needed to look upward towards the root, or look one generation higher to calculate sibling elements. So things like the "nth-child" selector were cumbersome but do-able. And Position + Extract seems fast enough for what I was aiming for.

One thing that I couldn't see offhand in the sources you shared is how they deal with namespaces. Symbolic XML doesn't always hang on to the namespaces unless you specifically set "IncludeNamespaces" -> True during XML import, which I found a little frustrating. Instead of trying to workaround that, I put the onus on the user to include this option. Otherwise I simply fail to find a match because there's nothing else I can do; if the namespace information didn't make its way into the symbolic XML expression on import, then there's nothing that I can look for.

POSTED BY: Kevin Daily
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard