‘xslt_transform_xml’ is the plugin’s primary function. This shortcode processes XML data using an XSL stylesheet, and then outputs the result as HTML, more XML, or as simple TEXT.
[xslt_transform_xml/]
is the plugin’s primary function. This shortcode processes XML data using an XSL stylesheet, and then outputs the result as HTML, more XML, or as simple TEXT.
There are four (4) options for specifying the XSL and/or XML for a transform when using the xsl
and xml
attributes:
local file: | xsl="/path/to/local/stylesheet.xsl" |
xml="/path/to/local/data.xml" |
remote file: | xsl="http://abc.de/remote/stylesheet.xsl" |
xml="http://abc.de/remote/data.xml" |
item id: | xsl="1234" |
xml="6789" |
item slug: | xsl="wp-stylesheet-xsl" |
xml="wp-data-xml" |
The XML for a transform can also be input directly in the body of the shortcode. This results in two (2) usage patterns:
[xslt_transform_xml xsl="{file|url|id|slug}" xml="{file|url|id|slug}" /]
[xslt_transform_xml xsl="{file|url|id|slug}"]<DATA>...</DATA>[/xslt_transform_xml]
The second pattern allows [xslt_transform_xml/]
to be used in tandem with the select shortcodes [xslt_select_xml/]
or [xslt_select_csv/]
.
[xslt_transform_xml][xslt_select_xml/][/xslt_transform_xml]
[xslt_transform_xml][xslt_select_csv/][/xslt_transform_xml]
The second pattern also allows [xslt_transform_xml/]
to be used with itself, but with one adjustment. WordPress does not support nested shortcodes with identical names, so, you must rename one or the other of the shortcodes as xslt_transform_alias
. In this way, you could write and use your own version of the xslt_select_xml
utility, for instance.
[xslt_transform_alias][xslt_transform_xml/][/xslt_transform_alias]
Cache parameters
When the shortcode specifies a remote file – xml="{url}"
or xsl="{url}"
– that url source is cached locally using WordPress Transients . The default duration – 60 Minutes – can be adjusted in Settings >> XSLT Processor >> Cache Lifetime
. Setting “0 Minutes” disables caching by default.
Individual transformations can override the default with the attribute cache="{minutes}"
, or specify cache="0"
to bypass caching.
Stylesheet parameters
Attributes specified in the shortcode are passed along as parameters to the stylesheet. For example, if myparam="myvalue"
is added to the shortcode, that value is available within the stylesheet using <xsl:param name="myparam">my default value</xsl:param>
.
For a complete example of this feature, see How To >> 101 – Sample.
Default data and stylesheet
The default XSL, if left unspecified, reads the input XML and then prints the source as HTML. The result is similar to PHP’s htmlentities()
function, but with XML syntax checking. If the input is not well-formed, an error message is returned. This is a convenient method for confirming the validity of your XML, prior to applying (and debugging) your own stylesheet.
The default XML, as this first example shows, is a single node: <NODATA/>
[xslt_transform_xml /]
Output:
<NODATA/>
XML in local file
You may specify just a file name, without additional path information, if the parent directory of the file is listed in Settings >> XSLT Processor >> Local File Search Path
.
[xslt_transform_xml xml="sample.xml" /]
Output
<sample> <comment>This is a test XML file. It contains this <comment> node, followed by a <list> node containing several <item> nodes, and a <dates> node containing several <date> nodes.</comment> <list title="My List"> <item value="1">One</item> <item value="2">Two</item> <item value="3">Three</item> <item value="4">Four</item> </list> <dates> <date>2022-12-02 12:12:12</date> <date>2023-03-03 13:13:13</date> </dates> </sample>
XML in remote file
By default, remote files are cached locally per Settings >> XSLT Processor >> Cache Lifetime
. That setting can be overridden by adding the attribute cache="{minutes}"
.
[xslt_transform_xml xml="https://xsltproc.tenandtwo.com/wp-content/uploads/upload_sample.xml" cache="10" /]
Output:
<sample> <comment>This is the uploaded version of sample.xml</comment> <comment>This is a test XML file. It contains this <comment> node, followed by a <list> node containing several <item> nodes, and a <dates> node containing several <date> nodes.</comment> <list title="My List"> <item value="1">One</item> <item value="2">Two</item> <item value="3">Three</item> <item value="4">Four</item> </list> <dates> <date>2022-12-02 12:12:12</date> <date>2023-03-03 13:13:13</date> </dates> </sample>
XML or XSL in wp_content
The recommended method for managing data and stylesheets within WordPress, is via the XSLT Processor plugin's custom post types - XSL Stylesheets
and XML Documents
. Once activated in Settings >> XSLT Processor >> Activate Content Types
, both types appear in the main administration menu. Like other WordPress items, the custom types have 'slug' identifiers, which can be found or changed in the Quick Edit form.
To access WordPress data for a transformation, specify either an 'id' or a 'slug' value - xml="12345"
or xsl="my-item-slug"
. If the custom post types are active, 'slug' values will only match XSL Stylesheets
or XML Documents
. If inactive, both 'id' and 'slug' values will match any WordPress post type ('post', 'page', etc).
In this example, we reference custom XSL and XML items using their 'slug' values. Just below the Output, note that the XML Document
is a variation on our usual sample data, and the XSL Stylesheet
is a variation on our usual print-nodes
stylesheet.
[xslt_transform_xml xml="sample-xml" xsl="print-node-names-xsl" /]
Output:
ERROR : XSLTProcessor->importStyleSheet(xsl):
----------------------------------------
Warning 1549: failed to load external entity "/srv/xsltproc.tenandtwo.com/htdocs/wp-content/plugins/tenandtwo-xslt-processor/xsl/util.xsl"
Line: 0
Column: 0
----------------------------------------
----------------------------------------
Error 1: compilation error: file /srv/plugins.tenandtwo.com/htdocs/ line 5 element include
Line: 0
Column: 0
----------------------------------------
----------------------------------------
Error 1: xsl:include : unable to load /srv/xsltproc.tenandtwo.com/htdocs/wp-content/plugins/tenandtwo-xslt-processor/xsl/util.xsl
Line: 0
Column: 0
----------------------------------------
WP XML Document - "sample-xml"
<sample> <comment>This is a test WordPress XML Document item</comment> <list title="My List"> <item value="1">One</item> <item value="2">Two</item> <item value="3">Three</item> <item value="4">Four</item> </list> <dates> <date>2022-12-02 12:12:12</date> <date>2023-03-03 13:13:13</date> </dates> </sample>
WP XSL Stylesheet - "print-node-names-xsl"
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:output method="html" version="1.0" encoding="UTF-8" indent="yes"/> <xsl:include href="/srv/xsltproc.tenandtwo.com/htdocs/wp-content/plugins/tenandtwo-xslt-processor/xsl/util.xsl"/> <xsl:template match="/"> <xsl:call-template name="util-print-node-names"/> </xsl:template> </xsl:stylesheet>
XML in shortcode body
Here the xml
attribute is replaced with data defined directly in the body of the shortcode.
[xslt_transform_xml]
<data><node id="1">one</node><node id="2">two</node></data>
[/xslt_transform_xml]
Output:
<data> <node id="1">one</node> <node id="2">two</node> </data>
XML from [xslt_select_xml/]
And finally, here the XML is defined in the body of the shortcode, but indirectly, using [xslt_select_xml/]
to dynamically fetch and filter the input for [xslt_transform_xml/]
.
[xslt_transform_xml]
[xslt_select_xml xml="sample.xml" select="//list" /]
[/xslt_transform_xml]
Output:
<RESULT xml="/srv/plugins.tenandtwo.com/htdocs/wp-content/plugins/tenandtwo-xslt-processor/xsl/sample.xml" select="//list"> <list title="My List"> <item value="1">One</item> <item value="2">Two</item> <item value="3">Three</item> <item value="4">Four</item> </list> </RESULT>