XSLT:基于姓氏和基于CHARMAP字符对作者进行排序

时间:2014-07-22 13:39:58

标签: xslt

当使用字符映射'我的代码无法对名称进行排序。使用模块。没有CharMap,我就能得到所需的结果。

排序也应该基于CharMap角色,即' Anupam'应该是结果中的第三位作者(我的代码在最后列出了他)。请建议。 (XSLT2 vesrion)

XML:

<article>
 <aug>
    <author><surname>Akhil</surname><fnm>GH</fnm></author>
    <author><surname>Kishan</surname><fnm>TR</fnm></author>
    <author><surname>&#x000C1;nupam</surname><fnm>TP</fnm></author>
    <author><surname>Abhi</surname><fnm>TD</fnm></author>
 </aug>
</article>

XSLT:

 <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="text" use-character-maps="chars"/>
    <xsl:character-map name="chars">
        <xsl:output-character character="&#x000C1;" string="A"/>
    </xsl:character-map>
    <xsl:template match="node()|@*">
        <xsl:copy>
            <xsl:apply-templates select="node()|@*"/>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="aug">
        <aug>
        <xsl:for-each select="author">
            <xsl:sort select="surname"/>
            <au><xsl:apply-templates select="surname"/><xsl:text> </xsl:text><xsl:apply-templates select="fnm"/></au>
        </xsl:for-each>
        </aug>
    </xsl:template>
</xsl:stylesheet>

必填结果:

<article>
 <aug>
    <author><surname>Abhi</surname><fnm>TD</fnm></author>
    <author><surname>Akhil</surname><fnm>GH</fnm></author>
    <author><surname>Anupam</surname><fnm>TP</fnm></author>
    <author><surname>Kishan</surname><fnm>TR</fnm></author>
 </aug>
</article>

1 个答案:

答案 0 :(得分:1)

字符映射是一种序列化功能,序列化仅在创建结果树后的最后一步中发生。因此,您需要运行两个单独的转换,一个应用您的字符映射(例如使用标识转换)并创建结果文件,第二个转换使用结果文件并进行排序。

作为替代方案,根据您使用的XSLT处理器,例如Saxon 9,您可能需要检查使用排序规则是否解决了排序问题:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" indent="yes" use-character-maps="chars"/>
    <xsl:character-map name="chars">
        <xsl:output-character character="&#x000C1;" string="A"/>
    </xsl:character-map>
    <xsl:template match="node()|@*">
        <xsl:copy>
            <xsl:apply-templates select="node()|@*"/>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="aug">
        <aug>
        <xsl:for-each select="author">
            <xsl:sort select="surname" collation="http://saxon.sf.net/collation?lang=en&amp;ignore-modifiers=yes"/>
            <au><xsl:apply-templates select="surname"/><xsl:text> </xsl:text><xsl:apply-templates select="fnm"/></au>
        </xsl:for-each>
        </aug>
    </xsl:template>
</xsl:stylesheet>

有关详细信息,请参阅http://saxonica.com/documentation/index.html#!extensibility/collation