Marshall

XSLT - modifying the values ​​of member elements based on a 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 the city is marshal and the school name is Marshalls then check if both chemistry and chemistry science exist, if true, remove the chemistry department element. If Science-Chemistry does not exist, change the chemistry department values ​​with DeptCode as 4 and Dept Name as the Science-Chemistry attribute identifier and Department as 4.

Below is the XSLT I am using. I need to write code under remDept template to remove Dept if Science Chemistry name exists. And change Dept Code and Dept Name to D4 in modifyDept template if Science Chemistry doesn't exist. Can anyone 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>

      

+3


source to share


1 answer


From the above xml, if the city is marshal and the school name is Marshalls, then check if both chemistry and chemistry science exist, if true, remove the chemistry department element. If Science-Chemistry does not exist, then modify the chemistry department value with DeptCode as 4 and Dept Name as Science-Chemistry and Department Attribute ID as 4.

Maybe I'm missing something, but it seems to me that it can be done (relatively) simply:



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>

      

+4


source







All Articles