使用 XSLT 计算 XML 中字符串出现的次数
我想使用 XSLT 计算 XML 文档中特定节点中字符串出现的次数。 考虑这个例子
<mainNode>
<book>
<price> 100 </price>
<city> chennai </city>
<list>
<language> c java ruby </language>
</list>
</book>
<book>
<price> 200 </price>
<city> banglore </city>
<list>
<language> c java </language>
</list>
</book>
<book>
<price> 300 </price>
<city> delhi </city>
<list>
<language> java ruby </language>
</list>
</book>
</mainNode>
在这里我想计算“java”的出现次数
我想要这样的输出:: java -- 3
如何做到这一点???有什么想法吗???
I want to calcutae the number of occurrences of a string in a particular node in XML document using XSLT.
Consider this example
<mainNode>
<book>
<price> 100 </price>
<city> chennai </city>
<list>
<language> c java ruby </language>
</list>
</book>
<book>
<price> 200 </price>
<city> banglore </city>
<list>
<language> c java </language>
</list>
</book>
<book>
<price> 300 </price>
<city> delhi </city>
<list>
<language> java ruby </language>
</list>
</book>
</mainNode>
Here I want to count the occurrences of "java"
I Want Output like this:: java -- 3
How to do this??? any idea???
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
使用:
完整的 XSLT 转换:
应用于提供的 XML 文档时:
生成所需的正确结果 :
更新:
如果我们要计算字符串的所有出现次数 - 而不仅仅是包含该字符串的所有节点 - 以下是如何执行此操作:
当此转换应用于此 XML 文档:
想要的正确结果产生:
Use:
The complete XSLT transformation:
when applied on the provided XML document:
the wanted, correct result is produced:
Update:
If we are to count all occurences of the string -- not just all nodes that contain the string -- here's how to do it:
when this transformation is applied on this XML document:
the wanted, correct result is produced:
也许你可以尝试这个XSL模板来计算子字符串:
用法:
maybe you can try this XSL Template to count substrings:
Usage:
在
语句中尝试:count(//language[contains(concat(' ',.,' '), ' java ')])< /code>
如果您的文档结构相对静态,或者您在其他地方有名为
language
的节点用于其他目的,则可以将//language
替换为/mainNode/book/list/language
。concat
位可能看起来有点复杂,但通过确保您正在查找的文本的开头和结尾有空格,并搜索' java '
两边都有空格,就不会错误地包含恰好包含java
的其他术语,例如javascript
。如果“java”可能在一个节点中多次存在,那么您将需要使用递归模板。这是一种方法:
这实际上创建了一个由
0
组成的字符串,每个java
出现一个,然后简单地使用该字符串的长度。如果您可以选择使用 XSLT 2.0,则可以创建一个计算字符串中出现次数的函数,并使用
或类似的东西。正如我在对您的问题的评论中指出的那样,更好地设计源 XML 会有很大帮助;如果一种语言的每次出现都有它自己的元素,那么这一切都是不必要的。当然,这可能超出您的控制范围,但如果您可以选择更改它(或说服提供商更改它),我强烈推荐它。
Try this in a
<xsl:value-of>
statement:count(//language[contains(concat(' ',.,' '), ' java ')])
If your document structure is relatively static, or you have nodes called
language
elsewhere that serve another purpose, you can substitute//language
for/mainNode/book/list/language
.The
concat
bit might seem a bit convoluted, but by making sure there's a space at the beginning and end of the text you're looking in, and searching for' java '
with a space either side, you won't incorrectly include other terms that happen to includejava
, such asjavascript
.If it's possible for 'java' to exist more than once in a node, then you'll need to use a recursive template. Here's one way:
This essentially creates a string of
0
's, one for each occurrence ofjava
, and then simply uses the length of that string.If you have the option of using XSLT 2.0, you can create a function that counts the number of occurrences in a string, and use
<xsl:value-of select="sum(mycountfunction(//language))" />
or something similar.As I pointed out in a comment on your question, a better design of the source XML would have helped significantly; none of this is necessary if each occurrence of a language had it's own element. Of course that may be outside your control, but if you have the option of changing that (or persuading the provider to change it) I'd strongly recommend it.
http://www.xsltfunctions.com/xsl/functx_number-of-matches。 html
我在这里也同样回答了:查找xslt 中的字符串中出现子字符串
http://www.xsltfunctions.com/xsl/functx_number-of-matches.html
I have answered likewise here: Find the number of occurences of a substring in a string in xslt