I. 在XPath 2.0中,这个只需要翻译成:
if(leasehold='Yes')
then 'Rent'
else 'Buy'
)
else
if(
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表达式并将此评估结果复制到输出:
Buy
II. XPath 1.0 解决方案
XPath 1.0 中没有 if
运算符。
仍然可以使用单个 XPath 1.0 表达式来实现条件语句,但这更加棘手,表达式可能不太可读和易于理解。
这里是一种通用方式(最初由 Jeni Tennison 提出),可以在条件 $cond
为 true()
时生成 $stringA
,否则生成 $stringB
:
concat(substring($stringA, 1 div $cond), substring($stringB, 1 div not($cond)))
这个公式的主要成就之一是它可以适用于任意长度的字符串,而不需要指定长度。
解释:
在这里,我们利用了以下定义:
number(true()) = 1
而且
number(false()) = 0
并且那个。
1 div 0 = Infinity
因此,如果$cond
为false
,则上面concat()
的第一个参数是:
substring($stringA, Infinity)
这是一个空字符串,因为$stringA
有限长度。
另一方面,如果$cond
是true()
,那么上面concat()
的第一个参数是:
sibstring($stringA, 1)
那只是 $stringA
。
所以,根据 $cond
的值,concat()
上面的两个参数中只有一个是非空字符串(分别为 $stringA
或 $stringB
)。
将这个通用公式应用于特定问题,我们可以将大条件表达式的前半部分翻译成:
concat(
substring('rent',
1 div boolean(
这应该能让你了解如何将整个条件表达式转换为单个的XPath 1.0表达式。
XSLT 1.0验证基于:<xsl:stylesheet version="1.0" xmlns:xsl="http:
<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表达式中替换这些字符串,无需担心指定任何长度。