Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
什么';XSLT/XPath if/then/else的正确语法是什么?_Xslt_Xpath - Fatal编程技术网

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

什么';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

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

我有以下XSLT,我想将
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

您可能正在寻找上述上下文中的语法是什么?您可能正在寻找上述上下文中的语法是什么?如果PhoneID元素不总是存在怎么办?我尝试过xslt 1.0解决方案,当我有一个PhoneID时,我得到了期望的结果,但是当PhoneID为空时,“否则”不起作用,所以我假设PhoneID元素并不总是存在,“如果PhoneID元素不总是存在怎么办?”它仍然会起作用。如果您有问题,请编辑您的问题并显示为小标题。但你们的意见有代表性的例子。我更新了原来的帖子。看起来我需要在for each循环之外进行测试,因为如果没有//ContactPhone,那么元素将不存在。那么如何检查txtPhoneID字段中是否有值?看起来我可能需要使用一个变量。如果我在for each之外声明了一个变量,那么将phoneid放入for each内的变量中,然后在for each.xml输入之外检查变量的语法是什么。因此,如果contactphones不存在,我仍然需要返回带有插入值的updateType,这会使帮助您变得非常困难。我已经纠正并最小化了您的输入示例—这是您应该自己做的。我希望我在答案中添加的示例能让您明白这一点。如果PhoneID元素不总是存在,该怎么办?我尝试过xslt 1.0解决方案,当我有一个PhoneID时,我得到了期望的结果,但是当PhoneID为空时,“否则”不起作用,所以我假设PhoneID元素并不总是存在,“如果PhoneID元素不总是存在怎么办?”它仍然会起作用。如果您有问题,请编辑您的问题并显示为小标题。但代表