macOS includes a built-in XQuery engine (NSXMLNode.ObjectsForXQuery
), and supports XInclude
, so that we can apply a query like:
//p[ matches(., $Search_Pattern, "i") ]
not just over one Bike document, but over a whole folder-full, and generate clickable reports like the following, in which the links take you straight to the Bike document, and to the specific line that contains a match.
with a Keyboard Maestro macro like:
XQuery search over Bike folder.kmmacros.zip (5.3 KB)
which requires you to have the following Keyboard Maestro subroutine in any active KM macro group.
XQuery over XML SUBROUTINE.kmmacros.zip (3.2 KB)
Useful.
You may notice, however, that:
- My Bike files (one per day) have ISO8601 dates in their names
- the most recent matches returned above are from 2023. None of the matches in 2024 or 2025 (or even in late 2023) are found.
The problem seems to be that files written out by Bike now (since a request made in Aug 2023) decorate their enclosing <html>
tag with the XHTML name-space attribute:
xmlns="http://www.w3.org/1999/xhtml"
At the time, it looked as if this very reasonable request entailed no side effects, but I should have realised a long time ago why:
- more recent Bike files seemed invisible to XQuery over an XIncluded folder
- older bike files that did show up in the queries suddenly vanished from them if re-saved by current builds of Bike 1.0
It seems that adding xmlns="http://www.w3.org/1999/xhtml"
to the <html>
tag may shadow the xmlns:xi="http://www.w3.org/2003/XInclude"
namespace declaration of a tag that wraps the filepaths of the files in the folder, allowing them to be treated as a single NSXMLDocument
by XQuery
.
In other words, XInclude appears to stop including the XML at the point where it finds a competing xmlns
namespace declaration.
Expand disclosure triangle to view XInclude source
<?xml version="1.0" encoding="utf-8"?>
<group xmlns:xi="http://www.w3.org/2003/XInclude">
<doc path="/Users/houthakker/DayNotes/" file="welcome.bike">
<xi:include href="file:///Users/houthakker/DayNotes/welcome.bike"/>
</doc>
<doc path="/Users/houthakker/DayNotes/" file="notes2025-04-06.bike">
<xi:include href="file:///Users/houthakker/DayNotes/notes2025-04-06.bike"/>
</doc>
<doc path="/Users/houthakker/DayNotes/" file="notes2025-04-05.bike">
<xi:include href="file:///Users/houthakker/DayNotes/notes2025-04-05.bike"/>
</doc>
<doc path="/Users/houthakker/DayNotes/" file="notes2025-04-04.bike">
<xi:include href="file:///Users/houthakker/DayNotes/notes2025-04-04.bike"/>
</doc>
<doc path="/Users/houthakker/DayNotes/" file="notes2025-04-03.bike">
<xi:include href="file:///Users/houthakker/DayNotes/notes2025-04-03.bike"/>
</doc>
<doc path="/Users/houthakker/DayNotes/" file="notes2025-04-02.bike">
<xi:include href="file:///Users/houthakker/DayNotes/notes2025-04-02.bike"/>
</doc>
<doc path="/Users/houthakker/DayNotes/" file="notes2025-04-01.bike">
<xi:include href="file:///Users/houthakker/DayNotes/notes2025-04-01.bike"/>
</doc>
<doc path="/Users/houthakker/DayNotes/" file="notes2025-03-29.bike">
<xi:include href="file:///Users/houthakker/DayNotes/notes2025-03-29.bike"/>
</doc>
</group>
Any chance of making the xmlns="http://www.w3.org/1999/xhtml"
optional when the <html>
tag is written out ?
It seems a pity not to be able to use standard XML tools, built into the Apple Foundation classes, and available to JXA (and AppleScript), like XQuery
and XInclude
…
UPDATE: Same problem for other versions and implementations of XQuery
I’ve just tested .bike
files with and without the late 2023 addition of the namespace
xmlns="http://www.w3.org/1999/xhtml"
in Saxon implementations of XQuery, and with XQuery 3.1
(rather than the XQuery 1.0
in Apple’s NSXMLDocument
Same result:
- XIncluded bike files vanish from XQuery results if their
<html>
tag contains that xmlns schema declaration, and - reappear as soon as the tag is restored to a vanilla
<html>
Tested: Saxon-PE XQuery 12.5, running in Oxygen XML,
and also Saxon-HE and Saxon-EE equivalents.
Perhaps this sheds light ?
Perhaps writing out xmlns="http://www.w3.org/1999/xhtml"
might work better as an option than as a default ?
In the meanwhile, while one could theoretically use a command line to remove all those xmlns declarations, before perfoming a query, doing so would, I think, break the path-independent bike://
row links to those files by losing the com.apple.metadata:kMDItemIdentifier
extended file attribute.
(And a command line pre-process takes time, alarms me, and I would not recommend to it anyone