Jump to content

Recommended Posts

Posted

Did you know that you can use JSON in our Saxon functions? Originally Saxon is a XML library, but it can take JSON as input in various queries. The queries internally work with maps and arrays and these can be filled by JSON.

Let us show you how to use JSON for Saxon.XPathQuery and Saxon.XQuery functions:

Saxon.XPathQuery

XPathQuery (commonly called XPath) is a language used to navigate and select nodes from an XML document using path-like expressions. It's often used within XSLT, XQuery, and XML-processing tools to pinpoint data locations efficiently.

We can use the Saxon.XPathQuery function in MBS FileMaker Plugin to run XPath queries directly in FileMaker. Check our example databases and try a few sample queries. In general you pass the XML document and the query.

XPath query syntax uses path-like expressions to navigate through elements and attributes in an XML document. It starts with / for absolute paths (from the root) or // for relative paths (anywhere in the document). Nodes are selected by names (e.g., /bookstore/book), and filters can be applied using square brackets (e.g., //book[price>30]). You can also use @ to select attributes (e.g., //book[@category='fiction']) and functions like text(), contains(), or position() to refine queries.

You can run a XPath query on the JSON. Since the JSON doesn't have nodes like XML, you need to pick something from a map or array using the question mark operator.

Here is an example query:

Set Variable [ $r ; Value: MBS( "Saxon.XPathQuery"; "{ \"name\": \"John\", \"age\": 30, \"city\": \"New York\" }"; "?name" ) ]
Set Field [ Saxon XSLT::Result ; $r ]

Example result: John

As you see, we put the JSON directly in the call and quote it. Backlashes before the quotes make FileMaker to pass the quotes through to the plugin.

Saxon.XQuery

XQuery is a powerful query language designed to extract and manipulate data from XML documents. It can perform complex operations such as filtering, joining, and transforming XML data much like SQL does for relational databases.

XQuery syntax is designed to query and transform XML data using FLWOR expressions—For, Let, Where, Order by, and Return. It allows variables (prefixed with $), path expressions (like XPath), conditional logic (if, where), and supports functions to manipulate XML or return results in various formats. XQuery builds on XPath, so all XPath expressions are valid inside XQuery.

Once you have working XPath queries, you can embed it in the html or xml with XQuery. In our XQuery we pass the html part <p> and inside that we use {} to put in the XPath query to find the name entry from the JSON data:

Set Variable [ $r ; Value: MBS( "Saxon.XQuery"; "{ \"name\": \"John\", \"age\": 30, \"city\": \"New York\" }"; "<p>{ data(?name) }</p>" ) ]
Set Field [ Saxon XSLT::Result ; $r ]

Please try this with our 15.3 plugin. Check out other Saxon articles in our blog:

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.