将xml标记值移动到xslt中根标记的底部

时间:2016-08-17 11:11:30

标签: xml xslt

下面是输入XML。

我需要将orfid代码值与affiliation标签值匹配,如果两者相同,则id应生成为af1af2af3,...

我还需要创建一个新的ID来链接mailid

<author-group>

    <author>

        <given-name>Lars</given-name>

        <surname>Dammeier</surname>

        <orfid>a</orfid>

        <e-address type="email">lars.dammeier@itp.uni-hannover.de</e-address>

    </author>

    <author>

        <given-name>Ren</given-name>

        <surname>Schwonnek</surname>

        <orfid>b</orfid>

        <e-address type="email">rene.schwonnek@itp.uni-hannover.de</e-address>

    </author>

    <affiliation><label>a</label>Trichy</affiliation>

    <affiliation><label>b</label>Chennai</affiliation>

</author-group>

使用xslt

输出xml如下所示
<contrib-group content-type="all">

    <contrib contrib-type="author">

        <name>

            <surname>Dammeier</surname>

            <given-names>Lars</given-names>

        </name>

        <xref ref-type="aff" rid="af1"/>

        <xref ref-type="aff" rid="em1"/>

    </contrib>

    <contrib contrib-type="author">

        <name>

            <surname>Schwonnek</surname>

            <given-names>Ren</given-names>

        </name>

        <xref ref-type="aff" rid="af2"/>

        <xref ref-type="aff" rid="em2"/>

    </contrib>

    <aff id="af1">Trichy</country>

    </aff>

    <aff id="af2">Chennai</country>

    </aff>

    <ext-link ext-link-type="email" id="em1">lars.dammeier@itp.uni-hannover.de</ext-link>

    <ext-link ext-link-type="email" id="em2">rene.schwonnek@itp.uni-hannover.de</ext-link>

</contrib-group>

1 个答案:

答案 0 :(得分:0)

我在输入xml中添加了一个名为root的根标记。据推测,这应该是contrib-group标签。

以下XSLT可以完成您需要的工作。

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

    <xsl:strip-space elements="*"/>
    <xsl:output indent="yes"/>

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

    <xsl:template match="author">
        <contrib>
            <name>
                <xsl:apply-templates select="surname"/>
                <xsl:apply-templates select="given-name"/>
            </name>
            <xsl:apply-templates select="orfid"/>
            <xsl:apply-templates select="e-address" mode="xref"/>
        </contrib>
    </xsl:template>

    <xsl:template match="given-name">
        <given-names>
            <xsl:apply-templates/>
        </given-names>
    </xsl:template>

    <!-- orfids a-i, will have an id of af1-af9, if affiliations are more than 10, there is a need to recode -->
    <xsl:template match="orfid">
        <xref ref-type="aff" rid="af{translate(., 'abcdefghi', '123456789')}"/>
    </xsl:template>

    <xsl:template match="e-address" mode="xref">
        <xref ref-type="aff" rid="em{count(preceding::e-address) + 1}"/>
    </xsl:template>

    <xsl:template match="e-address">
        <ext-link ext-link-type="email" id="em{count(preceding::e-address) + 1}">
            <xsl:value-of select="."/>
        </ext-link>
    </xsl:template>

    <xsl:template match="affiliation">
        <aff id="af{translate(label, 'abcdefghi', '123456789')}">
            <xsl:apply-templates select="node()[not(self::label)]"/>
        </aff>
    </xsl:template>

    <xsl:template match="root">
        <xsl:copy>
            <xsl:apply-templates/>
            <xsl:apply-templates select="descendant::e-address"/>
        </xsl:copy>
    </xsl:template>

</xsl:stylesheet>