问题
Hello I want to test variable, if its possible, I want it with attribute. So I have a XML:
<tr class="alt-row">
<td class="column-country">American Samoa <span class="type">(Mobile)</span></td>
<td class="column-rate">0.500</td>
<td class="column-vat">0.575</td>
</tr>
<tr class="">
<td class="column-country">Andorra <span class="type">(Landline)</span> <span class="free">FREE*</span> <span class="superdeal">SuperDeal!**</span></td>
<td class="column-rate">FREE*</td>
<td class="column-vat">FREE*</td>
</tr>
And when td class = "column-rate" is FREE*, I need to put number 0, otherwise its normal rate. If its possible I want it with attribute / free = 1, rate = 0. or free = 0, rate = 0.500. Or in normal way :D... without free... So if column contains from FREE*, put number 0. END.
I try this:
<country>
<xsl:variable name="country" select="normalize-space(xhtml:td[@class='column-country'])"/>
<xsl:attribute name="name">
<xsl:value-of select="normalize-space(substring-before($country, '('))"/>
</xsl:attribute>
<rate>
<xsl:variable name="type1" select="normalize-space(xhtml:td[@class = 'column-country']/xhtml:span)"/>
<xsl:variable name="type" select="translate($type1, '()', '') "/>
<xsl:variable name="price" select="normalize-space(translate(xhtml:td[@class = 'column-rate'], 'abcdefghijklmnopqrstuvwzyxABCDEFGHIJKLMNOPQRSTUVVWXYZ()*¢$€', '')) "/>
<xsl:variable name="cena">
<xsl:choose>
<xsl:when test="$price != ''">
<xsl:value-of select="$price"/>
<xsl:apply-templates select="."/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$VAT"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:variable name="cena1" select="normalize-space(substring-before($cena, '(')) "/>
<xsl:variable name="cena2" select="normalize-space(translate(xhtml:td[@class = 'column-rate'], 'abcdefghijklmnopqrstuvwzyxABCDEFGHIJKLMNOPQRSTUVVWXYZ()*¢$€', '')) "/>
<xsl:attribute name="type">
<xsl:value-of select="$type"/>
</xsl:attribute>
<xsl:attribute name="operator">
</xsl:attribute>
<xsl:attribute name="currency">
<xsl:value-of select="$Currency"/>
</xsl:attribute>
<xsl:attribute name="vat">
<xsl:value-of select="$VAT"/>
</xsl:attribute>
<xsl:attribute name="unit">
<xsl:value-of select="$UNIT"/>
</xsl:attribute>
<xsl:value-of select="$cena2"/>
</rate>
</country>
and my ouput is:
<country name="American Samoa">
<rate type="Mobile" operator="" currency="EUR" vat="0" unit="minute">0.500</rate>
</country>
<country name="Andorra">
<rate type="Landline" operator="" currency="EUR" vat="0" unit="minute"/>
</country>
but i need that:
<country name="American Samoa">
<rate type="Mobile" operator="" currency="EUR" vat="0" unit="minute">0.500</rate>
</country>
<country name="Andorra">
<rate type="Landline" operator="" currency="EUR" vat="0" unit="minute"/>0</rate>
</country>
or
<country name="American Samoa">
<rate type="Mobile" operator="" currency="EUR" vat="0" unit="minute">0.500</rate>
</country>
<country name="Andorra">
<rate type="Landline" operator="" currency="EUR" vat="0" unit="minute" free="1"/>0</rate>
</country>
But its not work correctly, because when its FREE, the rate is empty.
回答1:
How about:
...
<xsl:variable name="cena">
<xsl:choose>
<xsl:when test="number($price)">
<xsl:value-of select="$price"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="0"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
...
回答2:
There is one definite and one probable error in your XSLT. The definite error is caused by comparing the variable price
to an empty string. Since the variable is derived from a translate
function call it is actually containing five spaces (from FREE*
) since translate
only replaces each character in the source character string by the corresponding character in the destination character string with space being the default if the destination string is too short. So, I would suggest that you change your test to
<xsl:when test="normalize-space($price) != ''">
However, this done, you will proably (depending on the context) run into the other error by calling <xsl:apply-templates>
. What do you need this for? I would suggest that that you simply delete it.
来源:https://stackoverflow.com/questions/20630832/i-have-problems-with-right-test-variable-with-mark-attribute