借助xsl:for-each-group方法创建作者索引

时间:2014-11-14 12:38:35

标签: xslt

建议将作者和其他信息分组(排序)在分组部分中,其中具有作者姓名(fnm)的第一个字符的部分,即所有以“D”开头的名称都应列在D ....中/ p>

还有如何在fnm和snm之间插入空格('current-grouping-key()'方法的内容)。

在必需的输出文件中查看我对所需文本的评论。

XML:

<root>
<article>
    <fm>
        <title>The <i>article</i> 1</title>
        <author><fnm>Kishan</fnm><snm>TR</snm></author>
        <author><fnm>Rudramuni</fnm><snm>TP</snm></author>
        <articleInfo>
            <pii>PII:001</pii>
        </articleInfo>
    </fm>
</article>
<article>
    <fm>
        <title>The <b>article</b> 2</title>
        <author><fnm>Likhith</fnm><snm>MD</snm></author>
        <author><fnm>Kowshik</fnm><snm>MD</snm></author>
        <author><fnm>Kishan</fnm><snm>TR</snm></author>
        <articleInfo>
            <pii>PII:002</pii>
        </articleInfo>
    </fm>
</article>
<article>
    <fm>
        <title>The <bi>article</bi> 3</title>
        <author><fnm>Deepu</fnm><snm>JS</snm></author>
        <author><fnm>Kishan</fnm><snm>TR</snm></author>
        <articleInfo>
            <pii>PII:003</pii>
        </articleInfo>
    </fm>
</article>
<article>
    <fm>
        <title>The <bi>article</bi> 4</title>
        <author><fnm>Divya</fnm><snm>JS</snm></author>
        <articleInfo>
            <pii>PII:004</pii>
        </articleInfo>
    </fm>
</article>
</root>

XSLT:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"  version="2.0">
<xsl:output indent="yes"/>
 <xsl:strip-space elements="*"/>

<xsl:template match="@* | node()">
   <xsl:copy><xsl:apply-templates select="@*|node()"/></xsl:copy>
</xsl:template>

<xsl:template match="root">
   <index>
        <!--xsl:for-each-group select="article" group-by="substring(fm/author, 1,1)">
           <index-sec><title><xsl:value-of select="current-grouping-key()"/></title-->
        <xsl:for-each-group select="article" group-by="fm/author">
          <xsl:sort select="current-grouping-key()"/>
              <index-entry>
                   <index-heading><xsl:value-of select="current-grouping-key()"/></index-heading>
                   <xsl:apply-templates select="current-group()"/>
              </index-entry>
        </xsl:for-each-group>
           <!--/index-sec>
        </xsl:for-each-group-->
   </index>
</xsl:template>

<xsl:template match="title">
   <intra-ref href="{self::title/following-sibling::articleInfo/pii}"><xsl:apply-templates/></intra-ref>
</xsl:template>

<xsl:template match="articleInfo|author"/>

<xsl:template match="article|fm"><xsl:apply-templates/></xsl:template>

</xsl:stylesheet>

必需的outPut:

<?xml version="1.0" encoding="UTF-8"?>
<index>
 <index-sec><title>D</title><!--Required-->
    <index-entry>
       <index-heading>DeepuJS</index-heading><!--How to insert space between  FNM and SNM, i.e., Deep JS, like this for all Authors-->
       <intra-ref href="PII:003">The <bi>article</bi> 3</intra-ref>
    </index-entry>
    <index-entry>
       <index-heading>DivyaJS</index-heading>
       <intra-ref href="PII:004">The <bi>article</bi> 4</intra-ref>
    </index-entry>
</index-sec><!--Required-->
<index-sec><title>K</title><!--Required-->  
    <index-entry>
       <index-heading>KishanTR</index-heading>
       <intra-ref href="PII:001">The <i>article</i> 1</intra-ref>
       <intra-ref href="PII:002">The <b>article</b> 2</intra-ref>
       <intra-ref href="PII:003">The <bi>article</bi> 3</intra-ref>
    </index-entry>
    <index-entry>
       <index-heading>KowshikMD</index-heading>
       <intra-ref href="PII:002">The <b>article</b> 2</intra-ref>
    </index-entry>
</index-sec><!--Required-->
<index-sec><title>L</title><!--Required-->  
    <index-entry>
       <index-heading>LikhithMD</index-heading>
       <intra-ref href="PII:002">The <b>article</b> 2</intra-ref>
    </index-entry>
</index-sec><!--Required-->
 <index-sec><title>R</title><!--Required-->  
    <index-entry>
       <index-heading>RudramuniTP</index-heading>
       <intra-ref href="PII:001">The <i>article</i> 1</intra-ref>
    </index-entry>
</index-sec><!--Required-->
</index>

1 个答案:

答案 0 :(得分:1)

您必须将author组合两次,一次是fnm的第一个字母,然后是concat(fnm,snm)。这是我的尝试:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:output indent="yes"/>
<xsl:strip-space elements="*"/>

<xsl:template match="@* | node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="root">
    <index>
        <xsl:for-each-group select="//author" group-by="substring(fnm,1,1)">
            <xsl:sort select="current-grouping-key()"/>
            <index-sec>
                <title>
                    <xsl:value-of select="current-grouping-key()"/>
                </title>
                <xsl:for-each-group select="current-group()" group-by="concat(fnm,snm)">
                    <xsl:sort select="number(substring-after(../articleInfo/pii,':'))" data-type="number"/>
                    <index-entry>
                        <index-heading>
                            <xsl:value-of select="*"/>
                        </index-heading>
                        <xsl:apply-templates select="current-group()"/>
                    </index-entry>
                </xsl:for-each-group>
            </index-sec>
        </xsl:for-each-group>
    </index>
</xsl:template>

<xsl:template match="author">
    <intra-ref href="{../articleInfo/pii}">
        <xsl:apply-templates select="../title"/>
    </intra-ref>
</xsl:template>

<xsl:template match="title">
    <xsl:apply-templates select="@* | node()"/>
</xsl:template>

</xsl:stylesheet>