【问题标题】:Putting Value In A Variable And Using It In XSLT将值放入变量并在 XSLT 中使用它
【发布时间】:2013-03-15 20:35:44
【问题描述】:
<xsl:choose>
  <xsl:when test="type='LEVEL'">
    <xsl:variable name="myVar" select = "value"/>
      <xsl:variable name="spaces" select = "'&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0'"/>
      <xsl:value-of select="substring($spaces, 1, $myVar)"/>
   </xsl:when>

我在 XSLT 中有上述代码。 myVar 是一个变量,其值类似于(1 或 2 或 3)。 我需要将以下代码行的输出存储在一个变量中,并在 when 条件之外使用它。

xsl:value-of select="substring($spaces, 1, $myVar)"/

我目前无法做到。 有人可以提出一些建议吗?

【问题讨论】:

    标签: xml xslt xslt-1.0


    【解决方案1】:

    我不确定您要做什么,但您可以尝试以下操作。 源 XML:

    <?xml version="1.0" encoding="UTF-8" standalone="yes" ?> 
    <Result>
    <resultDetails>
        <resultDetailsData>
            <itemProperties>
                <ID>1</ID>
                <type>LEVEL</type> 
                <value xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xs="http://www.w3.org/2001/XMLSchema" xsi:type="xs:int">5</value> 
            </itemProperties>
        </resultDetailsData>
    </resultDetails>
    </Result>
    

    应用此 XSLT:

    <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="1.0">
    
    
    <xsl:template match="itemProperties">
        <xsl:variable name="fromOutputTemplate">
            <xsl:call-template name="output"/>  
        </xsl:variable>
        <out>
            <xsl:value-of select="$fromOutputTemplate"/>    
        </out>          
    </xsl:template>
    
    <xsl:template name="output">
        <xsl:variable name="spaces" select = "'&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;'"/>
        <xsl:variable name="myVar" select = "value"/>
        
        <xsl:choose>
            <xsl:when test="type='LEVEL'">
                    <xsl:value-of select="substring($spaces, 1, $myVar)"/>
            </xsl:when>
        </xsl:choose>
    </xsl:template>
    
    
    </xsl:stylesheet>
    

    它给你这个输出:

    <?xml version="1.0" encoding="UTF-8"?>
    
        
            <out>     </out>
    

    这是你想要的方式吗?

    【讨论】:

      【解决方案2】:

      你不能。 您可以在 when 条件之外声明变量(即使它们的声明中的某些 XPath 会失败并返回 null),或者在 when 条件内使用输出。 但是,如果要使用输出,为什么还要使用选择? 最后一次尝试,可能是声明变量并在其序列构造函数中使用选择,如下所示:

      <!-- You declare the 'tool' variables alone -->
      <xsl:variable name="myVar" select = "value"/>
      <xsl:variable name="spaces" select = "'&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0'"/>
      
      <!-- For myVarSub you use a sequence constructor instead of the select way -->
      <xsl:variable name="myVarSub">      
          <xsl:choose>
             <xsl:when test="type='LEVEL'">    
                  <!-- xsl:sequence create xml node -->
                  <xsl:sequence select="substring($spaces, 1, $myVar)"/>
             </xsl:when>
          <xsl:choose>
      </xsl:variable>
      

      之后,只需在需要时输出或使用变量。如果不添加其他 when 条件,则当 test 为 false 时,myVar 将为 null。但是,请注意这是一个 xslt 2.0 解决方案,因为 xsl:sequence。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-08-14
        • 1970-01-01
        • 2014-10-13
        • 1970-01-01
        • 2013-03-14
        • 2020-04-02
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多