Enabling the XSLT extension for use in your XSLT templates.
Add the following XML snippet to your ~/config/xsltExtensions.config
file:
<XsltExtensions>
...
<ext assembly="uComponents.XsltExtensions" type="uComponents.XsltExtensions.Xml" alias="ucomponents.xml" />
...
</XsltExtensions>
Here are available methods in the Xml
library:
The FilterNodes
method is used to further filter a node-set with an XPath expression (string).
Typically you could perform these filters in XSLT directly, however there are situations where you may want to build an XPath expression dynamically, which can lead to complexities - especially for those new to developing with XSLT.
Returns: Returns an XPathNodeIterator of the filtered node-set.
| Name | Type | Notes |
|——|——|——-|
| nodeset | System.Xml.XPath.XPathNodeIterator
| |
| xpath | System.String
| |
<!-- Filter all the descendant node of the $currentPage for those that contain a 'bodyText' element (with normalized space, e.g. not empty) -->
<xsl:for-each select="ucomponents.xml:FilterNodes($currentPage, '//*[@isDoc and bodyText[normalize-space()]]')">
<xsl:value-of select="@nodeName" />
</xsl:for-each>
Get an XML document by local file path.
This method makes a call to the umbraco.library:GetXmlDocument()
method, with the addition of the cacheInSeconds
parameter - which will cache the XML document for a specified number of seconds.
| Name | Type | Notes |
|——|——|——-|
| path | System.String
| |
| relative | System.Boolean
| |
| cacheInSeconds | System.Int32
| |
<xsl:copy-of select="ucomponents.xml:GetXmlDocument('~/App_Data/data.xml', true(), 600)" />
Gets an XML document by URL.
This method is the same as the umbraco.library:GetXmlDocumentByUrl()
method, with the addition of the isGzipped
parameter - which is able to decompress Gzipped XML responses.
If isGzipped
is set to false()
, then the umbraco.library:GetXmlDocumentByUrl(url, cacheInSeconds)
is called by default.
| Name | Type | Notes |
|——|——|——-|
| url | System.String
| |
| cacheInSeconds | System.Int32
| |
| isGzipped | System.Boolean
| |
<xsl:copy-of select="ucomponents.xml:GetXmlDocumentByUrl('http://google.com/sitemap.xml', 600, false())" />
The Parse method is used for those situations where you have an XML string and need to convert it into a node-set (e.g. XML document, or rather an XPathNodeIterator).
Internally this method calls ParseXml
with the the xpath
parameter set to the root (“/”).
Returns: Returns an XPathNodeIterator of the XML string.
| Name | Type | Notes |
|——|——|——-|
| xml | System.String
| |
<xsl:copy-of select="ucomponents.xml:Parse('<root><node>Value 1</node><node>Value 2</node></root>')" />
Parses the specified XML string.
Returns: Returns an XPathNodeIterator of the XML string.
| Name | Type | Notes |
|——|——|——-|
| xml | System.String
| |
| xpath | System.String
| The XPath expression to return the node-set. |
<xsl:copy-of select="ucomponents.xml:ParseXml('<root><node>Value 1</node><node>Value 2</node></root>', '//node')" />
Parses the specified XHTML string.
Returns: Returns an XPathNodeIterator of the XHTML string.
| Name | Type | Notes |
|——|——|——-|
| xhtml | System.String
| |
<!-- Parse the HTML content of the 'bodyText' property into workable XML -->
<xsl:copy-of select="ucomponents.xml:ParseXhtml($currentPage/bodyText)" />
The RandomChildNode
method is a compliment to RandomNode
, where instead of passing a node-set to select a random node from, you pass it a parent node, of which one of the child nodes is returned at random.
Returns: Returns an XPathNodeIterator of a random child node from the parent node.
| Name | Type | Notes |
|——|——|——-|
| node | System.Xml.XPath.XPathNavigator
| |
<!-- Return a random property from the $currentPage node -->
<xsl:value-of select="ucomponents.xml:RandomChildNode($currentPage)" />
The RandomNode
method accepts a node-set and returns a single node selected at random.
It is important to note that the input node-set must be a collection of nodes, (a result tree fragment) and not a parent node. For example, calling RandomNode($currentPage)
would return itself - as there is only a single node to choose from!
Returns: Returns an System.Xml.XPath.XPathNodeIterator of a random node from the node-set.
| Name | Type | Notes |
|——|——|——-|
| nodeset | System.Xml.XPath.XPathNodeIterator
| |
<!-- Return a random content page from the $currentPage -->
<xsl:copy-of select="ucomponents.xml:RandomNode($currentPage/*[@isDoc])" />
Randomizes the sort order of the specified node’s children, (can be limited to a specified number of nodes).
Returns: Returns the node with its children randomly sorted.
| Name | Type | Notes |
|——|——|——-|
| node | System.Xml.XPath.XPathNavigator
| |
| count | System.Int32
| (optional) |
<xsl:copy-of select="ucomponents.xml:RandomSort($currentPage, 5)" />
Splits the specified delimited string into an XPathNodeIterator.
Returns: Returns an System.Xml.XPath.XPathNodeIterator representation of the delimited string data.
| Name | Type | Notes |
|——|——|——-|
| data | System.String
| A string delimited list, e.g. CSV |
| separator | System.String
| (optional - defaults to a comma ,
) |
| rootName | System.String
| (optional - defaults to ‘values’) |
| elementName | System.String
| (optional - defaults to ‘value’) |
<!-- Take a comma-separated list, split it and display as HTML list. -->
<ul>
<xsl:for-each select="ucomponents.xml:Split('hello,world,foo,bar')//values">
<li>
<xsl:value-of select="text()" />
</li>
</xsl:for-each>
</ul>