简体   繁体   中英

move xml tag to another place in xml and id generation using xslt

Input XML :

<author-group>
<author>    
<given-name>aaa</given-name>    
<surname>bbb</surname>    
<orfid>a</orfid>    
<e-address type="email">abc@gmail.com</e-address>    
</author>    
<affiliation>chennai</affiliation>    
</author-group>

Output XML should be:

<contrib-group content-type="all">    
<contrib contrib-type="author">    
<name>    
<given-name>aaa</given-name>   
<surname>bbb</surname>   
</name>   
<xref ref-type="aff" rid="af1"/>  
<xref ref-type="email" rid="em1"/>   
</contrib>  
<aff id="af1">chennai</aff>  
<ext-link id="em1">abc@gmail.com</ext-link>
</contrib-group>

Can anyone help me to convert the input XML to output XML using XSLT?

Hello and welcome to Stackoverflow.

Assuming that your escaped markup is just a mistake, the following XSL will produce the exact output you asked.

However,there is no "generation" in it, as I have no idea what you meant by this. Please ask a clearer question if needed.

XML input

<?xml version="1.0" encoding="utf-8"?>

<author-group>
<author>
<given-name>aaa</given-name>
<surname>bbb</surname>
<orfid>a</orfid>
<e-address type="email">abc@gmail.com</e-address>
</author>
<affiliation>chennai</affiliation>
</author-group>

XSL stylesheet

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
>
    <xsl:output method="xml" indent="yes"/>

  <xsl:template match="author-group">
    <xsl:element name="contrib-group">
      <xsl:attribute name="content-type">
        <xsl:value-of select="'all'"/>
      </xsl:attribute>
      <xsl:element name="contrib">
        <xsl:attribute name="contrib-type">
          <xsl:value-of select="'author'"/>
        </xsl:attribute>
        <xsl:element name="name">
          <xsl:copy-of select="./author/given-name"/>
          <xsl:copy-of select="./author/surname"/>
        </xsl:element>
        <xsl:element name="xref">
          <xsl:attribute name="ref-type">
            <xsl:value-of select="'aff'"/>
          </xsl:attribute>
          <xsl:attribute name="rid">
            <xsl:value-of select="'af1'"/>
          </xsl:attribute>
        </xsl:element>
        <xsl:element name="xref">
          <xsl:attribute name="ref-type">
            <xsl:value-of select="'email'"/>
          </xsl:attribute>
          <xsl:attribute name="rid">
            <xsl:value-of select="'em1'"/>
          </xsl:attribute>
        </xsl:element>
      </xsl:element
      <xsl:element name="aff">
        <xsl:attribute name="id">
          <xsl:value-of select="'af1'"/>
        </xsl:attribute>
        <xsl:value-of select="./affiliation"/>
      </xsl:element>
      <xsl:element name="ext-link">
        <xsl:attribute name="id">
          <xsl:value-of select="'em1'"/>
        </xsl:attribute>
        <xsl:value-of select="./author/e-address"/>
      </xsl:element>
    </xsl:element>
  </xsl:template>
</xsl:stylesheet>

XML output

<?xml version="1.0" encoding="utf-8"?>
<contrib-group content-type="all">
  <contrib contrib-type="author">
    <name>
      <given-name>aaa</given-name>
      <surname>bbb</surname>
    </name>
    <xref ref-type="aff" rid="af1" />
    <xref ref-type="email" rid="em1" />
  </contrib>
  <aff id="af1">chennai</aff>
  <ext-link id="em1">abc@gmail.com</ext-link>
</contrib-group>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM