Xml xpath-if-else结构

Xml xpath-if-else结构,xml,xpath,xpath-2.0,Xml,Xpath,Xpath 2.0,我正在试验xpath 以下是我用于实验的xml: <moves> <roll player="1">6</roll> <piece nr="1" player="1" field="1"/> <roll player="2">4</roll> <roll player="2">6</roll> <piece nr="5" player="2" field=

我正在试验xpath

以下是我用于实验的xml:

<moves>
    <roll player="1">6</roll>
    <piece nr="1" player="1" field="1"/>
    <roll player="2">4</roll>
    <roll player="2">6</roll>
    <piece nr="5" player="2" field="11"/>
    <roll player="1">4</roll>
    <piece nr="1" player="1" field="5"/>
    <roll player="2">6</roll>
    <piece nr="5" player="2" field="17"/>
    <roll player="1">6</roll>
    <piece nr="2" player="1" field="1"/>
    <roll player="2">6</roll>
    <piece nr="6" player="2" field="11"/>
</moves>

如果第二个元素是player 1或不是player 1,那么这会返回给我,所以现在我想添加一个else路径,就像它是?那么如何添加它呢?

使用XPath 2.0表达式,如下所示:

if(/moves/roll[2]/@player eq '1')
  then 'player: 1'
  else ('player: ', data(/moves/roll[2]/@player) )
基于XSLT2.0的验证:

<xsl:stylesheet version="2.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:xs="http://www.w3.org/2001/XMLSchema">
 <xsl:output method="text"/>

 <xsl:template match="/">
  <xsl:sequence select=
  "if(/moves/roll[2]/@player eq '1')
      then 'player: 1'
      else ('player: ', data(/moves/roll[2]/@player) )
  "/>
 </xsl:template>
</xsl:stylesheet>
<moves>
    <roll player="1">6</roll>
    <piece nr="1" player="1" field="1"/>
    <roll player="2">4</roll>
    <roll player="2">6</roll>
    <piece nr="5" player="2" field="11"/>
    <roll player="1">4</roll>
    <piece nr="1" player="1" field="5"/>
    <roll player="2">6</roll>
    <piece nr="5" player="2" field="17"/>
    <roll player="1">6</roll>
    <piece nr="2" player="1" field="1"/>
    <roll player="2">6</roll>
    <piece nr="6" player="2" field="11"/>
</moves>
player:  2

我不明白你到底想要什么。加上到目前为止您已经尝试过的(什么样的XPath表达式对您不起作用,但您希望它能起作用)。好的,这里是我尝试过的:布尔(/game/moves/roll[2]/@player=1)给我假回退,因为玩家不是一个,但如果它是一个,如何插入带有动作的else路径(如回退)??@user1248720您看到了吗?您的else路径可能是XPATH中的逻辑OR。您想在else上“选择”什么?这是纯XPath还是XSLT样式表?
player:  2