XSLT - Modify values of Element siblings based on condition

<?xml version="1.0" encoding="UTF-8"?>
<schools>
<city>Marshall</city>
<state>Maryland</state>
<highschool>
<schoolname>Marshalls</schoolname>
<department id="1">
  <deptCode>D1</deptCode>
  <deptName>Chemistry</deptName>
  <deptHead>Henry Carl</deptHead>
</department>
<department id="2">
  <deptCode>D2</deptCode>
  <deptName>Science-Physics</deptName>
  <deptHead>Martin Sean</deptHead>
</department>
<department id="3">
  <deptCode>D3</deptCode>
  <deptName>Science-Botany</deptName>
  <deptHead>Susanne Charles</deptHead>
</department>
<department id="4">
  <deptCode>D4</deptCode>
  <deptName>Science-Chemistry</deptName>
  <deptHead>Henry Carl</deptHead>
</department>
<highschool>
<schools>

From the above xml, if city is Marshal and school name is Marshalls, then check if both dept names Chemistry and Science-Chemistry exists, if true, remove the department element of Chemistry. If Science-Chemistry does not exist, then modify the Chemistry department values with DeptCode as 4 and Dept Name as Science-Chemistry and Department attribute id as 4.

Below is the XSLT I am using. I need to write the code under remDept template to remove the Dept if dept name Science Chemistry exists. And modify the Dept Code and Dept Name to D4 in template modifyDept if Science Chemistry does not exist. Can someone help me? Thanks in advance

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

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

<xsl:template match="/schools[city='Marshall' and /highschools/schoolname='Marshalls']">
   <xsl:if test="contains(deptName='Science-Chemistry')">
      <xsl:choose>
         <xsl:when test="contains(deptName='Chemistry'">
            <xsl:call-template name="remDept">
         </xsl:when>
         <xsl:otherwise>
             <xsl:call-template name="modifyDept">
         </xsl:otherwise>
      </xsl:choose>
   </xsl:if>
</xsl:template>

<xsl:template name="remDept">
// TO DO
</xsl:template>

<xsl:template name="modifyDept">
// TO DO
</xsl:template>


</xsl:stylesheet>

Upvotes: 3

Views: 1056

Answers (1)

michael.hor257k
michael.hor257k

Reputation: 116959

From the above xml, if city is Marshal and school name is Marshalls, then check if both dept names Chemistry and Science-Chemistry exists, if true, remove the department element of Chemistry. If Science-Chemistry does not exist, then modify the Chemistry department values with DeptCode as 4 and Dept Name as Science-Chemistry and Department attribute id as 4.

Perhaps I am missing something, but it seems to me this could be done (relatively) simply by:

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="department[../../city='Marshall' and ../schoolname='Marshalls' and deptName='Chemistry']">
    <xsl:if test="not(../department[deptName='Science-Chemistry'])">
        <department id="4">
            <deptCode>D4</deptCode>
            <deptName>Science-Chemistry</deptName>
            <xsl:copy-of select="deptHead"/>
        </department>
    </xsl:if>
</xsl:template>

</xsl:stylesheet>

Upvotes: 4

Related Questions