XSL 中的小写转换

发布于 2024-09-06 13:00:07 字数 981 浏览 2 评论 0原文

我有一个 XML,就像

<emps>
<emp id='3432'>
 <fname>Jack</fname>
 <lname>Dawson</lname>
<emp>
<emp id='1122'>
 <fname>Jack</fname>
 <lname>Thompson</lname>
<emp>
<emps>

我正在开发一个 Web 应用程序一样,该应用程序根据输入的名字搜索此 xml,并显示结果页面。为了实现这一点,我编写了一个 xslt,根据输入搜索字符串将 XML 转换为 HTML,该字符串作为名为 srchStr 的变量传递。

<xsl:template match="employees">
  <xsl:for-each select="emp[fname=$srchStr]">
<tr>
   <xsl:variable name="id">
    <xsl:value-of select="@id" />
   </xsl:variable>
   <td>
    <a href='detailSearch.do?id={$id}'>
     <xsl:value-of select="fname" />
     ,
     <xsl:value-of select="lname" />
    </a>
   </td>

  </tr>
</xsl:for-each
</xsl:template>

但用户可以输入大写或小写的名称。那么如何将xml标签fname中的名字转换为小写并进行比较呢?

有人可以在我的 xsl 中放置一段代码以使用 fn:lower-case 吗?

I have an XML like

<emps>
<emp id='3432'>
 <fname>Jack</fname>
 <lname>Dawson</lname>
<emp>
<emp id='1122'>
 <fname>Jack</fname>
 <lname>Thompson</lname>
<emp>
<emps>

I am developing a web application which searches this xml based on the first name entered and comes up with a resultant page. To achieve this I have written an xslt to transform the XML to HTML based on the input search string which is passed as a variable named srchStr.

<xsl:template match="employees">
  <xsl:for-each select="emp[fname=$srchStr]">
<tr>
   <xsl:variable name="id">
    <xsl:value-of select="@id" />
   </xsl:variable>
   <td>
    <a href='detailSearch.do?id={$id}'>
     <xsl:value-of select="fname" />
     ,
     <xsl:value-of select="lname" />
    </a>
   </td>

  </tr>
</xsl:for-each
</xsl:template>

But the user may enter the name either in upper case or lower case. So how to convert the first name inside the xml tag fname to lower case and do the comparison?

Can some one put a code snippet to use fn:lower-case inside my xsl.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

纸伞微斜 2024-09-13 13:00:07

要将字符串转换为小写或大写,您可以使用 XPath 1.0 函数 translate

首先定义小写和大写字母的字母表。注意,每对字符的位置需要相同:

<xsl:variable name="lcletters">abcdefghijklmnopqrstuvwxyz</xsl:variable>
<xsl:variable name="ucletters">ABCDEFGHIJKLMNOPQRSTUVWXYZ</xsl:variable>

然后可以转换为大写:

<xsl:value-of select="translate($toconvert,$lcletters,$ucletters)"/>

或转换为小写

<xsl:value-of select="translate($toconvert,$ucletters,$lcletters)"/>

To convert a string to lower case or uppercase you can use the XPath 1.0 function translate:

First define your alphabets for lower case and upper case letters. Note that the position of each pair of characters needs to be the same:

<xsl:variable name="lcletters">abcdefghijklmnopqrstuvwxyz</xsl:variable>
<xsl:variable name="ucletters">ABCDEFGHIJKLMNOPQRSTUVWXYZ</xsl:variable>

Then you can convert to upper case:

<xsl:value-of select="translate($toconvert,$lcletters,$ucletters)"/>

or to lower case

<xsl:value-of select="translate($toconvert,$ucletters,$lcletters)"/>
妄断弥空 2024-09-13 13:00:07

emp[lower-case(fname)=lower-case($srchStr)]

或者,如果您只有 XPath 1.0,您可以尝试使用这样的翻译:
链接

不过请注意,带有 translate 的示例不适用于带有重音符号的名称(例如我的:)

emp[lower-case(fname)=lower-case($srchStr)]

Or, if you have XPath 1.0 only, you may try using translate like here:
Link

Be warned though, the example with translate would not work on names with accents (like mine :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文