Home > Enterprise >  Need to use xslt transformation for xml file
Need to use xslt transformation for xml file

Time:03-29

I am very new to xslt and after multiple retries I am not able to proceed from here. The current xslt I have prepared is :

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" omit-xml-declaration="yes" indent="yes" />
<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>
<xsl:template match="action">
<action> 
    <xsl:apply-templates select="@*|node()"/>
   <attribute name="phase" value="new" />
</action>    
</xsl:template>
</xsl:stylesheet>

which changes this input xml :

<root>
<attribute name="phases">
<tag>
  <entryMap name="startphase">
    <action>
      <type>Some type</type>
      <attribute name="first" value="abc" />
    </action>
  </entryMap>
</tag>
</attribute>
</root>

to this output xml (adds <attribute name="phase" value="new"/> inside <action> and after <type>):

<root>
<attribute name="phases">
  <tag>
     <entryMap name="startphase">
        <action>
           <type>Some type</type>
           <attribute name="first" value="abc"/>
           <attribute name="phase" value="new"/>
        </action>
     </entryMap>
  </tag>
</attribute>
</root>

My xml file can change and have multiple phases and so I need to make changes in the xslt file to transform this example input xml :

<root>
<attribute name="phases">
    <tag>
      <entryMap name="startphase">
        <action>
          <type>Some type</type>
          <attribute name="first" value="abc" />
        </action>
      </entryMap>
    </tag>
</attribute>
<attribute name="phases">
    <tag>
      <entryMap name="midphase">
        <action>
          <type>Some other type</type>
          <attribute name="second" value="def" />
        </action>
      </entryMap>
    </tag>
</attribute>
<attribute name="phases">
    <tag>
      <entryMap name="endphase">
        <action>
          <type>Other type</type>
          <attribute name="third" value="ghi" />
        </action>
      </entryMap>
    </tag>
</attribute>
</root>

to this output file :

<root>
   <attribute name="phases">
      <tag>
         <entryMap name="startphase">
            <action>
               <type>Some type</type>
               <attribute name="first" value="abc"/>
               <attribute name="phase" value="startphase"/>
            </action>
         </entryMap>
      </tag>
   </attribute>
   <attribute name="phases">
      <tag>
         <entryMap name="midphase">
            <action>
               <type>Some other type</type>
               <attribute name="second" value="def"/>
               <attribute name="phase" value="midphase"/>
            </action>
         </entryMap>
      </tag>
   </attribute>
   <attribute name="phases">
      <tag>
         <entryMap name="endphase">
            <action>
               <type>Other type</type>
               <attribute name="third" value="ghi"/>
               <attribute name="phase" value="endphase"/>
            </action>
         </entryMap>
      </tag>
   </attribute>
</root>

That is, I need to add <attribute name="phase" value="entryMap name value"/>to every phase and the value attribute should contain the value of entryMap name attribute value.

I have tried and gone through many links and tutorials but I am still not able to achieve this. I will be very, very grateful if someone could help me with this. Thank you in advance!

CodePudding user response:

How about:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

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

<xsl:template match="action">
    <xsl:copy>
        <xsl:copy-of select="*"/>
        <attribute name="phase" value="{../@name}"/>
    </xsl:copy>
</xsl:template>

</xsl:stylesheet>

To understand how this works, read about attribute value templates and abbreviated syntax.


Added:

is there any way by which the <attribute name="phase" value="entryMap name value"/> is added right after the <type></type> tag?

You could do it this way:

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

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

<xsl:template match="type">
    <xsl:copy-of select="."/>
    <attribute name="phase" value="{../../@name}"/>
</xsl:template>

</xsl:stylesheet>
  • Related