xml 使用 xsl 删除字符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13089002/
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
Remove characters using xsl
提问by Dinesh Sachdev 108
I need to remove the following characters from a string value using xsl 1.0
我需要使用以下方法从字符串值中删除以下字符 xsl 1.0
*, /, \, #, %, !, @, $, (, ), &
*, /, \, #, %, !, @, $, (, ), &
I have come up with the following:
我想出了以下几点:
translate(translate(translate(string(//xpath/@value),'.',''),'/',''),',','')
In the above approach, I would have to duplicate the same code many times (one time per character).
在上述方法中,我必须多次复制相同的代码(每个字符一次)。
How can I achieve the same goal without duplicating the code?
如何在不重复代码的情况下实现相同的目标?
Thanks :-)
谢谢 :-)
采纳答案by Martin Honnen
You simply need translate(//foo/@value, '*\%!@$&', '')in pure XPath respectively inside of an XML document like an XSLT stylesheet you need to escape the ampersand <xsl:value-of select="translate(//foo/@value, '*\%!@$&', '')"/>.
您只需要translate(//foo/@value, '*\%!@$&', '')在纯 XPath 中分别在 XML 文档(如 XSLT 样式表)中转义&符号<xsl:value-of select="translate(//foo/@value, '*\%!@$&', '')"/>。
回答by Dimitre Novatchev
The translate()function accepts as its second and third argument two strings-- not just two characters.
该translate()函数接受两个字符串作为它的第二个和第三个参数——而不仅仅是两个字符。
translate(., $string1, '')
produces a string which is the string value of the context (current) node in which any occurence of a character that is in $string1is deleted.
生成一个字符串,它是上下文(当前)节点的字符串值,其中$string1删除任何出现的字符。
Therefore you can use:
因此,您可以使用:
translate(expressionSelectingNode, "/\#%!@$()&", "")
to delete any of the characters contained in the second argument.
删除包含在第二个参数中的任何字符。
Of course, if the translate()function is used within an XSLT stylesheet (or, generally within an XML document), some special characters, such as <and &must be escaped respectively as <and &.
当然,如果translate()在 XSLT 样式表(或通常在 XML 文档中)中使用该函数,则某些特殊字符,例如<和&必须分别转义为<和&。
Using this is so powerful, that one can remove a set of unknowncharacters:
使用它是如此强大,以至于可以删除一组未知字符:
Imagine that you want to remove from any string all characters that are not numeric. We don't know in advance what characters would be present in the string, therefore we cannot just enumerate them in the second argument of translate(). However we can still delete all these unknown charcters like that:
想象一下,您想从任何字符串中删除所有非数字字符。我们事先不知道字符串中会出现哪些字符,因此我们不能只在 的第二个参数中枚举它们translate()。但是我们仍然可以像这样删除所有这些未知字符:
translate(., translate(., '0123456789', ''), '')
The inner translate()produces the string sans any digits.
内部translate()产生无任何数字的字符串。
The outer translate()deletes all this non-digit characters (found by the inner translate()) from the original string -- therefore what remains are only the digit characters.
外部从原始字符串中translate()删除所有这些非数字字符(由内部发现translate())——因此剩下的只是数字字符。

