I have an XML flow where I need to insert content based on the whether an element contains data or if it is empty.
I've tried several techniques but it's still not working.
Here is my XSLT:
<?xml version="1.0" encoding="UTF-8"?><!-- DWXMLSource="pricesample.xml" -->
<!DOCTYPE xsl:stylesheet>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" encoding="UTF-8"/>
<xsl:template match="catalog">
<catalog>
<xsl:for-each select="shoe">
<shoe>
<xsl:value-of select="name"/><xsl:text> </xsl:text>
<xsl:apply-templates select="price" />
</shoe>
</xsl:for-each>
</catalog>
</xsl:template>
<xsl:template match="price">
<xsl:choose>
<xsl:when test=". =''">
<price><xsl:text>Price is Empty</xsl:text></price><xsl:text>
</xsl:text>
</xsl:when>
<xsl:otherwise>
<price><xsl:text> $</xsl:text><xsl:value-of select="."/></price><xsl:text>
</xsl:text>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
Here is the XML:
<?xml version="1.0" encoding="UTF-8"?>
<catalog>
<shoe>
<name>Shoe 1</name>
<price>49.98</price>
</shoe>
<shoe>
<name>Shoe 2</name>
<price>65.5</price>
</shoe>
<shoe>
<name>Shoe 3</name>
<price>70</price>
</shoe>
<shoe>
<name>Shoe 4</name>
<price/>
</shoe>
<shoe>
<name>Shoe 5</name>
<price/>
</shoe>
</catalog>
So the output should look like this:
<?xml version="1.0" encoding="UTF-8"?>
<catalog>
<shoe>
<name>Shoe 1</name>
<price>$49.98</price>
</shoe>
<shoe>
<name>Shoe 2</name>
<price>$65.5</price>
</shoe>
<shoe>
<name>Shoe 3</name>
<price>$70</price>
</shoe>
<shoe>
<name>Shoe 4</name>
<price>Price is Empty</price>
</shoe>
<shoe>
<name>Shoe 5</name>
<price>Price is Empty</price>
</shoe>
</catalog>
I have tried several tests, including:
test=". =''"
test="not(price)"
test="not(string(.))"
None of them seem to work for me.