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
根据XPATH条件返回字符串值_Xpath - Fatal编程技术网

根据XPATH条件返回字符串值

根据XPATH条件返回字符串值,xpath,Xpath,如果我有下面的XML,那么如何指定xpath以根据条件返回字符串。例如这里如果//b[@id=23],则为“利润”,否则为“损失” 123 234 345 456 678 567 你不能;对此,您必须使用XQuery。见例 或者,如果生成的字符串仅在Java中使用,则可以在Java代码中处理XPath返回的值: XPathFactory factory = XPathFactory.newInstance(); XPath xpath = factory.newXPath(); XPathEx

如果我有下面的XML,那么如何指定xpath以根据条件返回字符串。例如这里
如果//b[@id=23],则为“利润”,否则为“损失”


123
234
345
456
678
567

你不能;对此,您必须使用XQuery。见例

或者,如果生成的字符串仅在Java中使用,则可以在Java代码中处理XPath返回的值:

XPathFactory factory = XPathFactory.newInstance();
XPath xpath = factory.newXPath();
XPathExpression expr = xpath.compile("//b[@id=23]");
boolean result = expr.evaluate(doc, XPathConstants.BOOLEAN);

if (result) return "Profit";
else return "Loss";

I.XPath 2.0解决方案(如果您可以访问XPath 2.0引擎,建议使用)

II。XPath 1.0解决方案

Profit
<b id="23"/>
<b id="24"/>
Loss
concat(substring($stringX, 1 div $cond),
       substring($stringY, 1 div not($cond)),
      )
使用:

使用XSLT 1.0进行验证

Profit
<b id="23"/>
<b id="24"/>
Loss
concat(substring($stringX, 1 div $cond),
       substring($stringY, 1 div not($cond)),
      )
这一转变:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

 <xsl:template match="/">
  <xsl:value-of select=
   "concat(substring('Profit', 1 div boolean(//b[@id=23])),
           substring('Loss', 1 div not(//b[@id=23]))
          )"/>
 </xsl:template>
</xsl:stylesheet>
当我们在XML文档中替换时

Profit
<b id="23"/>
<b id="24"/>
Loss
concat(substring($stringX, 1 div $cond),
       substring($stringY, 1 div not($cond)),
      )
说明

Profit
<b id="23"/>
<b id="24"/>
Loss
concat(substring($stringX, 1 div $cond),
       substring($stringY, 1 div not($cond)),
      )
我们利用以下事实:

substring($someString, $N)
是所有
$N>字符串长度($someString)
的空字符串

另外,数字
无穷大
是唯一大于任何字符串长度的数字

最后:

number(true())
根据定义是
1

number(false())
根据定义是
0

因此:

1 div$someCondition

$someCondition
true()

$someCondition
false()

因此,如果我们想在
$Cond
true()
时生成
$stringX
,并在
$Cond
false()
时生成
$stringY
,表达这一点的一种方法是:

Profit
<b id="23"/>
<b id="24"/>
Loss
concat(substring($stringX, 1 div $cond),
       substring($stringY, 1 div not($cond)),
      )

在上述表达式中,
concat()
函数的两个参数中正好有一个非空

我正在使用JAXP进行xml处理。如何在这里使用这个xquery?如果您使用的是JAXP,那么为什么不在Java端测试检索到的字符串值呢?我的问题是基于节点属性值,我必须决定结果字符串。当我试图检索字符串时,没有收到任何信息,因为这个节点只有一个属性。如果我给出类似于//b[@id=23]的条件,它将返回一个布尔值。您不能检查该布尔值是否为真,并根据该值返回相应的字符串吗?e、 g.如果(节点_具有_属性)返回“利润”,否则返回“损失”;是的,我检查了,返回的值是真的,但我不知道如何返回“利润”为真。任何示例代码都会对我有帮助。好问题,+1。有关纯XPath 1.0单行表达式,请参见我的答案。:)还添加了广泛的解释和显而易见的XPath2.0解决方案。+1 Dimitre您总是提供出色的解释,尤其是涉及XPath的问题。根据我的经验:)我正在使用XPath1.0解决方案的一个变体,它工作得很好。非常感谢。concat(子字符串($stringX,1 div$cond),子字符串($stringY,1 div$otherCond)),)