XSLT: If tag exists, apply template; if not, choose static value XSLT: If tag exists, apply template; if not, choose static value xml xml

XSLT: If tag exists, apply template; if not, choose static value


In the template for the parent element the country element is expected to be in use e.g.

<xsl:template match="foo">  <xsl:if test="not(country)">    <country>US</country>  </xsl:if>  <xsl:apply-templates/></xsl:template>

Instead of foo use the name of the parent element. And of course you could also do other stuff like copying the element, I have focused on the if check. You do not really need an xsl:choose/when/otherwise in my view, the xsl:if should suffice as the apply-templates will not do anything with child elements that don't exist.


Even simpler:

<xsl:template match="foo[not(country)]">        <country>US</country>    <xsl:apply-templates/></xsl:template>

Do note:

No XSLT conditional instructions (such as <xsl:if>) are used and they are not necessary.

Very often, the presence of <xsl:if> or <xsl:choose> is an indication that the code can be refactored and significantly improved by, among other things, getting rid of the conditional instructions.


You don't even need any kind of Conditional Processing. This stylesheet:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">    <xsl:template match="node()|@*">        <xsl:copy>            <xsl:apply-templates select="node()|@*"/>        </xsl:copy>    </xsl:template>    <xsl:template match="item[not(country)]">        <xsl:copy>            <xsl:apply-templates select="node()|@*"/>            <country>Lilliput</country>        </xsl:copy>    </xsl:template></xsl:stylesheet>

With this input:

<root>    <item>        <country>Brobdingnag</country>    </item>    <item>        <test/>    </item></root>

Output:

<root>    <item>        <country>Brobdingnag</country>    </item>    <item>        <test></test>        <country>Lilliput</country>    </item></root>