什么';XSLT/XPath if/then/else的正确语法是什么?
使用XSLT和XSLT中的XPath的if/then/else的正确语法是什么 我有以下XSLT,我想将什么';XSLT/XPath if/then/else的正确语法是什么?,xslt,xpath,Xslt,Xpath,使用XSLT和XSLT中的XPath的if/then/else的正确语法是什么 我有以下XSLT,我想将updateType字段的值设置为update或insert,具体取决于PhoneID是否存在值。如果存在PhoneID,则需要将updateType设置为update,如果不存在,则需要将其设置为insert 这就是我到目前为止所做的: <xsl:for-each select="//ContactPhones"> <xsl:if test="flt
updateType
字段的值设置为update
或insert
,具体取决于PhoneID
是否存在值。如果存在PhoneID
,则需要将updateType
设置为update
,如果不存在,则需要将其设置为insert
这就是我到目前为止所做的:
<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>
更新
插入
如何执行此操作-xml输入如下所示:
<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>
101000047723
01923 2531345
电话
家
真的
我认为以下可能是一个解决方案
<Result field="updateType">
<Value>
<xsl:choose>
<xsl:when test="ContactPhones[flt:Preferred='true']">update</xsl:when>
<xsl:otherwise>insert</xsl:otherwise>
</xsl:choose>
</Value>
更新
插入
既然您询问的是XQuery,我想您可以使用XPath 2.0。使用XPath2.0,您只需
<Value>
<xsl:value-of select="if (PhoneID) then 'insert' else 'update'"/>
</Value>
不要使用过于冗长的xsl:choose(如果您只能使用XSLT和XPath 1.0,您将使用它)。既然您在询问XQuery,我假设您可以使用XPath 2.0。使用XPath2.0,您只需
<Value>
<xsl:value-of select="if (PhoneID) then 'insert' else 'update'"/>
</Value>
选择
而不是冗长的xsl:choose
(如果只能使用XSLT和XPath 1.0,您将使用它)。假设PhoneID
元素始终存在于输入ContactPhones
中,但可以为空,您将使用:
XSLT1.0
<Result field="updateType">
<Value>
<xsl:choose>
<xsl:when test="string(PhoneID)">update</xsl:when>
<xsl:otherwise>insert</xsl:otherwise>
</xsl:choose>
</Value>
</Result>
<Result field="updateType">
<Value>
<xsl:value-of select="if (string(PhoneID)) then 'update' else 'insert'" />
</Value>
</Result>
<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>
更新
插入
XSLT2.0
<Result field="updateType">
<Value>
<xsl:choose>
<xsl:when test="string(PhoneID)">update</xsl:when>
<xsl:otherwise>insert</xsl:otherwise>
</xsl:choose>
</Value>
</Result>
<Result field="updateType">
<Value>
<xsl:value-of select="if (string(PhoneID)) then 'update' else 'insert'" />
</Value>
</Result>
<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>
否则,前面给出的另外两个答案就可以了
--
注意:
而不是:
<xsl:for-each select="//ContactPhones">
<xsl:if test="flt:Preferred/text()='true'">
考虑:
<xsl:template match="ContactPhones[flt:Preferred='true']">
例子: XML输入
<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>
<?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>
123
01923 2531345
01923 2531345
01923 2531345
XSLT1.0
<Result field="updateType">
<Value>
<xsl:choose>
<xsl:when test="string(PhoneID)">update</xsl:when>
<xsl:otherwise>insert</xsl:otherwise>
</xsl:choose>
</Value>
</Result>
<Result field="updateType">
<Value>
<xsl:value-of select="if (string(PhoneID)) then 'update' else 'insert'" />
</Value>
</Result>
<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>
更新
插入
结果
<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>
<?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>
更新
插入
插入
如您所见,这两种情况的结果都是“插入”:
为空李>PhoneID
- 缺少
PhoneID
PhoneID
元素始终存在于输入ContactPhones
中,但可以为空,您可以使用:
XSLT1.0
<Result field="updateType">
<Value>
<xsl:choose>
<xsl:when test="string(PhoneID)">update</xsl:when>
<xsl:otherwise>insert</xsl:otherwise>
</xsl:choose>
</Value>
</Result>
<Result field="updateType">
<Value>
<xsl:value-of select="if (string(PhoneID)) then 'update' else 'insert'" />
</Value>
</Result>
<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>
更新
插入
XSLT2.0
<Result field="updateType">
<Value>
<xsl:choose>
<xsl:when test="string(PhoneID)">update</xsl:when>
<xsl:otherwise>insert</xsl:otherwise>
</xsl:choose>
</Value>
</Result>
<Result field="updateType">
<Value>
<xsl:value-of select="if (string(PhoneID)) then 'update' else 'insert'" />
</Value>
</Result>
<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>
否则,前面给出的另外两个答案就可以了
--
注意:
而不是:
<xsl:for-each select="//ContactPhones">
<xsl:if test="flt:Preferred/text()='true'">
考虑:
<xsl:template match="ContactPhones[flt:Preferred='true']">
例子: XML输入
<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>
<?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>
123
01923 2531345
01923 2531345
01923 2531345
XSLT1.0
<Result field="updateType">
<Value>
<xsl:choose>
<xsl:when test="string(PhoneID)">update</xsl:when>
<xsl:otherwise>insert</xsl:otherwise>
</xsl:choose>
</Value>
</Result>
<Result field="updateType">
<Value>
<xsl:value-of select="if (string(PhoneID)) then 'update' else 'insert'" />
</Value>
</Result>
<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>
更新
插入
结果
<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>
<?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>
更新
插入
插入
如您所见,这两种情况的结果都是“插入”:
为空李>PhoneID
- 缺少
PhoneID