如何在XSLT变量上应用if else语句

时间:2016-02-18 14:18:12

标签: xml xslt

我有一个像这样的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.

谢谢

1 个答案:

答案 0 :(得分:2)

如果您只有一个简单的条件,请使用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才会匹配,其他人将会被忽略,即使他们匹配也是如此。

最后一点 - 您可能不想使用它来分配变量。这些变量只能在<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来匹配符合特定条件的节点集,而不是使用ifchoose语句:

<xsl:template match="/path/to/node[DIVISION_CODE = $A_Div_Code] and ..." >

这个模板将使您能够执行所需的操作,并且通常比嵌入在select语句中更容易管理和更轻松。