【问题标题】:XSLT 1.0 Conditional ValueXSLT 1.0 条件值
【发布时间】:2022-10-04 20:04:45
【问题描述】:

我有这样的条件:

<xsl:variable name="MsgIdBs">
                <xsl:variable name="dateB" select="substring($MsgIdB,1,8)"/>
                <xsl:variable name="biCodeB" select="substring($MsgIdB,9,8)"/>
                <xsl:variable name="trTpB" select="substring($MsgIdB,17,3)"/>
                <xsl:variable name="snB" select="substring($MsgIdB,20,8)"/>
                <xsl:choose>
                    <xsl:when test="not(fn:matches($MsgIdB, '^[a-zA-Z0-9]*$') and string-length($MsgIdB) &lt;= 35)">
                        <xsl:copy>
                            <xsl:copy-of select="$MsgIdB"/>
                        </xsl:copy>
                        <xsl:variable name="flag" select="'false'"/>
                    </xsl:when>
                    <xsl:when test="not(number(substring($dateB, 1, 4)) >= 1970 and number(substring($dateB, 5, 2)) &lt;= 12 and number(substring($dateB, 7, 2)) &lt;= 31)">
                        <xsl:copy>
                            <xsl:copy-of select="$MsgIdB"/>
                        </xsl:copy>
                        <xsl:variable name="flag" select="'false'"/>
                    </xsl:when>
                    <xsl:when test="not(contains($trTpB,'010') or contains($trTpB,'011') or contains($trTpB,'019') or contains($trTpB,'110') or contains($trTpB,'510') or contains($trTpB,'610') or contains($trTpB,'710') or contains($trTpB,'720') or contains($trTpB,'000'))">
                        <xsl:copy>
                            <xsl:copy-of select="$MsgIdB"/>
                        </xsl:copy>
                        <xsl:variable name="flag" select="'false'"/>
                    </xsl:when>
                    <xsl:when test="not(fn:matches($snB, '^\d+$'))">
                        <xsl:copy>
                            <xsl:copy-of select="$MsgIdB"/>
                        </xsl:copy>
                        <xsl:variable name="flag" select="'false'"/>
                    </xsl:when>
                    <xsl:otherwise>
                        <xsl:value-of select="$MsgIdB"/>
                    </xsl:otherwise>
                </xsl:choose>
            </xsl:variable>

我有一个变量来检查变量名标志的值。但我总是从其他方面获得价值。永远不要在有条件的情况下从变量标志中获取值。像这样的代码:

<xsl:variable name="output">
                <xsl:choose>
                    <xsl:when test="$flag = 'false'">
                        <ns2:TxSts>Not Deal</ns2:TxSts>
                    </xsl:when>
                    <xsl:otherwise>
                        <ns2:TxSts>Deal</ns2:TxSts>
                    </xsl:otherwise>
                </xsl:choose>
            </xsl:variable>

我如何在条件下从变量名称标志中获取值并将基数与变量输出进行比较。

【问题讨论】:

  • 变量的作用域是它的后续兄弟姐妹及其后代。在另一个变量中定义的变量不存在于它之外。
  • 另请注意,您的问题被标记为xslt-1.0,但matches() 函数需要XSLT 2.0 处理器——正如您已经被告知here

标签: xslt-1.0


【解决方案1】:
Create a seperate variable for flag.  This will keep the flag varible in scope for use in following code.  

<xsl:variable name="flag">
  <xsl:variable name="dateB" select="substring($MsgIdB,1,8)"/>
  <xsl:variable name="biCodeB" select="substring($MsgIdB,9,8)"/>
  <xsl:variable name="trTpB" select="substring($MsgIdB,17,3)"/>
  <xsl:variable name="snB" select="substring($MsgIdB,20,8)"/>
  <xsl:choose>
      <xsl:when test="not(number(substring($dateB, 1, 4)) >= 1970 and number(substring($dateB, 5, 2)) &lt;= 12 and number(substring($dateB, 7, 2)) &lt;= 31)">
          <xsl:value-of select="'false'"/>
      </xsl:when>
      <xsl:when test="not(contains($trTpB,'010') or contains($trTpB,'011') or contains($trTpB,'019') or contains($trTpB,'110') or contains($trTpB,'510') or contains($trTpB,'610') or contains($trTpB,'710') or contains($trTpB,'720') or contains($trTpB,'000'))">
          <xsl:value-of select="'false'"/>
      </xsl:when>
      <xsl:when test="not(fn:matches($snB, '^d+$'))">
          <xsl:value-of select="'false'"/>
      </xsl:when>
  </xsl:choose>
</xsl:variable>

Use the flag to create MsgIdBs.    
<xsl:variable name="MsgIdBs">
  <xsl:choose>
      <xsl:when test="contains($flag, 'false)">
          <xsl:copy>
            <xsl:copy-of select="$MsgIdB"/>
          </xsl:copy>
      </xsl:when>
      <xsl:otherwise>
          <xsl:value-of select="$MsgIdB"/>
      </xsl:otherwise>
  </xsl:choose>
</xsl:variable>

Use the flag again to create output.  
<xsl:variable name="output">
  <xsl:choose>
    <xsl:when test="contains($flag, 'false)">
      <ns2:TxSts>Not Deal</ns2:TxSts>
    </xsl:when>
    <xsl:otherwise>
      <ns2:TxSts>Deal</ns2:TxSts>
    </xsl:otherwise>
  </xsl:choose>
</xsl:variable>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-24
    • 1970-01-01
    • 1970-01-01
    • 2016-06-23
    • 2021-07-19
    相关资源
    最近更新 更多