加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

使用xsl计算xml节点中的单词数

发布时间:2020-12-15 23:56:50 所属栏目:百科 来源:网络整理
导读:这是示例xml文档. root node count the number of words /node/root 对于这个例子,我想计算xslt中节点“”中的单词数. 输出就像是单词数量:: 5 对此有何看法? 您的(Dimitre Novatchev)代码适用于上述xml. 您的代码是否适用于以下xml? roottest node pass p
这是示例xml文档.
<root>
  <node> count the number of words </node>
</root>

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

输出就像是单词数量:: 5

对此有何看法?

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

<root>

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

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

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

 </root>

输出如be:节点“node”中的单词总数:8

UPDATE3 ::

此代码完全适用于上述xml doc.
假设

<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>

但是您的代码会计算整个文档中的单词数.
我想只计算节点类型“node”中的单词数.
输出就像

“node”中的单词数:6
总传球:: 4
总失败:: 2

感谢名单
Sathish所在

使用这个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>

产生了想要的正确结果:

5

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

UPDATE1:

OP问道:

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

06004

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

<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随后在评论中提出:

“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”

回答:

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

<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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读