简体   繁体   中英

XSL using a variable with when test

I am having much difficulty trying to operate on a variable value using when test=... .

When I output $tcbrand it outputs 'Direct' but when I operate on it, it returns false and hits the otherwise block. What am I doing wrong?

    <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>

This is the xml that gets generated, which I am operating on:

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

will set the tcbrand variable to the smf:entity element. The string value of an element is the concatenation of all its descendant text nodes, which for

<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>

means a newline, seven spaces, "Direct", another newline, and another four spaces. This is not equal to "Direct". I can think of three possible fixes: you could use

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

at the top of your stylesheet, which will cause it to completely ignore all whitespace-only text nodes in the input document, you could normalize-space when doing the test:

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

or you could take the value of the child <smf:value> element instead of the parent <smf:entity> one, either at the variable declaration point

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

or at the point of the test

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

It appears that your variable tcbrand contains a document node and not a text string in your example.

I suspect that the reason it is printing Direct in your example is because this is the text content of the selected node. This will evaluate to false in the test attribute because the variable doesn't contain the literal string Direct .

To select the text value from the selected note use /text() in the variable declaration, eg

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

The use of text() is covered here in 'When should I use /text() at the end of my XPath expression?' on the site xquery.com :

You have probably seen a lot of examples that use /text() at the end of XPath expressions, and you might be wondering, Should I do the same? Are there any drawbacks?

There are cases in which you do want to append /text() to your XPath expression to force your XQuery to consider the text value of your XPath expression; the typical case is when you want to dynamically create the value of a result element; something like this:

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

… generates this output:

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

However, if what you are trying to create is actually this:

<title>La Divina Commendia</title>

… then you need to instruct XQuery to use the text value of the element, and an easy way to do that is appending /text() to the XPath expression:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM