XSLT:如何在XSLT:foreach迭代变量列表时模仿flag或break语句

XSLT:如何在XSLT:foreach迭代变量列表时模仿flag或break语句,xslt,Xslt,我是xslt新手,请原谅我的错误。 在xsl程序中,我有一个名为“foo:vars”的变量值列表,其中包含一个颜色值列表。 有一个变量声明为matchWith,它可以包含任何值(不一定存在于foor:var列表中) 程序应输出为: 如果变量matchWith包含列表“foo:vars”中存在的值,则该值应显示在标记中 与匹配的值 否则,变量matchWith中的值应该出现在另一个名为 下面是一个程序,它能够为案例1提供正确的输出,但我无法为案例2设置任何标志 <xsl:stylesheet

我是xslt新手,请原谅我的错误。
在xsl程序中,我有一个名为“foo:vars”的变量值列表,其中包含一个颜色值列表。
有一个变量声明为matchWith,它可以包含任何值(不一定存在于foor:var列表中)

程序应输出为:

  • 如果变量matchWith包含列表“foo:vars”中存在的值,则该值应显示在标记中
    与匹配的值
  • 否则,变量matchWith中的值应该出现在另一个名为
  • 下面是一个程序,它能够为案例1提供正确的输出,但我无法为案例2设置任何标志

    <xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" 
    xmlns:foo="http://foo.com" exclude-result-prefixes="foo">
      <xsl:output indent="yes" method="xml" omit-xml-declaration="yes"/>
      <foo:vars>
        <foo:var name="a1">Yellow</foo:var>
        <foo:var name="b1">red</foo:var>
        <foo:var name="c1">green</foo:var>
        <foo:var name="d1">blue</foo:var>
      </foo:vars>
      <xsl:variable name="matchWith">Yellow</xsl:variable>
      <xsl:template match="/">
        <xsl:for-each select="document('')/xsl:stylesheet/foo:vars/foo:var">
          <xsl:variable name="temp">
            <xsl:value-of select="."/>
          </xsl:variable>
          <xsl:choose>
            <xsl:when test="$temp=$matchWith">
              <color_found>
                <xsl:value-of select="$matchWith"/>
              </color_found>
            </xsl:when>
          </xsl:choose>
        </xsl:for-each>
      </xsl:template>
    </xsl:stylesheet>
    
    
    黄的
    红色
    绿色
    蓝色
    黄的
    
    问题在于,当您应该直接检查是否存在匹配值时,您正在为每个使用

    像这样:

    <xsl:stylesheet version="1.0" 
                    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
                    xmlns:foo="http://foo.com" exclude-result-prefixes="foo">
      <xsl:output indent="yes" method="xml" omit-xml-declaration="yes"/>
      <foo:vars>
        <foo:var name="a1">Yellow</foo:var>
        <foo:var name="b1">red</foo:var>
        <foo:var name="c1">green</foo:var>
        <foo:var name="d1">blue</foo:var>
      </foo:vars>
      <xsl:variable name="matchWith">Yellow</xsl:variable>
      <xsl:template match="/">
        <xsl:variable name="options"
                      select="document('')/xsl:stylesheet/foo:vars/foo:var" />
        <xsl:variable name="isMatch" select="$matchWith = $options" />
    
        <xsl:element name="color_{ substring('not_', 1, 4 * not($isMatch)) }found">
          <xsl:value-of select="$matchWith" />
        </xsl:element>
      </xsl:template>
    </xsl:stylesheet>
    
    
    黄的
    红色
    绿色
    蓝色
    黄的
    
    感谢您的回答,它适用于该场景。有没有一种方法可以让我有两个块(可以是if,或者某种程度上),如果找到/没有找到匹配项,我需要有一组不同的语句来执行。如果(matchWith呈现){some code to execute}else{some other code to execute}@Harish是的,您可以使用
    some code来执行some other code to execute
    谢谢,这是一个很大的帮助。@Harish:如果有帮助,请回答这个问题。谢谢