当元素内容(serno)被其他两个元素(姓氏,名字)分组时对其进行排序

时间:2014-11-29 03:24:17

标签: xslt

如果按其他两个元素(姓氏,名字)'进行分组,请建议如何对升序中的元素内容(serno)进行排序。分组正在发生但无法对“服务”的数字值进行排序。元素内容。 (XSLT2)

XML:

<!DOCTYPE index [<!ENTITY Racute "&#x00154;"><!ENTITY racute "&#x00155;">]>
<index>
<cmindexnote>This index is cumulative for volume 65</cmindexnote>
<issue>v65n</issue>
<author><lastname>Rudra</lastname><firstname>TP</firstname>
    <refserial><serno>14</serno><serno>24</serno></refserial></author>
<author><lastname>Rudra</lastname><firstname>TP</firstname>
    <refserial><serno>4</serno></refserial></author>
<author><lastname>Rudra</lastname><firstname>AP</firstname>
    <refserial><serno>14</serno></refserial></author>
<author><lastname>Rudramuni</lastname><firstname>CP</firstname>
    <refserial><serno>14</serno></refserial></author>
<author><lastname>Rudramuni</lastname><firstname>AP</firstname>
    <refserial><serno>24</serno></refserial></author>
<author><lastname>Rud&racute;amuni</lastname><firstname>AP</firstname>
    <refserial><serno>24</serno></refserial></author>
<author><lastname>Rudramuni</lastname><firstname>AP</firstname>
    <refserial><serno>224</serno></refserial></author>
<author><lastname>Rudramuni</lastname><firstname>TP</firstname>
    <refserial><serno>10</serno></refserial></author>
<author><lastname>Rudramuni</lastname><firstname>AP</firstname>
    <refserial><serno>9</serno></refserial></author>
<author><lastname>Rudresh</lastname><firstname>TP</firstname>
    <refserial><serno>11</serno></refserial></author>
<author><lastname>&Racute;udramuni</lastname><firstname>TP</firstname>
    <refserial><serno>19</serno></refserial></author>
</index>

XSLT:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:output method="xml" indent="no" use-character-maps="chars"/>
<xsl:character-map name="chars">
    <xsl:output-character character="&#x00154;" string="&amp;Racute;"/>
    <xsl:output-character character="&#x00155;" string="&amp;racute;"/>
</xsl:character-map>
<xsl:strip-space elements="*"/>
  <xsl:template match="@*|node()">
    <xsl:copy><xsl:apply-templates select="node()|@*"/></xsl:copy>
  </xsl:template>

  <xsl:template match="index">
      <cmindexnote><xsl:value-of select="cmindexnote"/></cmindexnote>
      <issue><xsl:value-of select="issue"/></issue>
      <xsl:for-each-group select="author" group-by="concat(lastname, firstname)">
          <xsl:sort select="lastname" collation="http://saxon.sf.net/collation?lang=en&amp;ignore-modifiers=yes"/>
          <xsl:sort select="current-grouping-key()" collation="http://saxon.sf.net/collation?lang=en&amp;ignore-modifiers=yes"/>
          <!--xsl:sort select="number(../serno)"/-->
                <author>
                    <lastname><xsl:value-of select="*[1]"/></lastname>
                    <firstname><xsl:value-of select="*[2]"/></firstname>
                    <refserial>
                        <xsl:for-each-group select="current-group()" group-by="refserial">
                            <xsl:apply-templates select="current-group()">
                        <xsl:sort select="number(serno)"/>
                        </xsl:apply-templates>
                        </xsl:for-each-group>
                    </refserial>
                </author>
      </xsl:for-each-group>
  </xsl:template>

 <xsl:template match="author">
     <xsl:apply-templates select="node() except (lastname, firstname)"/>
 </xsl:template>

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

</xsl:stylesheet>

必需的OutPut:

<?xml version="1.0" encoding="UTF-8"?><cmindexnote>This index is cumulative for volume 65</cmindexnote><issue>v65n</issue>
<author><lastname>Rudra</lastname><firstname>AP</firstname>
    <refserial><serno>14</serno></refserial></author>
<author><lastname>Rudra</lastname><firstname>TP</firstname>
    <refserial><serno>4</serno><serno>14</serno><serno>24</serno></refserial></author>
<author><lastname>Rudramuni</lastname><firstname>AP</firstname>
    <refserial><serno>9</serno><serno>24</serno><serno>224</serno></refserial></author>
<author><lastname>Rud&racute;amuni</lastname><firstname>AP</firstname>
    <refserial><serno>24</serno></refserial></author>
<author><lastname>Rudramuni</lastname><firstname>CP</firstname>
    <refserial><serno>14</serno></refserial></author>
<author><lastname>Rudramuni</lastname><firstname>TP</firstname>
    <refserial><serno>10</serno></refserial></author>
<author><lastname>&Racute;udramuni</lastname><firstname>TP</firstname>
    <refserial><serno>19</serno></refserial></author>
<author><lastname>Rudresh</lastname><firstname>TP</firstname>
    <refserial><serno>11</serno></refserial></author>

1 个答案:

答案 0 :(得分:1)

这对你有用吗?

XSLT 2.0

<xsl:stylesheet version="2.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes" use-character-maps="chars"/>

<xsl:character-map name="chars">
    <xsl:output-character character="&#x00154;" string="&amp;Racute;"/>
    <xsl:output-character character="&#x00155;" string="&amp;racute;"/>
</xsl:character-map>

<xsl:template match="/index">
    <xsl:copy-of select="cmindexnote | issue"/>
    <xsl:for-each-group select="author" group-by="concat(lastname, firstname)">
        <xsl:sort select="lastname"  collation="http://saxon.sf.net/collation?lang=en&amp;ignore-modifiers=yes"/>
        <xsl:sort select="firstname" collation="http://saxon.sf.net/collation?lang=en&amp;ignore-modifiers=yes"/>
        <author>
            <xsl:copy-of select="lastname | firstname"/>
            <refserial>
                <xsl:for-each select="current-group()/refserial/serno">
                    <xsl:sort select="." data-type="number" order="ascending"/>
                    <xsl:copy-of select="."/>
                </xsl:for-each>
            </refserial>
        </author>
    </xsl:for-each-group>
</xsl:template>

</xsl:stylesheet>