defiant.js provides the ability for you to build smart templates and query selections applicable on JSON objects - based upon standardized technologies such as XSLT & XPath.Download defiant.js
You could say that defiant.js joins XML and
related technologies with JSON.
Most likely, Douglas Crockford will hate this unholy matrimony.
Using defiant.js is pretty easy. Check out the examples below to get a hint of how it can
be used; the first one is rather simple and the latter is more advanced and exemplifies calling templates from
another template. It contains recursive template calling and renders a fictitious filesystem structure. When "tree-walker"
template is calling itself, indentation is passed as argument, thus indenting child elements of a folder.
To see the code below in action, here are the demo files; simple example and advanced example
One powerful tool when dealing with XML is the advantage of using XPath. XPath is a standardized language for addressing parts of an XML document and enables features such as analysis, transformation & refined data selection out of an XML structure. The lack of this tool when dealing with JSON has conveyed in initiatives with diverse prosperities and introduced suggestion of JSONPath, that has not been standardized (nor wideley absorbed by the community).
By using defiant.js, the power of XPath can be
unleashed on JSON objects, without the need of a new set
of expressions & syntax, as has been with JSONPath.
No re-schooling is required :-)
The syntax table below is a incision of the power that XPath brings. XPath has a lot more to offer. An important advantage of implementing XPath, as contrasted with JSONPath, is that this language has been documented a great deal on the web and information & how-to's can easily be found & digested.
|/||Child operator; selects immediate children of the left-side collection. When this path operator appears at the start of the pattern, it indicates that children should be selected from the root node.|
|//||Recursive descent; searches for the specified element at any depth. When this path operator appears at the start of the pattern, it indicates recursive descent from the root node.|
|.||Indicates the current context.|
|..||The parent of the current context.|
|*||Wildcard; selects all elements regardless of the element name.|
|@||Attribute; prefix for an attribute name.|
|@*||Attribute wildcard; selects all attributes regardless of name.|
|||Subscript operator; applies a filter pattern OR used for indexing within a collection.|
||||Union operator; results in a combination of element sets|
|()||Groups operations to explicitly establish precedence|
In order to access XPath functionality easily, some global objects has been
extended with useful methods, for instance;
Detailed information about these extension can be found in the Function reference section.
Not familiar with XPath? Don't worry, use the XPath evaluator below and you will become an expert in no time.
There are some internal procedures of defiant.js as well as known limitations of JSON (compared to XML) that you should be aware of. Below are some knowledge to keep in mind when working with XSLT, XPath and JSON.
Besides smart templating with the ability to perform transformations using XSLT on XML, & JSON structures, defiant.js is equiped with powerful methods for refined data selections and generic tools to analyze & manipulate these structures programmatically, implemented cross-browser.
xmlDoc.evaluate(xpath, xmlDoc, null, XPathResult.ANY_TYPE, null);and therefore implemented cross browser with defiant.js.
selectNodeswith the difference that it returns the first match of the XPath expression and exits the search algorithm as soon as the first match is found. It is a preferable alternative if a single node is known to be desired, hence better choise performance-wise.
XMLSerializerand call its function
serializeToString. This property outputs a human-readable version of the serialized XML string, i.e. with line breaks and tabbed rows. Lastly, this property is accessible from all element nodes, regardless of depth in the XML structure (the document node included).
JSON.search(object, xpath, first)
booleantype; if set to
true, the function exists the search algorithm, as soon as a matching element is found, based upon given XPath. The function
JSON.searchreturns an array with agregate functions, populated with matching elements. Besides the ability find elements with XPath, this function returns references to the matching elements in the JSON structure. Put differently; the matched elements can be altered with a loop, thus altering the original JSON object. See the code below as example.
powered by defiant.js