locked
Rename XML child using XSLT - XSLT 1.0 RRS feed

  • Question

  • User1310825703 posted

    Hello, does anyone know how I can rename a tag using XSLT?

    In my example I copy the tag "NUMBER" from "CONTACT/NUMBER" at the Tag "ADD" to "GRP".
    But now I also want the copied Tag "NUMBER" at "GRP" to be called "CONTACT_NUMBER" instead of "NUMBER".

    <xsl:template match="GRP">
        <xsl:copy>
    <!--copy the data from ADD - CN to the GRP so it can be used in the mapping to set the delivery address from end customer-->
            <xsl:for-each select ="./ADD">
             <xsl:if test="./QUALIFIER='CN'">
              <xsl:copy-of select="PARTY_NAME_1"/>    
              <xsl:copy-of select="STREET_1"/>
              <xsl:copy-of select="CITY"/>
              <xsl:copy-of select="POSTAL_CODE"/>
              <xsl:copy-of select="COUNTRY_CODE"/>
              <xsl:copy-of select="CONTACT/NUMBER"/>
           </xsl:if>
          </xsl:for-each>
        <!--copy all other nodes-->
          <xsl:apply-templates select="@* | node()"/>
        </xsl:copy>
      </xsl:template>

    Result:

    PARTY_NAME_1
    STREET_1
    ...
    CONTACT_NUMBER (instead of "NUMBER")

    Thanks, Julian

    Wednesday, October 31, 2018 1:25 PM

Answers

  • User-893317190 posted

    Hi juls_pro_37,

    You could define a node CONTACT_NUMBER in you xslt and copy all the attributes and child nodes  of your original NUMBER node to the newly defined node.

    Below is my xml.

    <?xml version="1.0" encoding="utf-8" ?>
    <GRP>
      <ADD>
        <city>city1</city>
        <contact>
          <number attr="aaa">
            <abc name="a">
              abc
            </abc>      
      </number>
        </contact>
      </ADD>
    
    
      <ADD>
        <city>city2</city>
        <contact>
          <number attr="bbb">
            <abc name="b">
              def
            </abc>  </number>
        </contact>
      </ADD>
    
    </GRP>
    

    The xslt

    <?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="GRP">
        <xsl:copy>
          <!--copy the data from ADD - CN to the GRP so it can be used in the mapping to set the delivery address from end customer-->
          <xsl:for-each select ="./ADD">
           
              <xsl:copy-of select="city"/>
             
            <CONTACT_NUMBER>
         
              <xsl:copy-of select="contact/number/node()|contact/number/@*"/> 
            </CONTACT_NUMBER>
          </xsl:for-each>
          <!--copy all other nodes-->
        
        </xsl:copy>
      </xsl:template>
    </xsl:stylesheet>
    

    The result.

    <GRP>
       <city>city1</city>
       <CONTACT_NUMBER attr="aaa">
            <abc name="a">
              abc
            </abc>      
      </CONTACT_NUMBER>
       <city>city2</city>
       <CONTACT_NUMBER attr="bbb">
            <abc name="b">
              def
            </abc>  
       </CONTACT_NUMBER>
    </GRP>

    Best regards,

    Ackerly Xu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, November 1, 2018 3:12 AM

All replies

  • User-893317190 posted

    Hi juls_pro_37,

    You could define a node CONTACT_NUMBER in you xslt and copy all the attributes and child nodes  of your original NUMBER node to the newly defined node.

    Below is my xml.

    <?xml version="1.0" encoding="utf-8" ?>
    <GRP>
      <ADD>
        <city>city1</city>
        <contact>
          <number attr="aaa">
            <abc name="a">
              abc
            </abc>      
      </number>
        </contact>
      </ADD>
    
    
      <ADD>
        <city>city2</city>
        <contact>
          <number attr="bbb">
            <abc name="b">
              def
            </abc>  </number>
        </contact>
      </ADD>
    
    </GRP>
    

    The xslt

    <?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="GRP">
        <xsl:copy>
          <!--copy the data from ADD - CN to the GRP so it can be used in the mapping to set the delivery address from end customer-->
          <xsl:for-each select ="./ADD">
           
              <xsl:copy-of select="city"/>
             
            <CONTACT_NUMBER>
         
              <xsl:copy-of select="contact/number/node()|contact/number/@*"/> 
            </CONTACT_NUMBER>
          </xsl:for-each>
          <!--copy all other nodes-->
        
        </xsl:copy>
      </xsl:template>
    </xsl:stylesheet>
    

    The result.

    <GRP>
       <city>city1</city>
       <CONTACT_NUMBER attr="aaa">
            <abc name="a">
              abc
            </abc>      
      </CONTACT_NUMBER>
       <city>city2</city>
       <CONTACT_NUMBER attr="bbb">
            <abc name="b">
              def
            </abc>  
       </CONTACT_NUMBER>
    </GRP>

    Best regards,

    Ackerly Xu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, November 1, 2018 3:12 AM
  • User1310825703 posted

    thanks a lot!! :) it works.

    Friday, November 2, 2018 6:43 AM