是否有一个「;“如果-那么-否则”;XPath中的语句?
xpath中丰富的函数似乎可以实现“如果”。然而,我的引擎一直坚持“没有这样的功能”,我在网上几乎找不到任何文档(我找到了一些可疑的来源,但它们的语法不起作用) 我需要从字符串的末尾删除“:”(如果存在),所以我想这样做:是否有一个「;“如果-那么-否则”;XPath中的语句?,xpath,if-statement,Xpath,If Statement,xpath中丰富的函数似乎可以实现“如果”。然而,我的引擎一直坚持“没有这样的功能”,我在网上几乎找不到任何文档(我找到了一些可疑的来源,但它们的语法不起作用) 我需要从字符串的末尾删除“:”(如果存在),所以我想这样做: if (fn:ends-with(//div [@id='head']/text(),': ')) then (fn:substring-before(//div [@id='head']/text(),': ') ) else
if (fn:ends-with(//div [@id='head']/text(),': '))
then (fn:substring-before(//div [@id='head']/text(),': ') )
else (//div [@id='head']/text())
有什么建议吗 用fn:replace(字符串、模式、替换)代替怎么样
XPATH经常在XSLT中使用,如果您处于这种情况,并且没有XPATH 2.0,则可以使用:
<xsl:choose>
<xsl:when test="condition1">
condition1-statements
</xsl:when>
<xsl:when test="condition2">
condition2-statements
</xsl:when>
<xsl:otherwise>
otherwise-statements
</xsl:otherwise>
</xsl:choose>
条件1声明
条件2声明
其他声明
W3.org上的官方语言规范详细说明了该语言确实支持if语句。特别是见。连同语法格式和解释,它给出了以下示例:
if ($widget1/unit-cost < $widget2/unit-cost)
then $widget1
else $widget2
但是,我强烈怀疑这可能会解决这个问题,因为您的XPath引擎似乎试图将if
解释为一个函数,而实际上它是该语言的一个特殊构造
最后,要指出显而易见的一点,请确保您的XPath引擎确实支持XPath2.0(与早期版本相反)!我不认为条件表达式是XPath早期版本的一部分。是的,在XPath 1.0中有一种方法可以做到这一点:
concat(
substring($s1, 1, number($condition) * string-length($s1)),
substring($s2, 1, number(not($condition)) * string-length($s2))
)
虽然我会尝试摆脱以前所有的“//”
另外,/div[@id='head']
可能返回多个节点。
请注意,使用//div[@id='head'][1]
更具防御性。根据pkarat定律,可以在1.0版中实现条件XPath
对于您的情况,请遵循以下概念:
concat(substring-before(your-xpath[contains(.,':')],':'),your-xpath[not(contains(.,':'))])
这肯定会奏效的。看看它是如何工作的。给出两个输入
praba:
karan
对于第一个输入:它包含:
,因此条件为true,:
之前的字符串将是输出,比如说praba
是您的输出。第二个条件为假,因此没有问题
对于第二个输入:它不包含:
所以条件失败,到第二个条件时,字符串不包含:
所以条件为真。。。因此,将抛出输出karan
最后,您的输出将是praba
,karan
,我个人将使用XSLT转换XML并删除尾随冒号。例如,假设我有以下输入:
<?xml version="1.0" encoding="UTF-8"?>
<Document>
<Paragraph>This paragraph ends in a period.</Paragraph>
<Paragraph>This one ends in a colon:</Paragraph>
<Paragraph>This one has a : in the middle.</Paragraph>
</Document>
本段以句号结尾。
这个以冒号结尾:
这个有A:在中间。
如果我想去掉段落中的尾随冒号,我将使用以下XSLT:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:fn="http://www.w3.org/2005/xpath-functions"
version="2.0">
<!-- identity -->
<xsl:template match="/|@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<!-- strip out colons at the end of paragraphs -->
<xsl:template match="Paragraph">
<xsl:choose>
<!-- if it ends with a : -->
<xsl:when test="fn:ends-with(.,':')">
<xsl:copy>
<!-- copy everything but the last character -->
<xsl:value-of select="substring(., 1, string-length(.)-1)"></xsl:value-of>
</xsl:copy>
</xsl:when>
<xsl:otherwise>
<xsl:copy>
<xsl:apply-templates/>
</xsl:copy>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
不幸的是,前面的答案对我来说没有选择余地,所以我研究了一段时间,找到了这个解决方案:
如果某个节点存在,我使用它来输出文本。4是文本foo的长度。所以我想更优雅的解决方案是使用变量
substring('foo',number(not(normalize-space(/elements/the/element/)))*4)
更简单的XPath 1.0解决方案,改编自Tomalek(发布于此处)和Dimitre():
注意:我发现将bool转换为int需要显式number(),否则一些XPath计算器会抛出类型不匹配错误。根据XPath处理器对类型匹配的严格程度,您可能不需要它。问题是否有更多的上下文?XPath通常用于查询信息,而不是作为操作工具。操作通常由XSL模板或更高级别的语言完成。我发现w3schools教程很容易使用。你可以从那里开始@詹姆斯:这实际上是一个疑问。不要让if-then-else语法欺骗你——从技术上讲,它是一个“条件表达式”,而不是一个“if语句”——更类似于许多编程语言中的三元条件运算符。类似的问题:我不认为它建议你不应该使用括号,只是你不需要使用它们。如果它抱怨“没有这样的函数”,那么我怀疑他没有使用XPath2。规范要求在条件的周围加上括号。@Rob:这是很可能的,但是我声明它不一定是修复的。但是,如果您想让某些东西发挥作用,那么始终值得遵循以下示例:但是是的,不知怎么的,我错过了条件周围的括号。现在修复了。我可能不会使用这个(我只是升级xpath引擎),但这是一个很好的技巧和一个很好的提示:)与数值表达式(如果不需要选择任何字符串),它更简单,因为您可以执行X*number(条件)+Y*number(条件)等操作,其中number(条件)为0表示false,1表示true(“布尔真值转换为1;布尔假值转换为0。”)可能这些表达式中需要+1,我必须在以下位置添加:…贝克尔方法。我正在寻找c#@Tomalak的?
操作符。链接已断开,下面是另一个链接,指向讨论该方法的页面:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:fn="http://www.w3.org/2005/xpath-functions"
version="2.0">
<!-- identity -->
<xsl:template match="/|@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<!-- strip out colons at the end of paragraphs -->
<xsl:template match="Paragraph">
<xsl:choose>
<!-- if it ends with a : -->
<xsl:when test="fn:ends-with(.,':')">
<xsl:copy>
<!-- copy everything but the last character -->
<xsl:value-of select="substring(., 1, string-length(.)-1)"></xsl:value-of>
</xsl:copy>
</xsl:when>
<xsl:otherwise>
<xsl:copy>
<xsl:apply-templates/>
</xsl:copy>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
substring('foo',number(not(normalize-space(/elements/the/element/)))*4)
concat(substring($s1, 1 div number($cond)), substring($s2, 1 div number(not($cond))))