使用在XSLT v1.0中结束

时间:2012-08-07 15:02:06

标签: xml xslt

我正在尝试编辑当前的XSLT。我想要的功能是当“// code_no”的值以01结尾时我想要编辑当前的城市位置。目前此功能不存在。 我已经尝试过使用字符串和子字符串,但是它给出了一个错误,指出功能结尾不存在。请帮忙

来自xml的值是

<code_no>
1870410001
</code_no>

在xsl中,我希望在值以01结束时打印。

<td align="left" width="33%"><SPAN style="font-size: 12pt; font-family: Arial;">
    <a> <b><u><xsl:value-of select="//city"/>, <xsl:value-of select="//state"/> 
    </u></b></a></SPAN></td>

5 个答案:

答案 0 :(得分:32)

XPath 1.0等效于(XPath 2.0)表达式:

ends-with($s, $t)

<强>是

$t = substring($s, string-length($s) - string-length($t) +1)

您只需要将最后一个XPath表达式中的$s$t替换为要测试的字符串和要测试的结尾。

以下是完整示例

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:template match="x|y">
     <xsl:value-of select="name()"/> ends-with '_01': <xsl:value-of select=
     "'_01' = substring(., string-length() - 2)"/>
=============   
 </xsl:template>
</xsl:stylesheet>

在以下XML文档上应用此转换时(问题中没有提供!!!):

<t>
 <x>abcd_01</x>
 <y>abcd_11</y>
</t>

产生了想要的正确结果:

x ends-with '_01': true
=============   
 y ends-with '_01': false
=============   

答案 1 :(得分:2)

XSLT 1.0使用XPath 1.0,其中不包含任何名为ends-with的函数。您可以使用此处的技术伪造它:

答案 2 :(得分:2)

我用过

contains($string, $part) and normalize-space(substring-after($string, $part)) = ''

哪里

我们正在检查$ string是否以$ part结尾

答案 3 :(得分:-1)

不完全结尾,但看起来比子串解决方案更好,并且仍适用于某些情况:

<xsl:template match="*[contains(name(), 'substr')]"/>

答案 4 :(得分:-1)

在特殊情况下,如果您知道标记中不能包含某些字符 例如§您可以执行以下操作:

<xsl:if test="contains(concat(code_no,'§'),'01§')">
  <td align="left" width="33%"><SPAN style="font-size: 12pt; font-family: Arial;">
    <a> <b><u><xsl:value-of select="//city"/>, <xsl:value-of select="//state"/> 
    </u></b></a></SPAN></td>
</xsl:if>