建议如何根据姓氏和名字对指数条目进行分组(有时候,姓氏可能会以& Racute; 等实体开头那么这些姓氏应该按照 R 系列进行排序。我的代码无法为姓氏和名字提供元素名称。 (XSLT ver 2)
根据实体进行排序,请参阅前面的建议See link for Sorting Author names even for Entities also。
输入XML:
<!DOCTYPE index [<!ENTITY Racute "Ŕ">]>
<index>
<cmindexnote>This index is cumulative for volume 65</cmindexnote>
<issue>v65n</issue>
<author>
<lastname>Rudramuni</lastname>
<firstname>TP</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>TP</firstname>
<refserial>
<serno>10</serno>
</refserial>
</author>
<author>
<lastname>Rudramuni</lastname>
<firstname>TP</firstname>
<refserial>
<serno>11</serno>
</refserial>
</author>
<author>
<lastname>Rudresh</lastname><firstname>TP</firstname>
<refserial>
<serno>11</serno>
</refserial>
</author>
<author>
<lastname>Ŕudramuni</lastname>
<firstname>TP</firstname>
<refserial>
<serno>11</serno>
</refserial>
</author>
</index>
XSLT:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:output method="xml" indent="yes" use-character-maps="chars"/>
<xsl:character-map name="chars">
<xsl:output-character character="Ŕ" string="&Racute;"/>
</xsl:character-map>
<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="current-grouping-key()" collation="http://saxon.sf.net/collation?lang=en&ignore-modifiers=yes"/>
<author><xsl:value-of select="current-grouping-key()"/>
<xsl:apply-templates select="current-group()">
<xsl:sort select="refserial/serno"/>
</xsl:apply-templates>
</author>
</xsl:for-each-group>
</xsl:template>
<xsl:template match="author">
<xsl:apply-templates select="node() except (lastname, firstname)"/>
</xsl:template>
</xsl:stylesheet>
必填结果:
<index>
<cmindexnote>This index is cumulative for volume 65</cmindexnote><issue>v65n</issue>
<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>
<refserial>
<serno>11</serno>
</refserial>
<refserial>
<serno>14</serno>
</refserial>
</author>
<author>
<lastname>Ŕudramuni</lastname><firstname>TP</firstname>
<refserial>
<serno>11</serno>
</refserial>
</author>
<author>
<lastname>Rudresh</lastname><firstname>TP</firstname>
<refserial>
<serno>11</serno>
</refserial>
</author>
<author>
</index>
答案 0 :(得分:0)
最后我得到了答案,请检查这是否正常。
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:output method="xml" indent="yes" use-character-maps="chars"/>
<xsl:include href="Unicode.xsl"/>
<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&ignore-modifiers=yes"/>
<xsl:sort select="current-grouping-key()" collation="http://saxon.sf.net/collation?lang=en&ignore-modifiers=yes"/>
<author>
<lastname><xsl:value-of select="*[1]"/></lastname>
<firstname><xsl:value-of select="*[2]"/></firstname>
<xsl:apply-templates select="current-group()">
<xsl:sort select="number(refserial/serno)"/>
</xsl:apply-templates>
</author>
</xsl:for-each-group>
</xsl:template>
<xsl:template match="author">
<xsl:apply-templates select="node() except (lastname, firstname)"/>
</xsl:template>
</xsl:stylesheet>
CharMap XSLT(Unicode.xsl):
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:character-map name="chars">
<xsl:output-character character="Ŕ" string="&Racute;"/>
</xsl:character-map>
</xsl:stylesheet>