This is a simplified example of using the [xslt_transform_xml/]
shortcode. The sample.xml
file contains some data, and the sample.xsl
file contains templates for reading that data and writing the final output.
We can specify just the file names in the shortcode attributes here, without additional path information, because the parent directory for both files is listed in the plugin’s “Local File Search Path”. That’s configured in Settings >> XSLT Processor
.
Also note the final parameter in the shortcode: hello_name="World"
. This is not an attribute that the plugin uses, however the name and value are passed along to the stylesheet for use there. For example, if myparam="myvalue"
were added to the shortcode, that value would be available within XSL using <xsl:param name="myparam">my default value</xsl:param>
. In this sample, the default value of hello_name
is “Nobody” and the shortcode attribute changes it to “World”.
The sample.xsl
file is included below <!-- with stylesheet comments -->
just after after the detail for sample.xml
.
shortcode
[xslt_transform_xml xsl="sample.xsl" xml="sample.xml" hello_name="World" /]
Output:
Hello World
Comments:
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.
Lists:
List: My List
- One (1)
- Two (2)
- Three (3)
- Four (4)
Dates:
Friday, December 02 2022, 22:12:12 Friday, March 03 2023, 23:13:13
XML file – “sample.xml”
<?xml version='1.0' encoding='utf-8'?>
<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>
XSL file – “sample.xsl”
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<!-- set output method="xml|html|text" -->
<xsl:output method="html" version="1.0" encoding="UTF-8" indent="yes" />
<!-- include templates from other files -->
<xsl:include href="../../../plugins/tenandtwo-xslt-processor/xsl/date.xsl" />
<!-- catch values passed in shortcode attributes -->
<xsl:param name="hello_name">Nobody</xsl:param>
<!-- main -->
<xsl:template match="/">
<h3>Hello <xsl:value-of select="$hello_name" /></h3>
<!-- select and process nodes with 'for-each' -->
<p>Comments:
<ul>
<xsl:for-each select="//comment">
<li><pre><xsl:value-of select="." /></pre></li>
</xsl:for-each>
</ul>
</p>
<!-- select and process nodes with 'apply-templates' -->
<p>Lists: <xsl:apply-templates select="//list" /></p>
<p>Dates: <ul><xsl:apply-templates select="//date" /></ul></p>
</xsl:template>
<!-- list nodes -->
<xsl:template match="list">
<p>List: <xsl:value-of select="./@title" />
<ul>
<xsl:apply-templates select="item" />
</ul>
</p>
</xsl:template>
<!-- list/item nodes -->
<xsl:template match="item">
<li>
<xsl:value-of select="." />
<xsl:value-of select="concat(' (', @value, ')')" />
</li>
</xsl:template>
<!-- date nodes : use template from date.xsl to format (and shift) the value -->
<xsl:template match="date">
<li>
<xsl:call-template name="date-format">
<xsl:with-param name="value"><xsl:value-of select="." /></xsl:with-param>
<xsl:with-param name="format">l, F d Y, H:i:s</xsl:with-param>
<xsl:with-param name="shift">+10 hours</xsl:with-param>
</xsl:call-template>
</li>
</xsl:template>
</xsl:stylesheet>
<!-- end sample.xsl -->