基于规则集对节点进行分组

时间:2011-10-04 19:22:28

标签: xslt xpath

我有:

<node handle="full"/>
<node handle="full"/>
<node handle="left"/>
<node handle="right"/>
<node handle="top-left"/>
<node handle="top-right"/>
<node handle="bottom"/>
<node handle="full"/>
<node handle="full"/>

我需要根据以下逻辑对这些节点进行分组:

  • full应该独立。
  • left应至少分组1个,最多2个额外节点righttop-rightbottom-right
  • right应至少分组1个,最多2个额外节点lefttop-leftbottom-left
  • top-left应至少分组2个,最多3个bottom-righttop-rightbottom-rightbottom类型的节点。
  • ...

显然,如果我从left开始而following-siblingright,则该过程应重置并继续使用下一个元素。

所以输出应该如下:

<group>
    <node handle="full"/>
</group>
<group>
    <node handle="full"/>
</group>
<group>
    <node handle="left"/>
    <node handle="right"/>
</group>
<group>
    <node handle="top-left"/>
    <node handle="top-right"/>
    <node handle="bottom"/>
</group>
<group>
    <node handle="full"/>
</group>
<group>
    <node handle="full"/>
</group>

是否有一种有效的(人类和机器)方式来处理这种情况,还是应该在代码中逐案管理?

<小时/>

编辑1:

认为我可以像这样定义我的规则集,然后根据具体情况进行比较:

<xsl:variable name="layouts">
    <opt start="left" min="1" max="2">
        <allow pos="right"          value="2"/>
        <allow pos="top-right"      value="1"/>
        <allow pos="bottom-right"   value="1"/>
    </opt>
    <opt start="right" min="1" max="2">
        <allow pos="left"           value="2"/>
        <allow pos="top-left"       value="1"/>
        <allow pos="bottom-left"    value="1"/>
    </opt>
</xsl:variable>

我将使用max分数,我将从中减去添加的每个元素的value。 怎么样?

<小时/>

编辑2:

我发现在 Tim C 之前发现他的答案的解决方案 我在两者之间看到的第一个区别是我的版本限制了序列的可接受的起始元素(左,左上)。我不知道这是一件好事还是我为避免匹配已经成为序列一部分的节点而引入的限制。
无论如何,Tim的aswer 方式比我的更优雅。

<!-- 
    For each /item:

    - see it it's one of the starting points of a sequence:
        - Not "full"
        - Left, Top-Left

    - if full, just return the element
    - if not full and not a starting point, skip it, since it means it being added by the previous item.
    - if not full and either of the starting points, kick into a recursion loop in a separate template:

    - store the item's current "score" (2 or 1 for single-quadrant images)
    - recur through the following-siblings with a counter for position(), checking if they are in the allowed list, and decreasing the "score" counter.
    - every time a match is found:
        - recreate the "allow" list, minus the current match, and pass the updated list to the next iteration
        - decrease the counter
    - if the iteration completes, reaching zero, return the position() of the last matched item
    - if during the iteration, while the score is still >0, a match is not found, return false(). Our sequence is broken, we have a user error.

    - the calling template (the one matching *every* item) checks whether the returned result is >0 or false()
    - if >0 returns a copy of every node up the number specified by >0
    - if false() print out and error, suggesting possible sequences.
-->

<xsl:variable name="layouts">
    <start handle="left"            score="2">      <!-- The starting score which we'll subtract from on every iteration -->
        <allow handle="right"           value="2"/> <!-- the acceptable position which we'll check against on every iteration -->
        <allow handle="top-right"       value="1"/> <!-- the value for each position which we'll subtract from the <start> score -->
        <allow handle="bottom-right"    value="1"/>
    </start>
    <start handle="top-left"        score="3">
        <allow handle="right"           value="2"/>
        <allow handle="bottom-left"     value="1"/>
        <allow handle="top-right"       value="1"/>
        <allow handle="bottom-right"    value="1"/>
    </start>
    <start handle="full"            score="0"/> <!-- Position which are not acceptable as the start of a sequence are scored 0 -->
    <start handle="right"           score="0"/>
    <start handle="top-right"       score="0"/>
    <start handle="bottom-right"    score="0"/>
    <start handle="bottom-left"     score="0"/>
</xsl:variable>

