goofballLogic / ld-query

Querying JSON-LD
MIT License
20 stars 6 forks source link

Precedence of result from 'query' #13

Closed gareth-robinson closed 7 years ago

gareth-robinson commented 7 years ago

Given a document that includes

[
  {
    ...,
    "http://example.org#annotations": [
      {
        "@type": [
          "https://www.w3.org/TR/annotation-model/Annotation"
        ],
        "https://www.w3.org/TR/annotation-model/body": [
          {
            ...,
            "http://example.org#identifier": [
              {
                "@value": "annotation1_identifier"
              }
            ]
          }
        ],
      },
    ],
    "http://example.org#identifier": [
      {
        "@value": "12345"
      }
    ],
  }
]

what should we expect query( "ex:identifier" ) to return? The "annotation1_identifier" or "12345"? At the moment it is "annotation1_identifier", the first entry that matches the query.

If this was html e.g.

<div class="b">
  <div class="b">
    <div class="a">test1</div>
  </div>
</div>
<div class="a">test2</div>

then document.querySelector(".a").innerHTML returns "test1", so that matches the functionality of the ld-query 'query' method

To get 'test2' in the html we'd need to use document.querySelector( "body > .a" )

Does ld-query need changed to find the 'simplest' match for a path? Or does it need to include additional operator types like '>'? Or can the 'expand' method that it uses internally be externalised, so that developers can process json manually (though that's a bit of a fudge)?

gareth-robinson commented 7 years ago

Ok, if it was known that in my example document above, that the document was always in the layout above and the 'root' identifier was always included, then I could obtain the value I need using

var allIdentifiers = doc.queryAll( "ex:identifier @value" );
var rootIdentidier = allIdentifiers[  allIdentifiers.length - 1 ];
goofballLogic commented 7 years ago

See here for the rules regarding order of selection: https://dev.w3.org/2006/webapi/selectors-api2/#first

Or, if you are a human, see here for MDN's statement that it is "depth-first, pre-order" traversal: https://developer.mozilla.org/en-US/docs/Web/API/Document/querySelector

goofballLogic commented 7 years ago

I think the most obvious example of this problem would be when trying to select the @id of the document

goofballLogic commented 7 years ago

Looks like this is a discussion for HTML as well - see here: https://groups.google.com/forum/#!topic/jsmentors/SYnpRji5TZU

Consensus seems to be that use of the child combinator is the solution for now. I suggest that we should also implement that for this case.

https://drafts.csswg.org/selectors-4/#child-combinators

gareth-robinson commented 7 years ago

I've added child combinators into my fork. Pull request is https://github.com/goofballLogic/ld-query/pull/14