Xpath 函数去除空白 [英] Xpath function to remove white space

查看:33
本文介绍了Xpath 函数去除空白的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 XSL 和 Xpath 函数从名为 name 的 XML 属性中删除所有空格并将其设为小写.目前我有类似的东西

Im trying to use XSL and Xpath functions to remove all the white space from a XML attribute called name and make it lower case. At the moment i have something like

 <xsl:variable name="linkName">
 <xsl:value-of select="normalize-space(name)"/>
 </xsl:variable>

这会删除开头和结尾的空白,但不会删除中间的空白.有什么建议吗?

This removes the white space at the beginning and end but not the middle. Any suggestions?

处理和更改 XML 数据的最佳实践是什么,因为您似乎可以使用

What is the best practice for handling and altering XML data as it seems that you can use

<xsl:value-of select="x"/>  

直接放在 HTML 中

placed directly in the HTML

<xsl:attribute name="y">
<xsl:value-of select="x"/>

或使用

<xsl:variable name="x">

我不太确定它们之间的差异或何时应该使用它们.非常感谢任何帮助.

I'm not really sure of the differences or when each should be used. Any help is much appreciated.

盟友

推荐答案

正如 Tim Robinson 指出的那样,translate 可以解决问题.(我不会称它为黑客",但我长期以来一直处于与 XSLT 关系的认同我的折磨者阶段.)如果您使用这样的东西,您的代码将更具可读性:

As Tim Robinson points out, translate will do the trick. (I wouldn't call it a "hack," but then I've long been at the identifying-with-my-torturers stage of my relationship with XSLT.) Your code will be a lot more readable if you use something like this:

<xsl:variable name="uc" value="ABCDEFGHIJKLMNOPQRSTUVWXYZ"/>
<xsl:variable name="lc" value="abcdefghijklknopqrstuvwxyz"/>
<xsl:variable name="ws" value=" &#13;&#10;&#09"/>

...它是 globals.xslt 文件的一部分,我包含在我编写的大多数转换的顶部.那么这个:

...which is part of the globals.xslt file that I include at the top of most transforms I write. Then this:

<xsl:value-of select="translate(x, concat($uc, $ws), $lc)"/>

将每个大写字母翻译成对应的小写字母,将每个空白字符翻译成空.

translates each upper-case letter into its lower-case equivalent, and each whitespace character into nothing.

通常您使用 xsl:variable 的原因是为了使代码更具可读性(如上例所示),或者存储无法通过其他方式有效计算的中间结果.将数据输入到输出中的第四种方法是您没有提到的,它非常有用:属性值模板.所有这些都做同样的事情:

Usually the reason you use xsl:variable is to make code more readable (as in the above example), or to store intermediate results that can't otherwise be effectively calculated. A fourth way of getting data into the output is one you didn't mention, and that's pretty darned useful: the attribute value template. All of these do the same thing:

<link>
   <xsl:attribute name="name">
      <xsl:value-of select="translate(name, concat($uc, $ws), $lc)"/>
   </xsl:attribute>
</link>

<link>
    <xsl:attribute name="name" value="translate(name, concat($uc, $ws), $lc)"/>
</link>

<xsl:variable name="linkName" value="translate(name, concat($uc, $ws), $lc)"/>
<link name="{$linkName}"/>

在这种特殊情况下,后两者中哪一个更简单、更清晰是有争议的.但在大多数情况下,情况并非如此:将值的计算与它们插入输出的方式分开使两者更容易理解,就像使用 AVT 而不是更冗长的 XSLT 构造来做同样的事情一样.

In this particular case, it's arguable which of the last two is simpler and clearer. Most of the time, though, it's not: separating the calculation of values from how they get inserted into the output makes both easier to understand, as does using an AVT instead of more verbose XSLT constructs that do the same thing.

这篇关于Xpath 函数去除空白的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