<!-- Applied to every /item -->
<xsl:template mode="imagewraps" match="item">
    <xsl:param name="i" select="position()"/>
    <xsl:variable name="nodeName"   select="name(.)"/>
    <xsl:variable name="layout"     select="exsl:node-set($layouts)"/>
    <xsl:variable name="position"   select="position/item/@handle"/>
    <xsl:variable name="score"      select="$layout/start[@handle = $position]/@score"/>
    <xsl:variable name="allowList"  select="$layout/start[@handle = $position]"/>

    <!-- This variable will store the final result of the recursion lanunched from within.
         The returned value will be a number, indication the position of the last node that is part of the sequence -->
    <xsl:variable name="sequenceFound">
        <xsl:if test="$score > 0">
            <xsl:apply-templates mode="test" select="parent::node()/*[name() = $nodeName][$i +1]">
                <xsl:with-param name="i" select="$i +1"/>
                <xsl:with-param name="score"        select="$score"/>
                <xsl:with-param name="allowList"    select="$allowList"/>
            </xsl:apply-templates>
        </xsl:if>
    </xsl:variable>


    <div style="border: 1px solid red">
    <xsl:choose>
        <!-- If the $score is 0 and the position is 'full' just return a copy if the current node -->
        <xsl:when test="$score = 0 and $position = 'full'">
            <xsl:copy-of select="."/>
        </xsl:when>
        <!-- if the $score is greater than 0, return a copy of the current node
             and the siblings the follow, up to the value stored in $sequenceFound -->
        <xsl:when test="$score > 0">
            <xsl:choose>
                <!-- Actually do the above only if $sequenceFound didn't end up being 0
                     (it currently never does, but good to have as an option to handle errors in here) -->
                <xsl:when test="$sequenceFound != 0">
                    <xsl:copy-of select="."/>
                    <xsl:copy-of select="following-sibling::*[$sequenceFound - $i >= position()]"/>
                </xsl:when>
            </xsl:choose>
        </xsl:when>
        <!-- If the first item is wrong, let jsut say it -->
        <xsl:when test="$score = 0 and position() > 1">
            <xsl:message>The first item should either be "full", "left", "top-left".</xsl:message>
        </xsl:when>
    </xsl:choose>
    </div>
</xsl:template>

<xsl:template mode="test" match="*">
    <xsl:param name="i"/>
    <xsl:param name="score"/>
    <xsl:param name="allowList"/>
    <xsl:variable name="this" select="."/>
    <xsl:variable name="nodeName"       select="name()"/>
    <xsl:variable name="position"       select="position/item/@handle"/>
    <xsl:variable name="isInAllowList"  select="count($allowList/allow[@handle = $position]) > 0"/>
    <xsl:variable name="value">
        <xsl:if test="$isInAllowList">
            <xsl:value-of select="$allowList/allow[@handle = $position]/@value"/>
        </xsl:if>
    </xsl:variable>
    <xsl:variable name="allowListMinusMatched">
        <xsl:if test="$isInAllowList">
            <xsl:copy-of select="$allowList/allow[@handle != $position]"/>
        </xsl:if>
    </xsl:variable>

    <xsl:choose>
        <xsl:when test="$isInAllowList">
            <xsl:choose>
                <!-- if we've not ran out of loops, continue -->
                <xsl:when test="($score - $value) > 0">
                    <xsl:apply-templates mode="test" select="parent::node()/*[name() = $nodeName][$i +1]">
                        <xsl:with-param name="i" select="$i +1"/>
                        <xsl:with-param name="allowList" select="$allowListMinusMatched"/>
                        <xsl:with-param name="score" select="$score - $value"/>
                    </xsl:apply-templates>
                </xsl:when>
                <xsl:when test="($score - $value) = 0">
                    <xsl:value-of select="$i"/>
                </xsl:when>
            </xsl:choose>
        </xsl:when>
        <xsl:otherwise>
            <xsl:variable name="layout"     select="exsl:node-set($layouts)"/>
            <xsl:variable name="allowed"  select="$layout/start[@handle = $position]"/>
            <xsl:message>Bombing out. Wrong Sequence.</xsl:message>
            <xsl:message>
                Items allowed after "<xsl:value-of select="$allowed/@handle"/>" are:
                <xsl:for-each select="$allowed/allow">
                    <xsl:value-of select="@handle"/>
                    <xsl:if test="count($allowed/allow) > position()">, </xsl:if>
                    <xsl:if test="count($allowed/allow) = position()">.</xsl:if>
                </xsl:for-each>
            </xsl:message>
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>

