xslt - programming techniques

霸气de小男生 提交于 2019-12-11 23:37:51

问题


A question on Oliver Beckers efficient methods on xslt programming at this link.

We know that using this code, we can eliminate verbose xsl choose method

concat(
substring(Str1,1 div Cond),
substring(Str2,1 div not(Cond))
)

However what can we specify in 'condition', just to check for presence or absence of nodes?

we cannot specify

concat(
substring(Str1,1 div test="/node"),
substring(Str2,1 div not(test="/node"))
)

which will throw syntax errors.


回答1:


Try this expression (where node is the name of the node you want to test):

<xsl:value-of select="concat(
   substring('Yes', 1 div not(not(/root/node))), 
   substring('No', 1 div not(/root/node)))"/>

Or better still

<xsl:value-of select="concat(
   substring('Yes', 1 div boolean(/root/node)), 
   substring('No', 1 div not(/root/node)))"/>

When applied to this XML, then Yes is output

<root>
   <node>Test</node>
</root>

But when applied to this XML, the No is output

<root>
   <othernode>Test</othernode>
</root>


来源:https://stackoverflow.com/questions/13231385/xslt-programming-techniques

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!