I have a situation where the param won't be set, but my attempts to capture & handle the sitation aren't working.
I'm using the following:
<xsl:template match="xs:complexType">
<xsl:param name="prefix" />
<xsl:variable name="prefix-no-core">
<xsl:choose>
<!-- if no value, default to 'AcRec' -->
<xsl:when test="not($prefix)">
<xsl:value-of select="'AcRec'" />
</xsl:when>
<!-- if 'core', leave as empty string -->
<xsl:when test="$prefix = 'core'">
</xsl:when>
<!-- if 'AcRec', set the value -->
<xsl:when test="$prefix = 'AcRec'">
<xsl:value-of select="$prefix" />
</xsl:when>
</xsl:choose>
</xsl:variable>
<xs:complexType name="{concat($prefix-no-core, @name)}">
...
</xsl:template>
I've also tried $prefix='' in the first test - neither work. but if I use:
<xsl:value-of select="not($prefix)" />
... the value prints out as true. But using that in my xsl:choose doesn't produce any output.
To use an XSLT parameterCreate an XsltArgumentList object and add the parameter using the AddParam method. Call the parameter from the style sheet. Pass the XsltArgumentList object to the Transform method.
The content model of both elements is the same. The way these elements declare variables is the same. However, the value of the variable declared using <xsl:param> is only a default that can be changed with the <xsl:with-param> element, while the <xsl:variable> value cannot be changed.
The match attribute is used to associate a template with an XML element. The match attribute can also be used to define a template for the entire XML document. The value of the match attribute is an XPath expression (i.e. match="/" defines the whole document).
The mode attribute allows multiple ways of processing the same XML elements. A template must have a match attribute if wanting to use a mode attribute, so they are not meant for templates relying solely upon the name attribute for calling.
You can specify a default value for the parameter, that way when it isn't passed, you can check for it.
Though in your example, it looks like just having a default value would simplify things greatly.
<xsl:template match="xs:complexType">
<xsl:param name="prefix" select="'default-value'" /> <!-- SET default -->
<xsl:variable name="prefix-no-core">
<xsl:choose>
<!-- if no value, default to 'AcRec' -->
<xsl:when test="$prefix = 'default-value'">
<xsl:value-of select="'AcRec'" />
</xsl:when>
<!-- if 'core', leave as empty string -->
<xsl:when test="$prefix = 'core'">
</xsl:when>
<!-- if 'AcRec', set the value -->
<xsl:when test="$prefix = 'AcRec'">
<xsl:value-of select="$prefix" />
</xsl:when>
</xsl:choose>
</xsl:variable>
<xs:complexType name="{concat($prefix-no-core, @name)}">
...
</xsl:template>
Call like this, and the value will be 'default-value':
<xsl:apply-templates select="//xs:complexType[@name='AddressType']" />
Call like this, and the value will be 'passed-value':
<xsl:apply-templates select="//xs:complexType[@name='AddressType']">
<xsl:with-param name="prefix" value="'passed-value'"/>
</xsl:apply-templates>
EDIT: For completeness, the simplified form of the original example is:
<xsl:template match="xs:complexType">
<xsl:param name="prefix" select="'AcRec'"/>
<xsl:variable name="prefix-no-core">
<xsl:choose>
<!-- if 'core', leave as empty string -->
<xsl:when test="$prefix = 'core'">
</xsl:when>
<!-- defaults to 'AcRec' -->
<xsl:otherwise>
<xsl:value-of select="$prefix" />
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xs:complexType name="{concat($prefix-no-core, @name)}">
...
</xsl:template>
It's a hack, but I have had success by first wrapping the parameter with the normalize-space() function before testing for an empty param.
<xsl:value-of select="not(normalize-space($prefix))" />
Note: replaced old answer, check history if you want it.
The following input:
<test xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:complexType name="something"/>
<xs:complexType name="somethingElse"/>
</test>
Fed to the following XSLT:
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" version="2.0">
<xsl:template match="/">
<xsl:for-each select="node()">
<xsl:apply-templates/>
</xsl:for-each>
</xsl:template>
<xsl:template match="xs:complexType">
<xsl:param name="prefix" />
<xsl:variable name="prefix-no-core">
<xsl:choose>
<xsl:when test="not($prefix)">AcRec</xsl:when>
<xsl:when test="$prefix = 'core'"/>
<xsl:when test="$prefix = 'AcRec'">AcRec</xsl:when>
</xsl:choose>
</xsl:variable>
<xs:complexType name="{concat($prefix-no-core, @name)}"/>
</xsl:template>
</xsl:transform>
Gives the following result:
<xs:complexType name="AcRecsomething" xmlns:xs="http://www.w3.org/2001/XMLSchema"/>
<xs:complexType name="AcRecsomethingElse" xmlns:xs="http://www.w3.org/2001/XMLSchema"/>
I'm not sure what more you're looking for...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With