Create an empty element if it doesn't exist

Thanks a lot for your reply and help. My problem is my XML file, some elements are missing in some xml sets and some others exist. So, if these items don't exist, I only want to create items with a value of 0. Earlier I got a response from a well-known advisor that I have, but not getting the required output, maybe I am doing the wrong implementation. Pls. Help me. I want to add new elements like SalMin, SalMax, SalType and SalCurrency when converting an xml file. Following is the xml and xslt file.

<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> 

      

<?xml version="1.0" ?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
  <xsl:output method="xml" indent="yes" encoding="utf-8"
  omit-xml-declaration="no" />
  <xsl:template match="/">
    <Monster>
      <xsl:for-each select="Monster/Jobs">
        <Jobs>

          <!--<Found>
            <xsl:value-of select="@Found" />
          </Found>
          <Returned>
            <xsl:value-of select="@Returned" />
          </Returned>-->
          <xsl:for-each select="Job">
            <Job>
            <Job_ID>
              <xsl:value-of select="@ID" />
            </Job_ID>
            <PositionID>
              <xsl:value-of select="@PositionID" />
            </PositionID>
            <xsl:for-each select="Title">
              <Title>
                <xsl:apply-templates/>
              </Title>
            </xsl:for-each >
            <xsl:for-each select="Summary">
              <Summary>
                <xsl:apply-templates/>
              </Summary>
            </xsl:for-each >
            <xsl:for-each select="DateActive">
              <DateActive>
                <xsl:apply-templates/>
              </DateActive>
            </xsl:for-each >
            <xsl:for-each select="DateExpires">
              <DateExpire>
                <xsl:apply-templates/>
              </DateExpire>
            </xsl:for-each >
            <xsl:for-each select="DateUpdated">
              <DateUpdated>
                <xsl:apply-templates/>
              </DateUpdated>
            </xsl:for-each >
            <xsl:for-each select="Location/Country">
              <Country>
                <xsl:apply-templates/>
              </Country>
            </xsl:for-each >
            <xsl:for-each select="Location/State">
              <State>
                <xsl:apply-templates/>
              </State>
            </xsl:for-each>
            <xsl:for-each select="Location/City">
              <City>
                <xsl:apply-templates/>
              </City>
            </xsl:for-each>
            <xsl:for-each select="Location/PostalCode">
              <PostalCode>
                <xsl:apply-templates/>
              </PostalCode>
            </xsl:for-each>
            <xsl:for-each select="CompanyName">
              <CompanyName>
                <xsl:apply-templates/>
              </CompanyName>
            </xsl:for-each>

  <xsl:for-each select="Salary/Min">             
                <SalMin>
                  <xsl:value-of select="@Value" />
                </SalMin>                          
            </xsl:for-each >
            <xsl:for-each select="Salary/Max">
              <SalMax>
                <xsl:value-of select="@Value" />
              </SalMax>
              </xsl:for-each >
            <xsl:for-each select="Salary/Type">
              <SalType>
                <xsl:apply-templates/>
              </SalType>
            </xsl:for-each >
            <xsl:for-each select="Salary/Currency">
              <SalCurrency>
                <xsl:apply-templates/>
              </SalCurrency>
            </xsl:for-each >
            <xsl:for-each select="BuilderFields">
              <BuilderFields>
                <xsl:apply-templates/>
              </BuilderFields>
            </xsl:for-each >
            <xsl:for-each select="DisplayOptions">
              <DisplayOptions>
                <xsl:apply-templates/>
              </DisplayOptions>
            </xsl:for-each >
            <xsl:for-each select="AddressType">
              <AddressType>
                <xsl:apply-templates/>
              </AddressType>
            </xsl:for-each >
              <xsl:call-template name="ApplyTemplatesOrCreate">
                <xsl:with-param name="elemName" select="'SalMin'" />
                <xsl:with-param name="elemDefault" select="'0'" />
              </xsl:call-template>
            </Job>
            <!--Closing-->
          </xsl:for-each >
        </Jobs >
      </xsl:for-each >
    </Monster>
  </xsl:template>




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

    <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>

</xsl:stylesheet >

      

Pls. Get me a solution, earlier Mr. Tomalak helped me a lot, even I told him that I would get a solution by running his suggested code, but I am doing something wrong.

+2


source to share


1 answer


Since you haven't submitted your XSL before, we couldn't tell that the XML example was actually the result of your stylesheet, not input. The quickest and dirtiest way to fix these required elements is to add a check for the presence of an input element:

        <xsl:for-each select="Salary/Max">
          <SalMax>
            <xsl:value-of select="@Value" />
          </SalMax>
        </xsl:for-each >
        <xsl:if test="not(Salary/Max/@Value)">
          <SalMax>0</SalMax>
        </xsl:if>

      

Slightly better would be to output the required element and then do the check inside:

      <SalMax>
        <xsl:value-of select="Salary/Max/@Value" />
        <xsl:if test="not(Salary/Max/@Value)">0</xsl:if>
      </SalMax>

      <SalType>
        <xsl:apply-templates select="Salary/Type" />
        <xsl:if test="not(Salary/Type)">NO_TYPE</xsl:if>
      </SalType>

      



You can write a call pattern similar to Tomalak's, so you don't have to duplicate xpath expression or logic:

    <SalType>
        <xsl:call-template name="ApplyTemplatesOrDefault">
            <xsl:with-param name="elem" select="Salary/Type"/>
            <xsl:with-param name="default">NO_TYPE</xsl:with-param>
        </xsl:call-template>
    </SalType>

      

...

    <xsl:template name="ApplyTemplatesOrDefault">
        <xsl:param name="elem" />
        <xsl:param name="default" select="0"/>

        <xsl:choose>
            <xsl:when test="$elem">
                <xsl:apply-templates select="$elem" />
            </xsl:when>
            <xsl:otherwise>
                <xsl:value-of select="$default" />
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>

      

+5


source







All Articles