XPath not() with path fields
22 November 2016 07:01 PM
|
|
IntroductionFields are a great way of restricting what parts of your documents to search, based on XML element QNames or JSON propertyNames . Fields are extremely useful when you have content in one or more elements or JSON properties that you want to query simply and efficiently as a single unit. But can you use field names you've created with XPath's fn:not ()? In other words, given a field name "test-field-name" can you do something like WorkaroundThe workaround is to create two fields, then to query across those two fields using Document 1
Document 2 xdmp:document-insert(
"/test/fields-002.xml",
<doc>
<content>
<courtcase>
<metadata>
<docinfo>
<hier>
<hierlev>
<heading>
<title>1879</title>
</heading>
<hierlev>
<heading>
<title>John had a little lamb</title>
</heading>
<hierlev>
<heading>
<title>Mary had a little lamb</title>
</heading>
</hierlev>
</hierlev>
</hierlev>
</hier>
</docinfo>
</metadata>
</courtcase>
</content>
</doc> ,
xdmp:default-permissions(),
("test", "fields")
)
Say you're interested in three different paths: 1) All titles, Which Should be defined as fn:collection()//heading/title 2) Titles with lower-level titles, Which Should be defined as fn:collection()//hierlev[.//hierlev/heading/title]/heading/title 3) Titles with NO lower-level titles, Which Should be defined as fn:collection()//hierlev[fn:not(.//hierlev/heading/title)]/heading/title Unfortunately, while we can express #3 in full XPath, we can not express #3 in the subset of XPath used to describe path fields. However, you can emulate #3 by defining fields corresponding to #1 & #2, then combining them in a cts:not-in-query. Create the path fields
Create a Path Field with name "titles-all" path "//heading/title"
Create a Path Field with name "titles-with-lower-level titles," path "//hierlev[.//hierlev/heading/title]/heading/title" Emulate the XPath you want by combining these two newly created path fields in a cts: not-in-query ()
| |
|