2016-08-17 81 views
0

下面是输入XML。将xml标记值移至xslt中根标记的底部

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

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

<author-group> 

    <author> 

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

     <surname>Dammeier</surname> 

     <orfid>a</orfid> 

     <e-address type="email">[email protected]</e-address> 

    </author> 

    <author> 

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

     <surname>Schwonnek</surname> 

     <orfid>b</orfid> 

     <e-address type="email">[email protected]</e-address> 

    </author> 

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

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

</author-group> 

输出XML将如下使用XSLT

<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">[email protected]</ext-link> 

    <ext-link ext-link-type="email" id="em2">[email protected]</ext-link> 

</contrib-group> 
+0

** 1。**你究竟在哪里卡住了这个?发布您的尝试,以便我们可以修复它,而不是从头开始为您编写代码。 - ** 2。**“rid”值从哪里来? –

+0

rid应该自动生成为af1,af2,af3,...这样,如果orfid的值与任何从属/标签匹配。和em1,em2一样...如果电子邮件ID代表作者 –

回答

0

我已经添加了一个名为root您输入的XML根标签。据推测,这应该是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> 
+0

感谢joel。它工作正常 –