[selectors4] :only-of-attribute, -key, -value

I recently had to select a node whose element type appears more than once in a document, but it was the only one using a certain attribute, say “foo”. (Let’s assume the attribute is exclusive to this element type so we can leave out its name.) In other documents using the same stylesheet, however, there are more than one instance of such elements and they should *not* be selected. Therefore

  [foo]

does not work and, actually, using Selectors 3 it doesn’t seem possible to select it directly. What I would have needed is something like the following:

  [foo]:only-of-key             [foo]:only-with-key

Although I didn’t need it yet, the same could be said about a certain value:

  [foo=bar]:only-of-value       [foo=bar]:only-with-value

If the combination of key and value had to be the same, which should be enough for most cases including mine:

  [foo]:only-of-attribute       [foo]:only-with-attribute

Example

  <E/>         – 1
  <E foo=bar/> – 2
  <E foo=baz/> – 3
  <E quz=bar/> – 4

  [quz]                         matches     #4
  [quz]:only-of-attribute       would match #4
  [quz]:only-of-key             would match #4
  [quz]:only-of-value           would match #4
  [foo]                         matches     #2 and #3
  [foo]:only-of-attribute       would match #2 and #3
  [foo]:only-of-key             would match nothing
  [foo]:only-of-value           would match #2 and #3
  [foo=bar]                     matches     #2
  [foo=bar]:only-of-attribute   would match #2
  [foo=bar]:only-of-key         would match #2
  [foo=bar]:only-of-value       would match #2
  [*=bar]                       would match #2 and #4
  [*=bar]:only-of-attribute     would match #2 and #4
  [*=bar]:only-of-key           would match #4
  [*=bar]:only-of-value         would match nothing
  [] /* = *[*] = *[*=*] */      would match #2, #3 and #4
  []:only-of-attribute          would match #2, #3 and #4
  []:only-of-key                would match #4
  []:only-of-value              would match #3
  :only-of-attribute            would match nothing
  :only-of-key                  would match nothing
  :only-of-value                would match nothing

I’m not sure how this would work with multiple attributes, though.

Received on Saturday, 31 March 2012 14:52:34 UTC