2

I am new to XSLT working on XML to XML transformation. I want to add a element in the xml depending on the condition.

I have this employee information and requirement is to add the tag inside the element for each and every employee.

**Scenario1**
<Employee>
 <Name>Check1</Name>
 <Position>
  <org>
   <orgName>COMPANY</orgName>
   <orgType>ABC</orgTyp>
  <org>
 </Position>
</Employee>

**Scenario2**
<Employee>
 <Name>Nitesh</Name>
 <Position>
  <role>Consultant</role>
 </Position>
</Employee>

**Scenario3**
<Employee>
 <Name>Nitesh</Name>
</Employee>

I wrote the below code but it is not giving me desired output.

`

<xsl:when test="not(xs:Position)">
    <xsl:copy>
        <!-- And everything inside it -->
        <xsl:apply-templates select="@* | node()"/> 
        <!-- Add node -->
        <xs:Position>
            <xs:Organization>
                <xs:Organization_Type>1<xsl:value-of select="$OrgType"/>
                </xs:Organization_Type>
                <xs:Organization_Code>2<xsl:value-of select="$OrgCode"/>
                </xs:Organization_Code>
                <xs:Organization_Name>3<xsl:value-of select="$OrgName"/>
                </xs:Organization_Name>
            </xs:Organization>
        </xs:Position>
    </xsl:copy>
</xsl:when>
<xsl:when test="xs:Position">
    <xsl:variable name="element" select="xs:Position"/>
    <xsl:choose>
        <xsl:when test="not(xs:Position/xs:Organization/xs:Organization_Type='COMPANY')">
            <xs:Organization>
                <xs:Organization_Type>1<xsl:value-of select="$OrgType"/>
                </xs:Organization_Type>
                <xs:Organization_Code>2<xsl:value-of select="$OrgCode"/>
                </xs:Organization_Code>
                <xs:Organization_Name>3<xsl:value-of select="$OrgName"/>
                </xs:Organization_Name>
            </xs:Organization>
            <xsl:copy-of select="$element"/>
        </xsl:when>
    </xsl:choose>
</xsl:when>`
4
  • What is the expected result in each scenario? Commented Jul 24, 2016 at 10:59
  • Output like scenario 1 Commented Jul 24, 2016 at 12:13
  • "Output like scenario 1" Really? So you do not want to output the <role>Consultant</role> node in scenario #2? Commented Jul 24, 2016 at 12:45
  • Apologies if my reply was unclear and I want to output as org tag inside position tag and if in position tag any other value is already present then in output I need to add org tag and other tag should remain untouched Commented Jul 24, 2016 at 12:53

1 Answer 1

1

I did not understand your attempted code - esp. since major parts of it are missing.

I think you want to do something like:

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="*"/>

<xsl:variable name="default-org">
    <org>
        <orgName>default name</orgName>
        <orgType>default type</orgType>
    </org>
</xsl:variable>

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

<xsl:template match="Employee[not(Position)]">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
        <Position>
            <xsl:copy-of select="$default-org"/>
        </Position>     
    </xsl:copy>
</xsl:template>

<xsl:template match="Position[not(org)]">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
        <xsl:copy-of select="$default-org"/>
    </xsl:copy>
</xsl:template>

</xsl:stylesheet>

When applied to the following test input:

XML

<root>
   <Employee>
      <Name>A</Name>
      <Position>
         <org>
            <orgName>COMPANY</orgName>
            <orgType>ABC</orgType>
         </org>
      </Position>
   </Employee>
   <Employee>
      <Name>B</Name>
      <Position>
         <role>Consultant</role>
      </Position>
   </Employee>
   <Employee>
      <Name>C</Name>
   </Employee>
</root>

the result will be:

<?xml version="1.0" encoding="UTF-8"?>
<root>
   <Employee>
      <Name>A</Name>
      <Position>
         <org>
            <orgName>COMPANY</orgName>
            <orgType>ABC</orgType>
         </org>
      </Position>
   </Employee>
   <Employee>
      <Name>B</Name>
      <Position>
         <role>Consultant</role>
         <org>
            <orgName>default name</orgName>
            <orgType>default type</orgType>
         </org>
      </Position>
   </Employee>
   <Employee>
      <Name>C</Name>
      <Position>
         <org>
            <orgName>default name</orgName>
            <orgType>default type</orgType>
         </org>
      </Position>
   </Employee>
</root>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.