Do XPath axes respect Xslt sorting?

孤人 提交于 2020-01-14 10:23:18

问题


If I call an xslt template like so:

  <xsl:template match="hist:Steps">
    <dgml:Links>
      <xsl:apply-templates select="hist:Step">
        <xsl:sort data-type="text" select="hist:End" order="ascending"/>
      </xsl:apply-templates>
    </dgml:Links>
  </xsl:template>

Will the following-sibling axis in the template below interrogate the document order or the sorted order?

  <xsl:template match="hist:Step">
    <xsl:if test="following-sibling::hist:Step">
      <dgml:Link>
        <xsl:attribute name="Source">
          <xsl:value-of select="hist:Workstation"/>
        </xsl:attribute>

        <xsl:attribute name="Target">
          <xsl:value-of select="following-sibling::hist:Step/hist:Workstation"/>
        </xsl:attribute>

      </dgml:Link>
    </xsl:if>
  </xsl:template>

回答1:


XSLT takes an input tree and transforms it into a result tree, your path expressions always operate on the input tree so any siblings you are looking for are navigated to in the input tree. With XSLT 2.0 (or with XSLT 1.0 and an extension function like exsl:node-set http://www.exslt.org/exsl/index.html) you can create variables with temporary trees you can then navigate in e.g.

<xsl:variable name="rtf1">
    <xsl:for-each select="hist:Step">
      <xsl:sort data-type="text" select="hist:End" order="ascending"/>
      <xsl:copy-of select="."/>
    </xsl:for-each>
</xsl:variable>
<xsl:apply-templates select="exsl:node-set($rtf1)/hist:Step"/>

would then process a temporary node-set of Step elements which have been sorted.

With XSLT 2.0 you don't need the exsl:node-set call.




回答2:


XPath axes reflect the relationships of a node in the input tree (or a temporary tree, if the node is in a temporary tree). They have nothing to do with the order of processing (the following sibling of a node is not necessarily even one of the nodes you selected for processing).

This differs from position() - it's a common mistake to think that position() tells you something about the position of the node within its tree, but it's actually the position of the node within the list of nodes selected for processing.



来源:https://stackoverflow.com/questions/5205276/do-xpath-axes-respect-xslt-sorting

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