Reputation: 10068
what is the correct syntax for if/then/else using XSLT and also XPath within XSLT?
I have the following XSLT and I want to set the value of the updateType
field to either update
or insert
depending on if a value exists for PhoneID
. If PhoneID
exists then updateType
needs to be set to update
, and if not it needs to be set to insert
.
This is what I have so far:
<xsl:for-each select="//ContactPhones">
<xsl:if test="flt:Preferred/text()='true'">
<Result field="txtPhoneID">
<Value>
<xsl:value-of select="PhoneID"/>
</Value>
</Result>
</xsl:if>
</xsl:for-each>
<Result field="updateType">
<Value>
<xsl:choose>
<xsl:when test="string(txtPhoneID)">update</xsl:when>
<xsl:otherwise>insert</xsl:otherwise>
</xsl:choose>
</Value>
</Result>
How can I do this - xml input as follows:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Body>
<FWTIndividual xmlns="http://www.test.com/wsdl/FLTypes">
<ContactPhones xmlns="">
<PhoneID>101000047723</PhoneID>
<Number>01923 2531345</Number>
<DeviceType>telephone</DeviceType>
<ns1:Usage xmlns:ns1="http://www.test.com/wsdl/FLTypes">home</ns1:Usage>
<ns2:Preferred xmlns:ns2="http://www.test.com/wsdl/FLTypes">true</ns2:Preferred>
</ContactPhones>
</FWTIndividual>
I think the following might be a solution
<Result field="updateType">
<Value>
<xsl:choose>
<xsl:when test="ContactPhones[flt:Preferred='true']">update</xsl:when>
<xsl:otherwise>insert</xsl:otherwise>
</xsl:choose>
</Value>
Upvotes: 0
Views: 853
Reputation: 116992
Assuming the PhoneID
element is always present in the input ContactPhones
but can be empty, you would use:
XSLT 1.0
<Result field="updateType">
<Value>
<xsl:choose>
<xsl:when test="string(PhoneID)">update</xsl:when>
<xsl:otherwise>insert</xsl:otherwise>
</xsl:choose>
</Value>
</Result>
XSLT 2.0
<Result field="updateType">
<Value>
<xsl:value-of select="if (string(PhoneID)) then 'update' else 'insert'" />
</Value>
</Result>
Otherwise the two other answers given previously will do.
--
Note:
Instead of:
<xsl:for-each select="//ContactPhones">
<xsl:if test="flt:Preferred/text()='true'">
consider:
<xsl:template match="ContactPhones[flt:Preferred='true']">
XML input
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Body>
<FWTIndividual xmlns="http://www.test.com/wsdl/FLTypes">
<ContactPhones xmlns="">
<PhoneID>123</PhoneID>
<Number>01923 2531345</Number>
</ContactPhones>
<ContactPhones xmlns="">
<PhoneID/>
<Number>01923 2531345</Number>
</ContactPhones>
<ContactPhones xmlns="">
<Number>01923 2531345</Number>
</ContactPhones>
</FWTIndividual>
</soapenv:Body>
</soapenv:Envelope>
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/">
<root>
<xsl:for-each select="//ContactPhones">
<Result field="updateType">
<Value>
<xsl:choose>
<xsl:when test="string(PhoneID)">update</xsl:when>
<xsl:otherwise>insert</xsl:otherwise>
</xsl:choose>
</Value>
</Result>
</xsl:for-each>
</root>
</xsl:template>
</xsl:stylesheet>
Result
<?xml version="1.0" encoding="UTF-8"?>
<root>
<Result field="updateType">
<Value>update</Value>
</Result>
<Result field="updateType">
<Value>insert</Value>
</Result>
<Result field="updateType">
<Value>insert</Value>
</Result>
</root>
As you can see, the result is 'insert' for both these cases:
PhoneID
is empty;PhoneID
is missing.Upvotes: 3
Reputation: 22617
Since you are asking about XQuery, I assume you can use XPath 2.0. With XPath 2.0, you could simply do
<Value>
<xsl:value-of select="if (PhoneID) then 'insert' else 'update'"/>
</Value>
instead of the rather verbose xsl:choose
(which is what you would use if you can only use XSLT and XPath 1.0).
Upvotes: 2