The following will create an XPATH sample as the value of the "generated XPATH" variable when run with a sample XML file:
<xsl:variable name="generatedXPATH"> <xsl:for-each select="/criteria/criterion"> <xsl:text>//</xsl:text> <xsl:value-of select="." /> <xsl:if test="position()!=last()"> <xsl:text> | </xsl:text> </xsl:if> </xsl:for-each> </xsl:variable>
You can use this variable in the stylesheet, which creates a stylesheet to build the values โโof the @match template as follows:
<?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xslt="http://www.w3.org/1999/XSL/TransformAlias" version="1.0"> <xsl:namespace-alias stylesheet-prefix="xslt" result-prefix="xsl"/> <xsl:output indent="yes" /> <xsl:template match="/"> <xsl:variable name="generatedXPATH"> <xsl:for-each select="/criteria/criterion"> <xsl:text>//</xsl:text> <xsl:value-of select="." /> <xsl:if test="position()!=last()"> <xsl:text> | </xsl:text> </xsl:if> </xsl:for-each> </xsl:variable> <xslt:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xslt:template> <xsl:attribute name="match"> <xsl:value-of select="$generatedXPATH" /> </xsl:attribute> <xsl:comment>Do something special, like select the value of the matched elements</xsl:comment> <xslt:value-of select="." /> </xslt:template> </xslt:stylesheet> </xsl:template> </xsl:stylesheet>
When executed against a sample, XML creates this stylesheet:
<?xml version="1.0" encoding="UTF-16"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="//AAA | //BBB | //CCC"> <xsl:value-of select="." /> </xsl:template> </xsl:stylesheet>
source share