用于计算特定子树中叶节点的简洁表达式 [英] Concise expression for counting leaf nodes in specific subtrees

查看:22
本文介绍了用于计算特定子树中叶节点的简洁表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我尝试回答问题XSLT - 棘手的转换时,我遇到了以下任务:><块引用>

对于每个标签 计算第二个叶子节点的数量 子标签.

将叶节点"定义为一个 标签,该标签没有其他 标签作为子标签.不过,它可能有其他标签作为孩子.

我最终得到了两个表达式:

<xsl:variable name="grandchildren_and_below" select="count(MMM[2]//MMM[count(MMM) = 0])"/>

第一个计算第二个孩子的叶子,第二个计算第二个孩子的所有祖先.我无法将这两个组合成一个表达式(除了我必须做的明显求和 :-) ).不知何故,我无法使节点数组索引选择器 [2]、附加条件 count(MMM) = 0 和祖先路径 `//' 适合相同的表达式.

那么,有没有更简洁(可能更优雅)的方式来编写这个表达式?

非常感谢!

为了测试,你可以使用这个输入文件:

<MMM><MMM><MMM><MMM/><MMM/></MMM><MMM><MMM/><MMM><MMM/></MMM></MMM></MMM><MMM><MMM/></MMM></MMM>

以及以下 XSLT 表:

<xsl:样式表版本=1.0"xmlns:xsl="http://www.w3.org/1999/XSL/Transform"><xsl:output method="xml" encoding="ISO-8859-1" indent="yes"/><xsl:template match="MMM"><xsl:variable name="just_children" select="count(MMM[position() = 2 and count(MMM) = 0])"/><xsl:variable name="grandchildren_and_below" select="count(MMM[2]//MMM[count(MMM) = 0])"/><MMM just_children="{$just_children}" grandchildren_and_below="{$grandchildren_and_below}"><xsl:apply-templates/></MMM></xsl:模板></xsl:stylesheet>

解决方案

你的 just_children 变量可以这样写:

您的 grandchildren_and_below 变量可以写成这样:

要将它们合二为一,您可以使用后代或自我轴,就像这样

In my attempt to answer question XSLT - Tricky Transformation I came across the following task:

For each tag <MMM> count the number of leaf nodes in the second <MMM> child tag.

Define "leaf node" as an <MMM> tag that does not have other <MMM> tags as children. It may have other tags as children, though.

I ended up with two expressions:

<xsl:variable name="just_children" select="count(MMM[position() = 2 and count(MMM) = 0])"/>
<xsl:variable name="grandchildren_and_below" select="count(MMM[2]//MMM[count(MMM) = 0])"/>

The first one counts the leafs in the second child, the second one in all ancestors of the second child. I was not able to combine these two into one expression (except for the obvious summing which I have to do :-) ). Somehow I cannot make the node array index selector [2], the additional condition count(MMM) = 0, and the ancestor path `//' fit into the same expression.

So, is there more a concise (and probably more elegant) way to write this expression?

Thanks a lot!

For testing you can use this input file:

<?xml version="1.0" encoding="ISO-8859-1"?>
<MMM>
  <MMM>
    <MMM>
      <MMM/>
      <MMM/>
    </MMM>    
    <MMM>
      <MMM/>
      <MMM>
        <MMM/>
      </MMM>
    </MMM>
  </MMM>
  <MMM>
    <MMM/>
  </MMM>
</MMM>

and the following XSLT sheet:

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet 
    version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <xsl:output method="xml" encoding="ISO-8859-1" indent="yes"/>

  <xsl:template match="MMM">
    <xsl:variable name="just_children" select="count(MMM[position() = 2 and count(MMM) = 0])"/>
    <xsl:variable name="grandchildren_and_below" select="count(MMM[2]//MMM[count(MMM) = 0])"/>
    <MMM just_children="{$just_children}" grandchildren_and_below="{$grandchildren_and_below}">
      <xsl:apply-templates/>
    </MMM>    
  </xsl:template>
</xsl:stylesheet>

解决方案

Your just_children variable could be written as this:

<xsl:variable name="just_children" select="count(MMM[2][not(MMM)])"/>

And your grandchildren_and_below variable could be written as this:

<xsl:variable name="grandchildren_and_below" select="count(MMM[2]//MMM[not(MMM)])"/>

To put them both together as one, you could use the descendant-or-self axes, like so

<xsl:variable name="all" select="count(MMM[2]//descendant-or-self::MMM[not(MMM)])"/>

这篇关于用于计算特定子树中叶节点的简洁表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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