はじめに
Template を使ったサービスで、以下のような if タグから条件分岐させるケースにおいて、ある2通りの {expression} について解説します。
<?if {expression}?>
...
<?elif {expression}?>
...
<?else?>
...
<?end?>
boolean と XPath Evaluation
このような Template を使ったサービスがあるとします
-YANG
}
leaf xxx {
type boolean;
default false;
}
-XML Template
#1
<?if {abc}?>
<some config ... />
<?end?>
#2
<?if {abc = 'true'}?>
<some config ... />
<?end?>
#1の例では、XPath evalution の結果は boolean true になります。
xpath: template int-test-template: evaluating: string(boolean(abc))
xpath: template int-test-template: result: true
"abc" がパスとして存在しているか (exists) 否かを評価しています。#2の例では、テンプレート適用時に abc を指定するまたは abc をデフォルト値にしている場合にのみ true と評価されます。
xpath: template int-test-template: evaluating: string(boolean(abc ='true'))
xpath: template int-test-template: result: true
xpath: template int-test-template: evaluating: string(boolean(abc ='true'))
xpath: template int-test-template: result: false
#1 の結果については XML Path Language ドキュメンテーションに説明がありますので、そちらをご参照下さい。
一部抜粋
The boolean function converts its argument to a boolean as follows:
a number is true if and only if it is neither positive or negative zero nor NaN
a node-set is true if and only if it is non-empty
a string is true if and only if its length is non-zero
an object of a type other than the four basic types is converted to a boolean in a way that is dependent on that type