如何对已加入的'进行排序xsl节点集

时间:2016-12-22 16:11:48

标签: sorting xslt xslt-1.0

我有这个(简化的)XML

-c      Indicates that added identities should be subject to confirmation
        before being used for authentication.  Confirmation is performed
        by ssh-askpass(1).  Successful confirmation is signaled by a zero
        exit status from ssh-askpass(1), rather than text entered into
        the requester.

和这个样式表

<?xml version="1.0" encoding="iso-8859-1"?>
<LENEX version="3.0">
  <MEETS>
    <MEET name="British Gas Champs 2012">
      <SESSIONS>
        <SESSION number="1" name="Session 1" course="LCM" date="2012-07-22">
          <EVENTS>
            <EVENT eventid="104" number="104" gender="M" round="PRE" order="4">
              <SWIMSTYLE distance="100" stroke="BACK" name="Boys 14 Yrs 100m Backstroke" />
              <AGEGROUPS>
                <AGEGROUP agegroupid="1" name="14 Yrs Age Group">
                  <RANKINGS>
                    <RANKING place="3" resultid="1" />
                  </RANKINGS>
                </AGEGROUP>
              </AGEGROUPS>
            </EVENT>
          </EVENTS>
        </SESSION>
        <SESSION number="2" name="Session 2" course="LCM" date="2012-07-22">
          <EVENTS>
            <EVENT eventid="207" number="207" gender="M" round="PRE" order="7">
              <SWIMSTYLE distance="100" stroke="FREE" name="Boys 14 Yrs 100m Freestyle"/>
              <AGEGROUPS>
                <AGEGROUP agegroupid="1" name="14 Yrs Age Group">
                  <RANKINGS>
                    <RANKING place="1" resultid="2"/>
                  </RANKINGS>
                </AGEGROUP>
              </AGEGROUPS>
            </EVENT>
          </EVENTS>
        </SESSION>
      </SESSIONS>
      <CLUBS>
        <CLUB name="Aberdeen ASC" region="X" type="CLUB">
          <ATHLETES>
            <ATHLETE athleteid="1169" lastname="Butt" firstname="Suleman">
              <RESULTS>
                <RESULT resultid="1" eventid="104" swimtime="00:01:01.18"/>
                <RESULT resultid="2" eventid="207" swimtime="00:00:53.06"/>
              </RESULTS>
            </ATHLETE>
          </ATHLETES>
        </CLUB>
      </CLUBS>
    </MEET>
  </MEETS>
</LENEX>

我似乎只能按主节点集排序,即RESULTS / RESULT / @resultid或RESULTS / RESULT / eventid。我想对“加入”的结果进行排序。通过EVENT / SWIMSTYLE / @ stroke,EVENT / SWIMSTYLE / @ distance和EVENT / @ round。我仅限于XSLT 1.0。我在上面的XSLT中评论了我所尝试过的内容。 我正在寻找的输出是......

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:php="http://php.net/xsl">

<xsl:variable name="session" select="/LENEX/MEETS/MEET/SESSIONS/SESSION" />
<xsl:variable name="athlete" select="/LENEX/MEETS/MEET/CLUBS/CLUB/ATHLETES/ATHLETE" />

<!-- variable for testing/debugging-->
<xsl:variable name="athlno" select="1169" />
 <!--<xsl:param name="evno" />-->

<xsl:template match="/">
    <html>
        <head>
        </head>
        <body>
             <xsl:apply-templates select="$athlete" />
        </body> 
    </html>
</xsl:template>

  <!-- find the results (@resultid) for athleteid in $athlno  -->
<xsl:template match="ATHLETE[@athleteid]">
    <xsl:choose>
    <xsl:when test="self::ATHLETE[@athleteid = $athlno]/RESULTS">
    <xsl:apply-templates select="child::RESULTS/RESULT">
      <!-- these 'sorts' works -->
   <!--   <xsl:sort select="@resultid" data-type="text" order="descending"/>
      <xsl:sort select="@eventid" order="descending"/> -->
    </xsl:apply-templates>
  </xsl:when>
    </xsl:choose>
