Do XPath axes respect Xslt sorting?

2019-06-25 09:28发布

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>

2条回答
仙女界的扛把子
2楼-- · 2019-06-25 09:43

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.

查看更多
Luminary・发光体
3楼-- · 2019-06-25 10:05

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.

查看更多
登录 后发表回答