[英]How to apply if else statement on XSLT variable
我有一个像这样的xSLT文件
<?xml version="1.0" encoding="UTF-8" ?>
<!-- New document created with EditiX at Thu Feb 18 19:08:23 IST 2016 -->
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:fn="http://www.w3.org/2005/xpath-functions"
xmlns:xdt="http://www.w3.org/2005/xpath-datatypes"
xmlns:err="http://www.w3.org/2005/xqt-errors"
exclude-result-prefixes="xs xdt err fn">
<xsl:output method="xml" indent="yes"/>
<xsl:variable name="DIVISION_CODE">
47
</xsl:variable>
<xsl:variable name="DEPT_CODE">
KT
</xsl:variable>
<xsl:variable name="JOB_LEVEL">
4B
</xsl:variable>
<xsl:variable name="MI_GROUP">
</xsl:variable>
<xsl:variable name="A_Div_Code">
47,48,49,50
</xsl:variable>
<xsl:variable name="A_Dept_Code">
KT,VT,SF,MQ
</xsl:variable>
<xsl:variable name="A_Job_Level">
4B,2B,7B
</xsl:variable>
<xsl:variable name="D_Div_Code">
70,65,42,31
</xsl:variable>
<xsl:variable name="D_Dept_Code">
LM,MX,PQ
</xsl:variable>
<xsl:variable name="D_Job_Level">
4D,2D,3D
</xsl:variable>
<xsl:variable name="C_Div_Code">
12,76,31
</xsl:variable>
<xsl:variable name="C_Dept_Code">
ML,KL,KO
</xsl:variable>
<xsl:variable name="C_Job_Level">
4C,2C,7C
</xsl:variable>
</xsl:stylesheet>
所以基本上我有3个变量将从服务器获取数据
<xsl:variable name="DIVISION_CODE">
47
</xsl:variable>
<xsl:variable name="DEPT_CODE">
KT
</xsl:variable>
<xsl:variable name="JOB_LEVEL">
4B
</xsl:variable>
我必须对照其他预定义变量检查这些变量。
我的Sudo代码如下
If ( A_Div_Code contain '$DIVISION_CODE') and ( A_Dept_Code contains '$DEPT_CODE') and (A_job_Level contains '$JOB_LEVEL)
MP_GROUP='A'
我在W3chools上学到了必须使用的知识
<xsl:choose></xsl:choose>
但是我不知道如何定位变量
可以给我一个例子吗?
更新的XSLT文件
<?xml version="1.0" encoding="UTF-8" ?>
<!-- New document created with EditiX at Thu Feb 18 19:08:23 IST 2016 -->
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:fn="http://www.w3.org/2005/xpath-functions"
xmlns:xdt="http://www.w3.org/2005/xpath-datatypes"
xmlns:err="http://www.w3.org/2005/xqt-errors"
exclude-result-prefixes="xs xdt err fn">
<xsl:output method="xml" indent="yes"/>
<xsl:variable name="DIVISION_CODE" select="'47'" />
<xsl:variable name="DEPT_CODE" select="'KT'" />
<xsl:variable name="JOB_LEVEL" select="4B''" />
<xsl:variable name="A_Div_Code" select="'47,48,49,50
'" />
<xsl:variable name="A_Dept_Code" select="'KT,VT,SF,MQ
'" />
<xsl:variable name="A_Job_Level" select="'4B,2B,7B
'" />
<xsl:variable name="D_Div_Code" select="'70,65,42,12,76,31,47,48,49,50
'" />
<xsl:variable name="D_Dept_Code" select="'LM,MX,PQ,ML,KL,KO
,KT,VT,SF,MQ
'" />
<xsl:variable name="D_Job_Level" select="'4D,2D,3D
,4C,2C,7C,4B,2B,7B'" />
<xsl:variable name="C_Div_Code" select="'12,76,31,47,48,49,50
'" />
<xsl:variable name="C_Dept_Code" select="'ML,KL,KO
,KT,VT,SF,MQ
'" />
<xsl:variable name="C_Job_Level" select="'4C,2C,7C,4B,2B,7B
'" />
<xsl:choose>
<xsl:when test="contains($A_Div_Code, $DIVISION_CODE) and contains($A_Dept_Code, $DEPT_CODE) and contains($A_job_level, $JOB_LEVEL)">
<xsl:variable name="MI_GROUP" select="'A
'" />
</xsl:when>
<xsl:when test="contains($C_Div_Code, $DIVISION_CODE) and contains($C_Dept_Code, $DEPT_CODE) and contains($C_job_level, $JOB_LEVEL)">
<xsl:variable name="MI_GROUP" select="'A
,C'" />
</xsl:when>
<xsl:when test="contains($D_Div_Code, $DIVISION_CODE) and contains($D_Dept_Code, $DEPT_CODE) and contains($D_job_level, $JOB_LEVEL)">
<xsl:variable name="MI_GROUP" select="'A
,C,D'" />
</xsl:when>
<xsl:otherwise>
<xsl:variable name="MI_GROUP" select="'A
,C,D,Z'" />
</xsl:otherwise>
</xsl:choose>
</xsl:stylesheet>
错误
ERROR - Looks like XML you provided is not valid: Error on line -1 of document : Premature end of file. Nested exception: Premature end of file.
谢谢
如果您只有一个简单的条件if,请使用xsl:if
。 如果需要处理“ else if”和“ else”,请使用xsl:choose
。 例如:
<xsl:if test="contains($A_Div_Code, $DIVISION_CODE) and contains($A_Dept_Code, $DEPT_CODE) and contains($A_job_level, $JOB_LEVEL)>
<!-- output what you want to here -->
</xsl:if>
与choose
相同的文字是:
<xsl:choose>
<xsl:when test="contains($A_Div_Code, $DIVISION_CODE) and contains($A_Dept_Code, $DEPT_CODE) and contains($A_job_level, $JOB_LEVEL)>
<!-- output what you want to here -->
</xsl:when>
<xsl:when test="some other logical test...">
<!-- output what you want to here - but note that this won't get executed if the first test succeeded! -->
</xsl:when>
<xsl:otherwise>
<!-- output default here -->
</xsl:otherwise>
</xsl:choose>
请注意,只有第一个 when
会匹配,而其他after会被点火,即使它们已经匹配也是如此。
最后一点-您可能不想使用它来分配变量。 这些变量只能在<xsl:if>
的范围内使用,并且如果在该范围之前声明它们,则以后不能将它们分配给变量(变量在XSLT中是只读的,在声明之后不能将它们赋给像其他语言一样):
<xsl:if test="true()">
<xsl:variable name="testing" select="'asdf'"/>
</xsl:if>
<xsl:value-of select="$testing" /> <!-- error! $testing isn't in scope-->
和
<xsl:variable name="testing" select="'foo'" />
<xsl:if test="true()">
<xsl:variable name="testing" select="'asdf'"/>
</xsl:if>
<xsl:value-of select="$testing" /> <!-- output will be 'foo', not 'asdf'! -->
正因为如此,它通常最好使用template
s到匹配满足一定的条件,而不是使用节点集if
与choose
的语句:
<xsl:template match="/path/to/node[DIVISION_CODE = $A_Div_Code] and ..." >
这样,该模板将使您能够执行所需的操作,并且与嵌入到select语句中相比,该模板通常更易于管理和移植。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.