</xsl:template>

  <!-- create $res1 $stim1 variables to pass to 'SESSIONS' node -->
  <xsl:template match="RESULT">
    <xsl:variable name="res1" ><xsl:value-of select="@resultid"/></xsl:variable>
    <xsl:variable name="stim1" ><xsl:value-of select="@swimtime"/></xsl:variable>
    <tr>
      <!--join to 'SESSIONS' node -->
    <xsl:apply-templates select="$session/EVENTS/EVENT">
   <!-- this xsl:sort doesn't work   
   <xsl:sort select="@number" data-type="text" order="descending"/>-->
      <xsl:with-param name="res2"  select="$res1"/>
      <xsl:with-param name="stim2"  select="$stim1"/>
    </xsl:apply-templates>
    </tr>
  </xsl:template>

    <xsl:template match="EVENT">
      <xsl:param name="res2" />
      <xsl:param name="stim2" />
      <xsl:for-each select="child::AGEGROUPS/AGEGROUP/RANKINGS/RANKING[@resultid = $res2]">
        <!-- this xsl:sort doesn't work   -->
        <xsl:sort select="@place" data-type="text" order="ascending"/>
        <xsl:value-of select="@place"/>
        <xsl:choose>
          <xsl:when test="../../../../@round = 'TIM'">
            <xsl:text> </xsl:text>HDW
          </xsl:when>
          <xsl:when test="../../../../@round = 'PRE'">
            <xsl:text> </xsl:text>Heat
          </xsl:when>
          <xsl:when test="../../../../@round = 'SEM'">
            <xsl:text> </xsl:text>Semi-F
          </xsl:when>
          <xsl:when test="../../../../@round = 'FIN'">
            <xsl:text> </xsl:text>Final
          </xsl:when>
          <xsl:otherwise>
            <xsl:text> </xsl:text>n/a
          </xsl:otherwise>
        </xsl:choose>
        <xsl:text> </xsl:text><xsl:value-of select="../../../../SWIMSTYLE/@name" />
        <xsl:text> </xsl:text><xsl:value-of select="substring($stim2,4)"/>
        <xsl:text> </xsl:text><xsl:value-of select="../../../../@number" /><br/>
      </xsl:for-each>
  </xsl:template>
</xsl:stylesheet>

1 个答案:

答案 0 :(得分:0)

一些键可以帮助您构建排序属性的路径:

<!-- Map from @resultid to EVENT -->
<xsl:key name="result-event" match="//EVENT" use="descendant::RANKING/@resultid"/>
<!-- Map from @resultid to RANKING -->
<xsl:key name="ranking" match="//RANKING" use="@resultid"/>

然后,在模仿ATHLETE的RESULT元素时,您可以引用排序属性:

<xsl:template match="/">
    <xsl:apply-templates select="//ATHLETE"/>
</xsl:template>

<xsl:template match="ATHLETE">
    <xsl:apply-templates select="RESULTS/RESULT">
        <xsl:sort select="key('result-event', @resultid)/SWIMSTYLE/@stroke"/>
        <xsl:sort select="key('result-event', @resultid)/SWIMSTYLE/@distance" data-type="number"/>
        <xsl:sort select="key('result-event', @resultid)/@round"/>
    </xsl:apply-templates>
</xsl:template>

<xsl:template match="RESULT">
    <xsl:value-of select="key('ranking', @resultid)/@place"/>
    <xsl:apply-templates select="key('result-event', @resultid)"/>
    <xsl:value-of select="@swimtime"/>
    <br/>
</xsl:template>

<xsl:template match="EVENT">
    <xsl:text> </xsl:text><xsl:value-of select="@round"/>
    <xsl:text> </xsl:text><xsl:value-of select="SWIMSTYLE/@name" />
    <xsl:text> </xsl:text><xsl:value-of select="../../@number" />
</xsl:template>

您可以使用查找表将@round键映射到任意代理键,从而调整轮次的排序顺序。