xpath中嵌套的条件if else语句 [英] Nested conditional if else statements in xpath
问题描述
我有这个XML:
<property id="1011">
<leasehold>No</leasehold>
<freehold>Yes</freehold>
<propertyTypes>
<propertyType>RESIDENTIAL</propertyType>
</propertyTypes>
</property>
我想创建一个与下面嵌套的if-else伪代码块相同的xpath语句。
and I want to create an xpath statement that is same as the following nested if-else pseudocode block.
if( propertyTypes/propertyType == 'RESIDENTIAL') {
if( leasehold == 'Yes' ){
return 'Rent'
} else
return 'Buy'
}
} else {
if( leasehold == 'Yes' ){
return 'Leasehold'
} else
return 'Freehold'
}
}
我见过有关Becker方法的一些内容,但我无法真正遵循它。 XPath真的不是我的强项。
I've seen something about Becker's method but I couldn't really follow it. XPath isn't my strong point really.
推荐答案
我。在XPath 2.0中,只需将其转换为:
if(/*/propertyTypes/propertyType = 'RESIDENTIAL')
then
(if(/*/leasehold='Yes')
then 'Rent'
else 'Buy'
)
else
if(/*/leasehold='Yes')
then 'Leasehold'
else 'Freehold'
基于XSLT 2.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 omit-xml-declaration="yes" indent="yes"/>
<xsl:template match="/">
<xsl:sequence select=
"if(/*/propertyTypes/propertyType = 'RESIDENTIAL')
then
(if(/*/leasehold='Yes')
then 'Rent'
else 'Buy'
)
else
if(/*/leasehold='Yes')
then 'Leasehold'
else 'Freehold'
"/>
</xsl:template>
</xsl:stylesheet>
在提供的XML文档上应用此转换时:
<property id="1011">
<leasehold>No</leasehold>
<freehold>Yes</freehold>
<propertyTypes>
<propertyType>RESIDENTIAL</propertyType>
</propertyTypes>
</property>
评估XPath表达式并将此评估结果复制到输出中: / strong>
the XPath expression is evaluated and the result of this evaluation is copied to the output:
Buy
II。 XPath 1.0解决方案
在XPath 1.0中,如果运算符没有。
In XPath 1.0 there isn't an if
operator.
条件语句仍然可以使用单个XPath 1.0表达式实现,但这更棘手,表达式可能不太容易阅读和理解。
A conditional statement can still be implemented with a single XPath 1.0 expression, but this is more tricky and the expression may not be too readable and understandable.
当条件 $ cond $ c时,这是一种生成
$ stringA
的通用方法(由Jeni Tennison首先提出) $ c>是 true()
,否则产生 $ stringB
:
Here is a generic way (first proposed by Jeni Tennison) to produce $stringA
when a condition $cond
is true()
and otherwise produce $stringB
:
concat(substring($stringA, 1 div $cond), substring($stringB, 1 div not($cond)))
此公式的主要成就之一是它适用于任何长度的字符串,不需要指定长度 。
解释:
这里我们使用的事实是: / p>
Here we use the fact that by definition:
number(true()) = 1
和
number(false()) = 0
和
1 div 0 = Infinity
因此,如果 $ cond
是 false
,则的第一个参数concat()
以上是:
So, if $cond
is false
, the first argument of concat()
above is:
substring($stringA, Infinity)
这是空字符串,因为 $ stringA
的长度有限。
and this is the empty string, because $stringA
has a finite length.
另一方面,如果 $ cond
是 true()
那么上面的 concat()
的第一个参数是:
On the other side, if $cond
is true()
then the first argument of concat()
above is:
sibstring($stringA, 1)
只是 $ stringA
。
因此,取决于 $ cond
的值,只有 concat()
上面是一个非空字符串(分别是 $ stringA
或 $ stringB
) 。
So, depending on the value of $cond
only one of the two arguments of concat()
above is a nonempty string (respectively $stringA
or $stringB
).
将这个通用公式应用于特定问题,我们可以翻译第一个h alf的大条件表达式为:
Applying this generic formula to the specific question, we can translate the first half of the big conditional expression into:
concat(
substring('rent',
1 div boolean(/*[leasehold='Yes'
and
propertyTypes/propertyType = 'RESIDENTIAL'
]
)
),
substring('buy',
1 div not(/*[leasehold='Yes'
and
propertyTypes/propertyType = 'RESIDENTIAL'
]
)
)
)
这应该可以让您了解如何将整个条件表达式转换为单个XPath 1.0表达式。
This should give you an idea how to translate the whole conditional expression into a single XPath 1.0 expression.
基于XSLT 1.0的版本ification :
<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:copy-of select=
"concat(
substring('rent',
1 div boolean(/*[leasehold='Yes'
and
propertyTypes/propertyType = 'RESIDENTIAL'
]
)
),
substring('buy',
1 div not(/*[leasehold='Yes'
and
propertyTypes/propertyType = 'RESIDENTIAL'
]
)
)
)
"/>
</xsl:template>
</xsl:stylesheet>
当对提供的XML文档(上面)应用此转换时,XPath表达式为评估并将此评估结果复制到输出中:
buy
请注意:
如果您决定用其他长度不同于原始字符串的字符串替换特定字符串,只需在上面的XPath 1.0表达式中替换这些字符串,您就不必担心指定任何长度。
If you decide to replace the specific strings with other strings that have different lengths than the original, you simply replace these strings in the above XPath 1.0 expression and you don't have to worry about specifying any lengths.
这篇关于xpath中嵌套的条件if else语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!