XSL在测试时使用变量

时间:2012-11-26 14:14:34

标签: variables xslt

我在使用when test=...尝试使用变量值时遇到了很多困难。

当我输出$tcbrand时,它会输出'Direct',但当我对其进行操作时,它会返回false并点击其他块。我做错了什么?

    <fo:static-content flow-name="header-normal" font-size="10pt">  

                <xsl:variable name="tcbrand" select="smf:entity[@type='d2501b79-f888-4aa8-9fcf-a667a4c47c84']"/>

                <fo:block font-size="10pt" text-align="left">
                    <fo:inline font-weight="normal">Brand: <xsl:value-of select="$tcbrand"/></fo:inline>
                </fo:block>

                <xsl:choose>
                    <!--<xsl:if test="$image = 'hello' ">-->
                    <xsl:when test="string($tcbrand)='Direct'">
                        <fo:block text-align="right" space-before="0mm">
                            <fo:external-graphic src="url('C:\Program Files (x86)\numerointeractive\whitemail\Images\18-30.png')" />
                        </fo:block>             
                    </xsl:when>

                    <xsl:otherwise>                         
                            <fo:block text-align="right" space-before="0mm">
                                <fo:external-graphic src="url('C:\Program Files (x86)\numerointeractive\whitemail\Images\ecom2.jpg')" />
                            </fo:block>             
                    </xsl:otherwise>                                
                </xsl:choose>

这是我生成的xml:

 <smf:entity id="48659" type="d2501b79-f888-4aa8-9fcf-a667a4c47c84" confidence="1.0" author="2e0b99b3-9cba-4736-a59b-fe00b5f62871" validated="true" preferred="false" requiresComponentExtraction="false" changeTime="2012-11-26T14:47:28.840Z" nonPersistedId="false" modified="false">
           <smf:value>Direct</smf:value>
        </smf:entity>

2 个答案:

答案 0 :(得分:4)

<xsl:variable name="tcbrand" select="smf:entity[@type='d2501b79-f888-4aa8-9fcf-a667a4c47c84']"/>

会将tcbrand变量设置为smf:entity元素。元素的字符串值是其所有后代文本节点的串联,对于

<smf:entity id="48659" type="d2501b79-f888-4aa8-9fcf-a667a4c47c84" confidence="1.0" author="2e0b99b3-9cba-4736-a59b-fe00b5f62871" validated="true" preferred="false" requiresComponentExtraction="false" changeTime="2012-11-26T14:47:28.840Z" nonPersistedId="false" modified="false">
       <smf:value>Direct</smf:value>
    </smf:entity>

表示换行符,七个空格,“直接”,另一个换行符和另外四个空格。这不等于“直接”。我可以想到三个可能的修复:你可以使用

<xsl:strip-space elements="*"/>

位于样式表的顶部,这将导致它完全忽略输入文档中所有仅限空格的文本节点,在进行测试时可以normalize-space

<xsl:when test="normalize-space($tcbrand)='Direct'">

或者您可以在变量声明点

获取子<smf:value>元素的值而不是父<smf:entity>元素的值
<xsl:variable name="tcbrand"
  select="smf:entity[@type='d2501b79-f888-4aa8-9fcf-a667a4c47c84']/smf:value"/>

或在测试时

<xsl:when test="$tcbrand/smf:value='Direct'">

答案 1 :(得分:0)

您的变量tcbrand似乎包含文档节点,而不是示例中的文本字符串。

我怀疑它在您的示例中打印Direct的原因是因为这是所选节点的文本内容。这将在test属性中评估为false,因为该变量不包含文字字符串Direct

要从选定的音符中选择文本值,请在变量声明中使用/text(),例如

 <xsl:variable name="tcbrand" select="smf:entity[@type='d2501b79-f888-4aa8-9fcf-a667a4c47c84']/text()"/>

'When should I use /text() at the end of my XPath expression?' on the site xquery.com中包含text()的使用:

  

您可能已经看到很多在XPath表达式末尾使用/ text()的示例,您可能想知道,我应该这样做吗?有什么缺点吗?

     

在某些情况下,您确实希望将/ text()附加到XPath表达式,以强制XQuery考虑XPath表达式的文本值;典型的情况是你想动态创建结果元素的值;像这样的东西:

<title>{$doc//book[1]/book-title}</title>
  

...生成此输出:

<title>
    <book-title>La Divina Commendia</book-title>
</title>
  

但是,如果您要创建的内容实际上是这样的:

<title>La Divina Commendia</title>
  

...然后你需要指示XQuery使用元素的文本值,一个简单的方法是将/ text()附加到XPath表达式:

<title>{$doc//book[1]/book-title/text()}</title>
相关问题