Xslt获取创建的xml元素的值

时间:2017-07-25 11:18:11

标签: xslt

variableStore

我有类似上面的内容。 现在我想检查创建的xml标记(typeId)的条件。 即,低于xslt文件中的条件,

<xsl:when test="conditon = 'value1'">
    <typeId>4</typeId>
</xsl:when>
<xsl:when test="conditon = 'value2'">
    <typeId>4</typeId>
</xsl:when>
<xsl:when test="conditon = 'value3'">
    <typeId>4</typeId>
</xsl:when>
....
....

那么,我怎样才能在创建的标签上使用上述条件(上面的typeId是我想要创建条件的创建标签)

或其他任何方式实现上述目标?

1 个答案:

答案 0 :(得分:1)

$typeId指的是名为typeId的变量,而不是您创建的任何元素。

你可以做的是定义一个名为typeId的变量,它被设置为你想要的值,并使用该变量来创建元素,并检查你的状况。

<xsl:variable name="typeId">
  <xsl:choose>
    <xsl:when test="conditon = 'value1'">1</xsl:when>
    <xsl:when test="conditon = 'value2'">2</xsl:when>
    <xsl:when test="conditon = 'value4'">4</xsl:when>
  <xsl:choose>
</xsl:variable>

<typeId>
  <xsl:value-of select="$typeId" />
</typeId>

<xsl:if test="$typeId = 4">
  <price>100</price>
</xsl:if>

请注意,此代码必须位于相同的代码块中,因为typeId变量将在该块的范围内是本地的。