以下是我的条件:
If (/person/address[type = "STD"]/addresskey = and /person/address[type = "BA"/addresskey )
  
  那么我应该得到/person/phone[2]/telephonenum. 如果第二个电话号码不存在,那么它应该得到第一个电话号码。
我猜addresskey你的意思是key(提供的 XML 中没有addresskey元素)。
此外,XML 格式不正确,我不得不更正即。
现在让我们解决规定的要求:
第一的:
  我应该得到/person/phone[2]/telephonenum.
翻译为:
/*/phone[2]
然后:
  如果第二个电话号码不存在,那么它应该得到第一个电话号码。
将上面的表达式修改为:
/*/phone[not(position() >2)][last()]
最后:
If (/person/address[type = "STD"]/addresskey = and /person/address[type = "BA"/addresskey )
完整的表达式变为:
/*/phone[not(position() >2)][last()]
            [/*/address[type = 'STD']/key
            =
             /*/address[type = 'BA']/key
            ]
基于 XSLT 的验证:
<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>
 <xsl:template match="/">
  <xsl:copy-of select=
   "/*/phone[not(position() >2)][last()]
         [/*/address[type = 'STD']/key
         =
          /*/address[type = 'BA']/key
         ]
   "/>
 </xsl:template>
</xsl:stylesheet>
当此转换应用于提供的(和更正的)XML 时:
<person>
    <address>
        <type>STD</type>
        <key>1234</key>
    </address>
    <address>
        <type>BA</type>
        <key>1234</key>
    </address>
    <phone>
        <type>TEL</type>
        <key>1234</key>
        <telephonenum>7</telephonenum>
    </phone>
    <phone>
        <type>TEL</type>
        <key>1234</key>
        <telephonenum>8</telephonenum>
    </phone>
    <phone>
        <type>TEL</type>
        <key>1234</key>
        <telephonenum>9</telephonenum>
    </phone>
</person>
想要的节点被选中并输出:
<phone>
   <type>TEL</type>
   <key>1234</key>
   <telephonenum>8</telephonenum>
</phone>
二、XPath 2.0 表达式:
for $check in
      /*/address[type = 'STD']/key[1]
     eq
      /*/address[type = 'BA']/key[1],
    $p1 in /*[$check]/phone[1],
    $p2 in /*[$check]/phone[2]
 return
   ($p2, $p1)[1]