使用 xsl 计算 xml 节点中的单词数 [英] count the number of words in a xml node using xsl

查看:29
本文介绍了使用 xsl 计算 xml 节点中的单词数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是示例 xml 文档.

Here is the sample xml document.

<root>
  <node> count the number of words </node>
</root>

对于这个例子,我想计算 xslt 中节点 "" 中的单词数.

For this example I want to count the number of words in the node "" in xslt.

输出为字数:: 5

对此有什么想法吗?

您的 (Dimitre Novatchev) 代码适用于上述 xml.您的代码是否适用于以下 xml?

Your (Dimitre Novatchev) code is working fine for the above xml. Is your code will work for the following xml?

<root>

<test>
   <node> pass pass </node>
</test>

  <test>
      <node> fail pass fail </node>
  </test>

  <test>
      <node> pass pass fail </node>
  </test>

 </root>

输出如下:节点node"中的单词总数:8

output like be: total number of words in the node "node": 8

更新 3::

此代码完美适用于上述 xml 文档.假设

This code perfectly working for the above xml doc. Suppose

<root>
<test>
   <node> pass pass </node>
   <a> value </a>
   <b> value </b>
</test>

  <test>
      <node> fail fail </node>
      <b> value </b>
  </test>

  <test>
      <node> pass pass</node>
      <a> value </a>
  </test>
 </root>

但是您的代码会计算整个文档中的字数.我只想计算节点类型节点"中的单词数.输出如

But yours code count the number of words in the entire document. I want to count the number of words in the node type "node" only. The output like

节点"中的单词数 :: 6总通过:4完全失败:: 2

Number of words in "node" :: 6 Total Pass:: 4 Total Fail:: 2

谢谢萨蒂什

推荐答案

使用这个 XPath 单行:

  string-length(normalize-space(node)) 
- 
  string-length(translate(normalize-space(node),' ','')) +1

这是使用 XSLT 的简短验证:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output method="text"/>

 <xsl:template match="/*">
  <xsl:value-of select=
   " string-length(normalize-space(node))
    -
     string-length(translate(normalize-space(node),' ','')) +1"/>
 </xsl:template>
</xsl:stylesheet>

当此转换应用于提供的 XML 文档时:

<root>
    <node> count the number of words </node>
</root>

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

the wanted, correct result is produced:

5

说明:标准 XPath 函数的使用 normalize-space()translate()string-length() .

Explanation: Use of the standard XPath functions normalize-space(), translate() and string-length() .

更新 1:

OP 询问:

您(Dimitre Novatchev)的代码是对上述 xml 工作正常.是您的代码适用于以下情况xml?"

"Your (Dimitre Novatchev) code is working fine for the above xml. Is your code will work for the following xml?"

<root>
  <test>
      <node> pass pass </node>
  </test>
  <test>
      <node> fail pass fail </node>
  </test>
  <test>
      <node> pass pass fail </node>
  </test>
</root>

答案:可以使用相同的方法:

Answer: The same approach can be used:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="text"/>
    <xsl:strip-space elements="*"/>

    <xsl:template match="/">
        <xsl:value-of select=
        "string-length(normalize-space(.))
        -
         string-length(translate(normalize-space(.),' ','')) +1
         "/>
    </xsl:template>
</xsl:stylesheet>

在新提供的 XML 文档(上图)上使用此转换时,会生成想要的正确答案:

8

Update2:OP 然后在评论中询问:

Update2: The OP then asked in a comment:

我可以和节点中的单词具有一些默认值单词.考虑节点包含值通过失败".我想计算通过次数和失败次数.比如pass=2 fail=1.是否可以?帮帮我男人"

"Can I have a comparision with the words in the node with some default word. Conside node contains value "pass pass fail". I want to calculate number of pass and number of fail. LIke pass=2 fail=1. is it possible? Help me man"

答案:

同样的方法也适用于这个问题的修改(不过,在一般情况下.你需要一个好的标记化——请在一个新问题中问我这个问题):

The same approach works with this modification of the problem, too (in the general case, though. you need a good tokenization -- ask me about this in a new question, please):

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="text"/>
    <xsl:strip-space elements="*"/>

    <xsl:template match="node">
        pass: <xsl:value-of select=
                "string-length()
                -
                 string-length(translate(.,'p',''))
         "/>
<xsl:text/>     fail: <xsl:value-of select=
                "string-length()
                -
                 string-length(translate(.,'f',''))
         "/>
    </xsl:template>
</xsl:stylesheet>

将此转换应用于最后一个 XML 文档(上图)时,会生成所需的正确内容:

    pass: 2     fail: 0
    pass: 1     fail: 2
    pass: 2     fail: 1

这篇关于使用 xsl 计算 xml 节点中的单词数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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