0
votes

I have an Input xml,using the below XSL Transform I am able to add deptId as a child element to 'Employees' element,but I want to add it as a child element to every 'employee' element.I am new to XSLT I tried the below transformation but not getting the required output

Input XML:

<?xml version="1.0" encoding="UTF-8"?>
<rootnode>
    <companyName>ABC</companyName>
    <deptId>12</deptId>
    <employee>
       <name>n1</name>
       <empid>e1</empid>
    </employee>
    <employee>
       <name>n2</name>
       <empid>e2</empid>
    </employee>
    <employee>
       <name>n3</name>
       <empid>e3</empid>
    </employee>
</rootnode>

XSL Transform:

<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:template match="/rootnode">
 <xsl:copy>
    <xsl:copy-of select="companyName"/>
     <xsl:copy-of select="deptId"/>

    <employees>
        <xsl:copy-of select="employee"/>
        <xsl:copy-of select="deptId"/>
    </employees>
  </xsl:copy>
 </xsl:template>

</xsl:stylesheet>

Output xml:

<?xml version="1.0" encoding="UTF-8"?>
<rootnode>
    <companyName>ABC</companyName>
    <deptId>D1</deptId>
    <employees>
        <employee>
            <name>n1</name>
            <empid>e1</empid>
        </employee>
        <employee>
            <name>n2</name>
            <empid>e2</empid>
        </employee>
        <employee>
            <name>n3</name>
            <empid>e3</empid>
        </employee>
       <deptId>D1</deptId>
   </employees>
 </rootnode>

Required Output:

<?xml version="1.0" encoding="UTF-8"?>
 <rootnode>
      <companyName>ABC</companyName>
      <deptId>12</deptId>
      <employees>
          <employee>
             <name>n1</name>
             <empid>e1</empid>
             <deptId>12</deptId>
          </employee>
          <employee>
             <name>n2</name>
             <empid>e2</empid>
             <deptId>12</deptId>
          </employee>
          <employee>
             <name>n3</name>
             <empid>e3</empid>
             <deptId>12</deptId>
          </employee>
     </employees>
</rootnode>   

I want the deptId to be copied to all the 'employee' elements under 'employees' element,could someone please help me on this.

1
Will there always be only one deptId in the entire document?michael.hor257k
Yes only one deptIdsreddy

1 Answers

1
votes

In your rootnode template match, you can do:

<xsl:template match="rootnode">
    <xsl:copy>
        <xsl:copy-of select="node()[not(self::employee)]"/>
        <employees>
            <xsl:apply-templates select="employee"/>
        </employees>
    </xsl:copy>
</xsl:template>

that is, copy the rootnode, copy all children nodes except employee. Then put all employee nodes inside employees

Then, in your employee template match, you can do:

<xsl:template match="employee">
    <xsl:copy>
        <xsl:copy-of select="node()"/>
        <xsl:copy-of select="preceding-sibling::deptId"/>
    </xsl:copy>
</xsl:template>

that is, copy the node and it's children, and copy the preceding-sibling deptId.

The whole stylesheet is below

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

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

    <xsl:template match="rootnode">
        <xsl:copy>
            <xsl:copy-of select="node()[not(self::employee)]"/>
            <employees>
                <xsl:apply-templates select="employee"/>
            </employees>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="employee">
        <xsl:copy>
            <xsl:copy-of select="node()"/>
            <xsl:copy-of select="preceding-sibling::deptId"/>
        </xsl:copy>
    </xsl:template>

</xsl:stylesheet>

See it in action https://xsltfiddle.liberty-development.net/bFDb2Dq.