XSLT/XPath if/then/else 的正确语法是什么?



在XSLT中使用XSLT和XPath的if/then/else的正确语法是什么?

我有以下 XSLT,我想将updateType字段的值设置为 updateinsert,具体取决于是否存在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>

我认为以下可能是一个解决方案

  <Result field="updateType">
<Value>
     <xsl:choose>
        <xsl:when test="ContactPhones[flt:Preferred='true']">update</xsl:when>
        <xsl:otherwise>insert</xsl:otherwise>
     </xsl:choose>
</Value>

假设PhoneID元素始终存在于输入ContactPhones中,但可以为空,则可以使用:

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>

否则,前面给出的另外两个答案就可以了。

--

而不是:

<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>

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>

结果

<?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不见了。

既然你问的是XQuery,我假设你可以使用XPath 2.0。使用 XPath 2.0,您可以简单地做到

<Value>
    <xsl:value-of select="if (PhoneID) then 'insert' else 'update'"/>
</Value>

而不是相当冗长的xsl:choose(如果您只能使用 XSLT 和 XPath 1.0,您将使用

这种方式)。

最新更新