‘xslt_select_xml’ is a helper function. It reads XML and returns a subset of the data, based on a supplied XPATH expression. Used to pre-filter data passed to ‘xslt_transform_xml’, ‘xslt_select_xml’ allows for more generic, reuseable stylesheets.
[xslt_select_xml/]
is a helper function. It reads XML and returns a subset of the data, based on a supplied XPATH expression. Used to pre-filter data passed to [xslt_transform_xml/]
, [xslt_select_xml/]
allows for more generic, reuseable stylesheets.
There are four (4) options for specifying the XML source using the xml
attribute:
local file: | xml="/path/to/local/data.xml" |
remote file: | xml="http://abc.de/remote/data.xml" |
item id: | xsl="2358" |
item name: | xml="wp-data-xml" |
There are two (2) options for specifying the XPath expression: the select
attribute, or the body of the shortcode. Complex select statements with quotes, square brackets or other special syntax, should use the second pattern:
[xslt_select_xml xml="{file|url|id|slug}" select="{XPath}" /]
[xslt_select_xml xml="{file|url|id|slug}"]{XPath}[/xslt_select_xml]
Of course, either pattern may be used in combination with [xslt_transform_xml/]
.
[xslt_transform_xml][xslt_select_xml/][/xslt_transform_xml]
Normally, [xslt_select_xml/]
wraps its result in a single <RESULT/>
node. This ensures the output can be handled as a valid XML document, where a single enclosing node is required. You can change the name of this root node, or suppress it altogether, with the root
attribute.
[xslt_select_xml root="{newname}" /]
[xslt_select_xml root="" /]
Cache parameters
When the shortcode specifies a remote file – xml="{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 selections can override the default with the attribute cache="{minutes}"
, or specify cache="0"
to bypass caching.
Namespace parameters
The XSLT Processor plugin provides two (2) methods for handling XML Namespaces within [xslt_select_xml/]
.
The first method is a cheat, really, and generally not recommended. Nonetheless, you can simply remove all namespace information from the input XML by adding strip-namespaces
or strip-namespaces="yes"
to the shortcode. Afterward, you can ignore the namespaces in your XPath expression – select="//nodename"
.
The second, recommended method is to add the required namespace information to [xslt_select_xml/]
. Step One: Add an xmlns
attribute to the shortcode containing one or more space-delimited namespace prefixes. Step Two: For each prefix, create a matching shortcode attribute containing the full namespace URI. Step Three: use the namespace prefixes in your XPath expression – select="//key:nodename"
.
Here are the usage patterns for 1) removing namespaces, 2) adding one namespace, and 3) adding multiple namespaces.
[xslt_select_xml xml="{file}" strip-namespaces select="//node" /]
[xslt_select_xml xml="{file}" xmlns="ns1" ns1="{namespace-uri-1}" select="//ns1:node" /]
[xslt_select_xml xml="{file}" xmlns="ns1 ns2" ns1="{namespace-uri-1}" ns2="{namespace-uri-2}" select="//ns1:node/ns2:node" /]
You can read more, and see working examples in How To >> 102 – XML Namespaces.
Default data and selection
The default XML –<NODATA/>
– is a single node. The default XPath –select="/"
– returns everything in the document.
[xslt_select_xml /]
Output:
<RESULT select="/"> <NODATA/> </RESULT>
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 your Settings >> XSLT Processor >> Local File Search Path
.
[xslt_select_xml xml="sample.xml" /]
Output:
<RESULT xml="/srv/plugins.tenandtwo.com/htdocs/wp-content/plugins/tenandtwo-xslt-processor/xsl/sample.xml" select="/"> <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> </RESULT>
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_select_xml xml="https://xsltproc.tenandtwo.com/wp-content/uploads/upload_sample.xml" select="//comment" cache="10" /]
Output:
ERROR : DomDocument->load(xml): <br/> ----------------------------------------<br/> Only HTTP(S) requests are handled.<br/> -^<br/> Fatal Error 4: Start tag expected, '<' not found<br/> Line: 1<br/> Column: 1<br/> ----------------------------------------<br/>
XML in wp_content
You may also pull data from WordPress items by specifying an 'id' or a 'slug' value - xml="12345"
or xml="my-item-slug"
. If the custom post types are active, 'slug' values will only match XML Documents
. If inactive, both 'id' and 'slug' values will match any WordPress post type ('post', 'page', etc).
[xslt_select_xml xml="sample-xml" root="WPXML" /]
Output:
<WPXML xml="sample-xml" id="876" select="/"> <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> </WPXML>
Select in shortcode body
Complex XPath expressions with quotes, brackets or other special syntax should be specified in the body of the shortcode, rather than in the select
attribute. Here, we're only selecting <item>
nodes with value
attributes greater than 2
.
[xslt_select_xml xml="sample.xml"]//list/item[@value > 2][/xslt_select_xml]
Output:
<RESULT xml="/srv/plugins.tenandtwo.com/htdocs/wp-content/plugins/tenandtwo-xslt-processor/xsl/sample.xml" select="//list/item[@value > 2]"> <item value="3">Three</item> <item value="4">Four</item> </RESULT>
Output JSON
This is a specialized use case for [xslt_select_xml/]
. For details and a full example, see How To >> JSON.
[xslt_select_xml xml="sample.xml" format="json"]//date[/xslt_select_xml]
Output:
{"RESULT":[{"attributes":{"xml":"\/srv\/plugins.tenandtwo.com\/htdocs\/wp-content\/plugins\/tenandtwo-xslt-processor\/xsl\/sample.xml","select":"\/\/date"},"date":[{"cdata":"2022-12-02 12:12:12"},{"cdata":"2023-03-03 13:13:13"}]}]}
Invalid URL
[xslt_select_xml xml="http://badurl" /]
Output:
ERROR : DomDocument->load(xml):
----------------------------------------
A valid URL was not provided.
-^
Fatal Error 4: Start tag expected, '<' not found
Line: 1
Column: 1
----------------------------------------