1 个答案:

答案 0 :(得分:1)

我为此设法解决了这个问题,但我并不完全确定“最低”要求。但是,如果有帮助的话,我会发布到目前为止我所做的事情......

首先,我为每个规则定义了 xsl:key 。例如,对于规则:

<xsl:key name="left" 
 match="node[@handle='right' or @handle='top-right' or @handle='bottom-right']" 
 use="generate-id(preceding-sibling::node[@handle='left'][1])"/>

因此,在这种情况下,它匹配右上角右下角元素,并将它们链接到最前面的< strong>左元素。请注意,如果元素之间存在底部元素,则会获取所需的更多元素。这将在稍后处理......

然后,我创建了一个模板,用于匹配组中的第一个节点。最初,我使用相关的 xsl:key 定义了一个变量以包含以下节点(其中 $ maximum 是包含最大节点数的变量)

<xsl:variable name="followingNodes" 
 select="key(@handle, generate-id())[position() &lt;= $maximum]" />

但是,这会拾取中间可能存在无效元素的元素。为了阻止这种情况,我首先计算了当前节点的位置......

<xsl:variable name="position" select="count(preceding-sibling::node)"/>

然后我可以检查键中节点的位置是否等于相对于当前节点的位置,在这种情况下,它们之前没有无效元素。

<xsl:variable 
 name="followingNodes" 
 select="key(@handle, generate-id())[position() &lt;= $maximum][count(preceding-sibling::node) - $position &lt;= position()]"/>

这是完整的XSLT ......

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

   <xsl:key name="left" 
      match="node[@handle='right' or @handle='top-right' or @handle='bottom-right']" 
      use="generate-id(preceding-sibling::node[@handle='left'][1])"/>

   <xsl:key name="right" 
      match="node[@handle='left' or @handle='top-left' or @handle='bottom-left']" 
      use="generate-id(preceding-sibling::node[@handle='right'][1])"/>

   <xsl:key name="top-left" 
      match="node[@handle='bottom-right' or @handle='top-right' or @handle='bottom-right' or @handle='bottom']" 
      use="generate-id(preceding-sibling::node[@handle='top-left'][1])"/>

   <xsl:template match="nodes">
      <xsl:apply-templates select="node[1]" mode="first"/>
   </xsl:template>

   <xsl:template match="node[@handle='full']" mode="first">
      <group>
         <xsl:copy-of select="."/>
      </group>
      <xsl:apply-templates select="following-sibling::node[1]" mode="first"/>
   </xsl:template>

   <xsl:template match="node" mode="first">
      <xsl:variable name="maximum">
         <xsl:choose>
            <xsl:when test="@handle='top-left'">3</xsl:when>
            <xsl:otherwise>2</xsl:otherwise>
         </xsl:choose>
      </xsl:variable>
      <xsl:variable name="position" select="count(preceding-sibling::node)"/>
      <xsl:variable name="followingNodes" select="key(@handle, generate-id())[position() &lt;= $maximum][count(preceding-sibling::node) - $position &lt;= position()]"/>
      <group>
         <xsl:copy-of select="."/>
         <xsl:apply-templates select="$followingNodes"/>
      </group>
      <xsl:apply-templates select="following-sibling::node[count($followingNodes) + 1]" mode="first"/>
   </xsl:template>

   <xsl:template match="@*|node()">
      <xsl:copy>
         <xsl:apply-templates select="@*|node()"/>
      </xsl:copy>
   </xsl:template>
</xsl:stylesheet>

当这应用于以下输入XML ...

<nodes>
    <node handle="full"/>
    <node handle="full"/>
    <node handle="left"/>
    <node handle="right"/>
    <node handle="top-left"/>
    <node handle="top-right"/>
    <node handle="bottom"/>
    <node handle="full"/>
    <node handle="full"/>
</nodes>

输出如下:

<group>
   <node handle="full"/>
</group>
<group>
   <node handle="full"/>
</group>
<group>
   <node handle="left"/>
   <node handle="right"/>
</group>
<group>
   <node handle="top-left"/>
   <node handle="top-right"/>
   <node handle="bottom"/>
</group>
<group>
   <node handle="full"/>
</group>
<group>
   <node handle="full"/>
</group>