When working with XSLT for data transformations, especially in enterprise integration scenarios such as Azure Logic Apps, BizTalk Server, or SAP PI/PO, it is common to need conditional mappings. One of the most frequent tasks is checking whether an XML element exists and whether it’s not empty before performing a transformation. Doing this correctly ensures your mappings are robust, your outputs are clean, and your processes don’t break due to missing or invalid data.
I actually had this exact requirement this week on a BizTalk Server mapping, and for this exercise, we will be using the element s5:AUTORE_COG.
📝 One-Minute Brief
This blog post explores practical XSLT techniques for checking whether an XML element exists and contains valid content. These tips are essential for ensuring reliable data transformations in integration scenarios like Logic Apps, BizTalk Server, or SAP PI/PO.
Here’s what’s covered:
- The difference between element existence and non-empty content
- The common expressions used this type of conditional checks and their main differences
- What should we be using and when
Why does this matter?
In many real-world integration projects, XML inputs are not always guaranteed to follow the expected structure. Fields may be optional, empty, or missing altogether. Without proper checks, your transformation logic can produce incorrect results or even fail. That’s why mastering conditional logic in XSLT is essential for any integration developer.
The Basics: Existence vs. Content
Before diving into tips, it’s important to distinguish:
- Element exists → The element is present in the XML (even if it’s empty).
- Element is not empty → The element exists and contains non-whitespace text.
You often need both conditions to be true before mapping the value.
The common expressions used
There are many ways to do this validation, and for sure, all of them have pros and cons associated, but in most cases, I have found that two approaches/expressions have been frequently used:
<xsl:if test="s5:AUTORE_COG">
and
<xsl:if test="s5:AUTORE_COG/text()">
In XSLT, both of your expressions technically check for the existence of the s5:AUTORE_COG
element, but they behave slightly differently depending on what exactly you want to test:
- The first expression: <xsl:if test=”s5:AUTORE_COG”> – Checks if the element exists
- What it does: Checks if the
s5:AUTORE_COG
element exists in the XML. - It doesn’t care if the element is empty or not.
- What it does: Checks if the
Therefore, we can use this expression if we only need to check if the element exists.
- The second expression:
<xsl:if test="s5:AUTORE_COG/text()">
– Checks if the element exists and is not empty (contains text – a single space is permitted)- What it does: Checks if the
s5:AUTORE_COG
element exists and contains some non-empty text. - An empty element like
<s5:AUTORE_COG/>
or<s5:AUTORE_COG></s5:AUTORE_COG>
would not pass this test.
- What it does: Checks if the
Therefore, we can use this expression if we want to check if the element contains text (non-empty):
Summary:
Expression | Checks for | Example Match | Empty Element Match |
---|---|---|---|
s5:AUTORE_COG | Element existence | ✅ <s5:AUTORE_COG>abc</s5:AUTORE_COG> | ✅ <s5:AUTORE_COG/> |
s5:AUTORE_COG/text() | Element contains text | ✅ <s5:AUTORE_COG>abc</s5:AUTORE_COG> | ❌ <s5:AUTORE_COG/> |
Now, both expressions have a problem or a limitation. They don’t check if the element just contains whitespaces (another way we developers or systems can interpret empty elements).
Another way to validate is to use string()
to Check for Non-Empty Content like:
<xsl:if test="string(s5:AUTORE_COG)">
<!-- This block runs only if the element exists and is not empty -->
...
</xsl:if>
However, it has the same limitations as the previous expressions. Using string()
does consider the element <s5:AUTORE_COG> </s5:AUTORE_COG>
to be non-empty, because string()
it retrieves the text content as-is, including whitespace.
- The element contains whitespace characters and
string()
will return those. - In XSLT, non-empty strings (even if only whitespace) are treated as
true()
in a boolean context.
Tip and Trick: The Solution
When working with XML inputs, it’s common to encounter elements that technically “exist” but contain only spaces, tabs, or newline characters. Using string()
alone won’t help here — whitespace is still considered valid content. That’s where normalize-space()
it becomes essential.

normalize-space()
is a built-in XSLT 1.0/XPath function that cleans up a string by:
- Trimming leading and trailing whitespace.
- Replacing sequences of whitespace characters (spaces, tabs, newlines) with a single space.
- Returning the cleaned-up string.
Practical example:
- If s5:AUTORE_COG is:
<s5:AUTORE_COG> Hello World </s5:AUTORE_COG>
- The output will be: “Hello World“
Therefore, by using the following expression:
<xsl:if test="normalize-space(s5:AUTORE_COG)">
<!-- This condition check if the element exists and if is not empty or full of spaces -->
</xsl:if>
We can actually validate all the conditions:
- If the element exists – is present in the XML
- And if it is not empty or contains only spaces.
Recommendations
- Use
<xsl:if test="normalize-space(s5:AUTORE_COG)">
if you want to check that it exists and is not just whitespace. - Use
<xsl:if test="s5:AUTORE_COG">
if you’re only checking for presence.
Hope you find this helpful! If you liked the content or found it useful and want to help me write more, consider buying (or helping me buy) a Star Wars Lego set for my son.