tags:

views:

500

answers:

2

Hi Friends, thanks a lot for giving me response and help. My problem is in my xml file some elements are missing in some xml set and in some other they exists. So, if these elements are not exists i want to create only elements with value 0. Earlier i got answer from the well known advisor, which i have implements but not getting the required output may be i am doing wrong implementation. Pls. help me. I want to Add new elements Like SalMin,SalMax,SalType and SalCurrency while transformation of xml file. Below follows the xml file 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 i will get a solution by implemention his suggested code, but i am doing somewhere wrong.

+1  A: 

Since you didn't post your XSL before, we couldn't tell that the example XML was actually the output of your stylesheet and not the input. The quickest and dirtiest way to fix for these required elements would be to add a check for whether the input element existed:

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

A little 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 could write a template to call, similar to Tomalak's, so you wouldn't have to duplicate the xpath expression or the 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>
Justin W
A: 

And how to do the same thing in plain XML & ASP? :-) Been searching for a solution for that.

A tag I trying to get through getElementByTag and the specified tag does not exist. Trying to find some way to give it a value or jump over it!

Any suggestions?

John Norrby