Change pattern's priority or revise the code?

社会主义新天地 提交于 2020-02-07 02:35:08

问题


Here is a code which, in fact, gives the correct output. But the question is about optimizing internal processes. They, conditionally, can be divided into 2 parts.

  • MAIN algorithm (set of codes which create relation chains)

  • DELETE pattern (removes unnecessary nodes, which do not meet the conditions)

Patterns' execution priority as i can rate on tests is 1-[MAIN] then finally 2-[DELETE]. But the thing is, the opposite order 1-[DELETE] then 2-[MAIN] would be, as i guess, a more effective solution. In this case, only the necessary nodes that form the chains would remain for MAIN-processing (so then inner additional-checking condition also will no longer be needed). But for now, MAIN algorithm is forced to do an extra, unnecessary computation. I tried to use additional priority attribute to the patterns, but it seems that it didn't change anything (because, as i understand, these two templates don't conflict with each other).

XSLT-code

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:exslt="http://exslt.org/common">
  <xsl:key name="Object-By-id" match="Object" use="@id"/>
  <xsl:key name="Object-By-parent-id" match="Object" use="string(@id-parent)"/>

  <xsl:variable name="fold-rtf">
    <xsl:apply-templates select="/" mode="fold"/>
  </xsl:variable>
  <xsl:variable name="folded-tree" select="exslt:node-set($fold-rtf)"/>

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

 <xsl:template match="Object[not(@status='0')]"/>      <!-- [DELETE] pattern -->

  <xsl:template match="Object[@status=0]/@*[last()]">  <!-- [MAIN] -->
    <xsl:variable name="current" select=".."/>
    <xsl:copy/>
    <xsl:for-each select="$folded-tree">
      <xsl:for-each select="key('Object-By-id',$current/@id)">
        <xsl:attribute name="chain-text">
          <xsl:for-each select="ancestor-or-self::*">
            <xsl:sort select="@rank"/>
            <xsl:if test="position()!=1"> | </xsl:if>
            <xsl:value-of select="concat(@text1,' ',@text2)"/>
          </xsl:for-each>
        </xsl:attribute>
        <xsl:for-each select="ancestor-or-self::*">
          <xsl:sort select="@rank"/>
          <xsl:attribute name="level-{position()}">
            <xsl:value-of select="concat(@text,' | ',@text2)"/>
          </xsl:attribute>
        </xsl:for-each>
      </xsl:for-each>
    </xsl:for-each>
  </xsl:template>

  <xsl:template match="/|*" mode="fold">
    <xsl:copy>
      <xsl:copy-of select="@*"/>
      <xsl:apply-templates select="key('Object-By-parent-id',string(@id))" mode="fold">
      </xsl:apply-templates>
    </xsl:copy>
  </xsl:template>
</xsl:stylesheet>

In sum i've got following questions:

a) In spite of the attempt, is there any kind of compulsory enforcing to change the priority of the template execution plan?

b) Or any other considerations?

here is sample bar just to demostrate how many computation cycles can be potentially saved. https://xsltfiddle.liberty-development.net/jz1Q1y5

来源:https://stackoverflow.com/questions/59921582/change-patterns-priority-or-revise-the-code

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!