我试图声明一个具有默认值的变量,或者如果重复集中存在一个值以使用新的不同值.
这就是我到目前为止所拥有的.
<xsl:variable name="lsind">
<xsl:value-of select="'N'"/>
<xsl:for-each select='./Plan/InvestmentStrategy/FundSplit'>
<xsl:choose>
<xsl:when test="contains(./@FundName, 'Lifestyle')">
<xsl:value-of select="'Y'"/>
</xsl:when>
</xsl:choose>
</xsl:for-each>
</xsl:variable>
Run Code Online (Sandbox Code Playgroud)
我想要的是,如果./Plan/InvestmentStrategy/FundSplit/@FundName'的任何实例包含'LifeStyle然后lsind'Y',否则它将回退到默认值'N'.
我这样做就好像我使用'否则最后一次出现可能会将lsind设置回N?
有什么建议?
Mar*_*nen 13
<xsl:variable name="lsind">
<xsl:choose>
<xsl:when test="Plan/InvestmentStrategy/FundSplit[contains(@FundName, 'Lifestyle')]">
<xsl:text>Y</xsl:text>
</xsl:when>
<xsl:otherwise>
<xsl:text>N</xsl:text>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
Run Code Online (Sandbox Code Playgroud)
应该足够了
这可以在单个XPath表达式中指定(即使在XPath 1.0中):
<xsl:variable name="vLsind" select=
"substring('YN',
2 - boolean(plan/InvestmentStrategy/FundSplit[@FundName='Lifestyle']),
1)"/>
Run Code Online (Sandbox Code Playgroud)
例1:
<plan>
<InvestmentStrategy>
<FundSplit FundName="Lifestyle"/>
</InvestmentStrategy>
</plan>
Run Code Online (Sandbox Code Playgroud)
转型:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:variable name="vLsind" select=
"substring('YN',
2 - boolean(plan/InvestmentStrategy/FundSplit[@FundName='Lifestyle']),
1)"/>
<xsl:template match="/">
<xsl:value-of select="$vLsind"/>
</xsl:template>
</xsl:stylesheet>
Run Code Online (Sandbox Code Playgroud)
结果:
Y
Run Code Online (Sandbox Code Playgroud)
例2:
<plan>
<InvestmentStrategy>
<FundSplit FundName="No Lifestyle"/>
</InvestmentStrategy>
</plan>
Run Code Online (Sandbox Code Playgroud)
结果:
N
Run Code Online (Sandbox Code Playgroud)
说明:
根据定义boolean(some-node-set),true()恰好some-node-set是非空时.
根据定义number(true())是1和number(false())是0
1个2 cobined给我们:number(boolean(some-node-set))是1什么时候some-node-set不为空,否则是0.
其他单表达式解决方案:
XPath 1.0:
translate(number(boolean(YourXPathExpression)), '10', 'YN')
Run Code Online (Sandbox Code Playgroud)
XPath 2.0:
if(YourXPathExpression)
then 'Y'
else 'N'
Run Code Online (Sandbox Code Playgroud)
甚至:
('N', 'Y')[number(boolean(YourXPathExpression)) +1]
Run Code Online (Sandbox Code Playgroud)