File Functions
Overview
This article explains how to implement file functions in your mapping files when running them in Zynk Workflow.
- Namespace urn:FileFunctions
- Available since Zynk Workflow 2.1.4
XSLT
Declaration
To use file functions, you need to add the namespace declaration to your XSLT file:
<xsl:stylesheet
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:FileFunctions="urn:FileFunctions"
exclude-result-prefixes="FileFunctions">
<!-- transformation -->
</xsl:stylesheet>
Functions
Below is a list of available functions within the urn:FileFunctions
namespace:
- FileExists Checks if a file exists in the given file path.
string file
- ValidXml Checks if the file contains valid XML data.
string file
Example
Input
<?xml version="1.0" encoding="utf-8"?>
<Company>
<Products>
<Product>
<Sku>ABC</Sku>
<Supplier>Bob</Supplier>
<Priority>2</Priority>
</Product>
<Product>
<Sku>ZXY</Sku>
<Supplier>Sam</Supplier>
<Priority>1</Priority>
</Product>
</Products>
</Company>
XSLT
<xsl:stylesheet
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:FileFunctions="urn:FileFunctions"
exclude-result-prefixes="FileFunctions">
<xsl:output indent="yes"/>
<xsl:param name="InventoryFile">C:\Users\joe.harrison\Downloads\inventory.xml</xsl:param>
<xsl:template match="/">
<StockInfo>
<xsl:choose>
<xsl:when test="FileFunctions:ValidXml($InventoryFile)">
<xsl:for-each select="Company/Products/Product">
<xsl:variable name="SKU" select="Sku"/>
<xsl:variable name="Inventory" select="document($InventoryFile)/InventoryInfo/Inventory[Sku=$SKU]"/>
<Stock>
<xsl:copy-of select="Sku"/>
<Qty><xsl:value-of select="$Inventory/QtyInStock - $Inventory/QtyAllocated"/></Qty>
</Stock>
</xsl:for-each>
</xsl:when>
<xsl:otherwise>
<xsl:variable name="SingleQuote">'</xsl:variable>
<xsl:comment><xsl:value-of select="concat('The file, ', $SingleQuote, $InventoryFile, $SingleQuote, ', is not valid XML.')"/></xsl:comment>
</xsl:otherwise>
</xsl:choose>
</StockInfo>
</xsl:template>
</xsl:stylesheet>
Output
<?xml version="1.0" encoding="utf-8"?>
<StockInfo>
<!--The file, 'C:\Users\joe.harrison\Downloads\inventory.xml', is not valid XML.-->
</StockInfo>