xml 在 XSLT v1.0 中使用结尾

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11848780/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 13:38:46  来源:igfitidea点击:

Use ends with in XSLT v1.0

xmlxslt

提问by user1582320

I am trying to edit a current XSLT. The functionality I want is when the value of "//code_no" ends with 01 I want to edit the current city location. Currently this functionality does not exist. I have tried using string and substring but it gives me an error saying ends with functionality does not exist. Please help

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

The value coming from the xml is

来自 xml 的值是

<code_no>
1870410001
</code_no>

in the xsl, I want to print this when the value ends with 01.

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

回答by Dimitre Novatchev

The XPath 1.0 equivalent of (the XPath 2.0) expression:

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

ends-with($s, $t)

is:

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

You need just to substitute $sand $tin the last XPath expressionwith, respectively, the string to be tested, and the ending to be tested.

你只需要以替代$s$t在过去的XPath表达式与分别要测试的字符串,并且要测试的结束。

Here is a complete example:

这是一个完整的示例

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

when this transformation is applied on the following XML document(none was provided in the question!!!):

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

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

the wanted, correct result is produced:

产生了想要的、正确的结果:

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

回答by Wayne

XSLT 1.0uses XPath 1.0, which does not include any function named ends-with. You can fake it using the technique found here:

XSLT 1.0使用XPath 1.0,它不包括任何名为ends-with. 您可以使用此处找到的技术来伪造它:

回答by Jyotsna Sonawane

I used

我用了

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

Where

在哪里

we are checking if $string ends with $part

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

回答by VladoS

In special cases, if you know that some character can not be contained in tag for example § you can do this:

在特殊情况下,如果您知道某些字符不能包含在标签中,例如 § 您可以这样做:

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

回答by CodeManX

Not exactly ends with, but nicer look than the substring-solution and still suitable for certain situations:

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

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