Create an empty element if it doesn't exist

Hello friends, everything is fine. I have a problem in my xml file, that is, in one set some elements exist and in the next set it does not exist, so I want to create those elements that do not exist in the set. Below is my XML file. Pls. help me.

<Jobs>
  <Job>
    <Job_ID>80000000</Job_ID> 
    <PositionID>60000002</PositionID> 
    <Title>Development Manager - Investment Banking - Equities Business</Title> 
    <Summary>An experienced Development Manager with previous experience leading a small to mid-size team of developers in a Java/J2EE environment. A hands on role, you will be expected to manage and mentor a team of developers working on a mix of greenfield and maintenance projects.&#160;&#160; My client, a well known investment bank, requires an experienced Development Manager to join their core technology team. This t</Summary> 
    <DateActive>10/6/2009</DateActive> 
    <DateExpire>11/5/2009</DateExpire> 
    <DateUpdated>10/6/2009</DateUpdated> 
    <Country>Country</Country> 
    <State>state</State> 
    <City>city</City> 
    <PostalCode>2000</PostalCode> 
    <CompanyName>Ambition Technology</CompanyName> 
    <BuilderFields /> 
    <DisplayOptions /> 
    <AddressType>6</AddressType> 
  </Job>
  <Job>
    <Job_ID>83790557</Job_ID> 
    <PositionID>61220512</PositionID> 
    <Title>SQL/VB Analyst Programmers With Strong Client Facing Skills $60 - $80K</Title> 
    <Summary>Excellent Location New Technologies Career Potential My client is a fast paced  IT company in Consultancy based in Inner West of Sydney. My client is experiencing a large amount of growth due to new exciting projects which they have won due to their impressive reputation and quality of work. Due to the large amount of growth my client is experiencing they are looking to take on&#160;3 Analyst/Programmer</Summary> 
    <DateActive>10/5/2009</DateActive> 
    <DateExpire>11/4/2009</DateExpire> 
    <DateUpdated>10/5/2009</DateUpdated> 
    <Country>Australia</Country> 
    <State>NSW</State> 
    <City>Sydney</City> 
    <PostalCode>2000</PostalCode> 
    <CompanyName>Skill Quest</CompanyName> 
    <SalMin>30000</SalMin> 
    <SalMax>70000</SalMax> 
    <SalType>Per Year</SalType> 
    <SalCurrency>AUD</SalCurrency> 
    <BuilderFields /> 
    <DisplayOptions /> 
    <AddressType>6</AddressType> 
  </Job>
</Jobs>

      

So, I want to add new elements like SalMin, SalMax, SalType and SalCurrency as empty elements if they don't exist. And this is what I want to do when using xslt transform.

0


source to share


1 answer


<xsl:template name="ApplyTemplatesOrCreate">
  <xsl:param name="elemName" select="''" />
  <xsl:param name="elemDefault" select="''" />

  <xsl:variable name="elem" select="*[name() = $elemName]" />

  <xsl:choose>
    <xsl:when test="$elem">
      <xsl:apply-templates select="$elem" />
    </xsl:when>
    <xsl:otherwise>
      <xsl:if test="$elemName != ''">
        <xsl:element name="{$elemName}">
          <xsl:value-of select="$elemDefault" />
        </xsl:element>
      </xsl:if>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

      

invoke like:



<xsl:call-template name="ApplyTemplatesOrCreate">
  <xsl:with-param name="elemName" select="'SalMin'" />
  <xsl:with-param name="elemDefault" select="'1000'" />
</xsl:call-template>

      

+3


source







All Articles