tags:

views:

153

answers:

3

I am facing a problem that is in my xml file somewhere some particular xml element is exists or not exists. so i want to create the element where not exist with value 0. Below is my xml file and xslt file.

<?xml version="1.0" encoding="UTF-8" ?>
  <Jobs>
    <Job ID="84590099" PositionID="61838475">
      <Title>Graduate Developer / Junior Devloper C# or Java / SQL Skills</Title>
      <Summary><![CDATA[C# or Java / SQL skills - Graduate Programmer or 1-3 years commercial experience - Excellent opportunity to develop your career - CBD location C# / Java / SQL Server Graduate Programmer / Junior Programmer - Excellent career opportunity Due to their continued success and the launch of a new product suite , our client have a fantastic opportunity for an aspiring Junior or Graduate Developer to join]]></Summary>
      <DateActive Date="2009-11-14T10:52:44-05:00">11/14/2009</DateActive>
      <DateExpires Date="2009-12-14T17:28:07-05:00">12/14/2009</DateExpires>
      <DateUpdated Date="2009-11-14 17:28:00">11/14/2009</DateUpdated>
      <Location>
        <Country>Australia</Country>
        <State>NSW</State>
        <City>SYDNEY</City>
        <PostalCode>2000</PostalCode>
      </Location>
      <CompanyName>Randstad - Information Technology - Sydney</CompanyName>
      <DisplayOptions />
      <AddressType>6</AddressType>
    </Job>
    <Job ID="84563414" PositionID="61816706">
      <Title>SQL Server Database Administrator</Title>
      <Summary><![CDATA[Successful high profile company Interesting challenging and rewarding work Secure permanent role - career focused Our client delivers world-class mining and construction projects for some of Australia&#39;s largest and most successful companies. They are very well known and have an outstanding reputation in the market. They have an immediate requirement for an experienced SQL Server Database Administr]]></Summary>
      <DateActive Date="2009-11-13T02:31:45-05:00">11/13/2009</DateActive>
      <DateExpires Date="2009-12-13T05:13:33-05:00">12/13/2009</DateExpires>
      <DateUpdated Date="2009-11-13 05:14:00">11/13/2009</DateUpdated>
      <Location>
        <Country>Australia</Country>
        <State>WA</State>
        <City>Perth</City>
        <PostalCode>6000</PostalCode>
      </Location>
      <CompanyName>Titan Recruitment</CompanyName>
      <DisplayOptions />
      <AddressType>6</AddressType>
    </Job>
    <Job ID="84558198" PositionID="61812145">
      <Title>SQL Developer</Title>
      <Summary><![CDATA[Large Finanical Services Organisation CBD Location Contract Opportunity SQL developement and re-engineering of access database Large Financial Services Organisation CBD Location SQL Developer (preferably SQL 2005) The responsibilities of the successful Datawarehouse Developer/Analyst Programmer will include: -Participating in functional and technical design workshops -Translating functional requir]]></Summary>
      <DateActive Date="2009-11-12T22:12:31-05:00">11/12/2009</DateActive>
      <DateExpires Date="2009-12-12T22:40:34-05:00">12/12/2009</DateExpires>
      <DateUpdated Date="2009-11-12 22:41:00">11/12/2009</DateUpdated>
      <Location>
        <Country>Australia</Country>
        <State>NSW</State>
        <City>Sydney</City>
        <PostalCode>2000</PostalCode>
      </Location>
      <CompanyName>Hays Information Technology</CompanyName>
      <Salary>
        <Max Value="70000">70,000.00</Max>
        <Type ID="1">Per Year</Type>
        <Currency ID="4">AUD</Currency>
      </Salary>
      <DisplayOptions />
      <AddressType>6</AddressType>
    </Job>
    <Job ID="84558149" PositionID="61812099">
      <Title>Systems Analyst - Application Support, SQL, Access, Excel</Title>
      <Summary><![CDATA[Challenging role - add your technical abilities to the team &#160;&#160; Brisbane location, plenty of parking and public transport &#160;&#160; $60,000- $65,000 (Base) + Super+ Benefits &#160;&#160; A team primarily focused on client service, support and solutions is looking for a System Analyst to join the team as a problem solving expert, to work with other team members and build the knowledge within the team. This involves a]]></Summary>
      <DateActive Date="2009-11-12T17:58:49-05:00">11/12/2009</DateActive>
      <DateExpires Date="2009-12-12T22:24:14-05:00">12/12/2009</DateExpires>
      <DateUpdated Date="2009-11-12 22:24:00">11/12/2009</DateUpdated>
      <Location>
        <Country>Australia</Country>
        <State>QLD</State>
        <City>BRISBANE</City>
        <PostalCode>4000</PostalCode>
      </Location>
      <CompanyName>Greythorn</CompanyName>
      <DisplayOptions />
      <AddressType>6</AddressType>
    </Job>   
    <Job ID="84532073" PositionID="61791599">
      <Title>Network Support Officer</Title>
      <Summary><![CDATA[Great New Age Company! Excellent Working Culture! New Technilogies! Hays Information Technology are working with a leading Software company who are integrated with enterprise clients all across Australia! &#160; With exceptional growth and a unique market position this group are in need of an experienced Network Support Officer &#160;to assist in the implementation and support of SQL based Software on clien]]></Summary>
      <DateActive Date="2009-11-11T17:27:15-05:00">11/11/2009</DateActive>
      <DateExpires Date="2009-12-11T21:27:52-05:00">12/11/2009</DateExpires>
      <DateUpdated Date="2009-11-11 21:28:00">11/11/2009</DateUpdated>
      <Location>
        <Country>Australia</Country>
        <State>QLD</State>
        <City>Brisbane</City>
        <PostalCode>4000</PostalCode>
      </Location>
      <CompanyName>Hays Information Technology</CompanyName>
      <Salary>
        <Min Value="50000">50,000.00</Min>
        <Max Value="70000">70,000.00</Max>
        <Type ID="1">Per Year</Type>
        <Currency ID="4">AUD</Currency>
      </Salary>
      <DisplayOptions />
      <AddressType>6</AddressType>
    </Job>
    <Job ID="84529239" PositionID="61789207">
      <Title>Coldfusion Developer</Title>
      <Summary><![CDATA[&lt;p&gt;Coldfusion developer URGENTLY required by market leading SaaS company that is in expansion mode. The package for this person is amazing, great team environment, financially secure organisation with the backing of one of the largest digital australian companies. Amazing offices that have to be seen to be believed, and friendly fun environment where you will be made to feel like an important p]]></Summary>
      <DateActive Date="2009-11-11T17:34:30-05:00">11/11/2009</DateActive>
      <DateExpires Date="2009-12-11T17:34:30-05:00">12/11/2009</DateExpires>
      <DateUpdated Date="2009-11-12 03:18:00">11/12/2009</DateUpdated>
      <Location>
        <Country>Australia</Country>
        <State>NSW</State>
        <City>Sydney</City>
      </Location>
      <CompanyName>Command Recruitment Group</CompanyName>
      <Salary>
        <Min Value="51000">51,000.00</Min>
        <Max Value="80999">80,999.00</Max>
        <Type ID="1">Per Year</Type>
        <Currency ID="4">AUD</Currency>
      </Salary>
      <DisplayOptions />
      <AddressType>6</AddressType>
    </Job>   
    <Job ID="84512278" PositionID="61776246">
      <Title>C#.Net, C++ or Python Developer - Quantitative Analysis Research</Title>
      <Summary><![CDATA[Tier 1 Investment Bank Quantitative Equity Research &#160;&#160; Fantastic Entry into Quantitative Development &#160;&#160; Heavy Object Oriented Programming C#.Net and Ruby &#160;&#160; Competitive Salary Package + Uncapped Bonus &#160;&#160; My client is a globally renowned investment bank with a reputation second to none in funds management. Their I.T department works closely with the business unit offering the most stimulating and p]]></Summary>
      <DateActive Date="2009-11-11T00:47:00-05:00">11/11/2009</DateActive>
      <DateExpires Date="2009-12-11T00:49:22-05:00">12/11/2009</DateExpires>
      <DateUpdated Date="2009-11-11 00:49:00">11/11/2009</DateUpdated>
      <Location>
        <Country>Australia</Country>
        <State>NSW</State>
        <City>Sydney</City>
        <PostalCode>2000</PostalCode>
      </Location>
      <CompanyName>Vantage Recruitment</CompanyName>
      <DisplayOptions />
      <AddressType>6</AddressType>
    </Job>
    <Job ID="84512827" PositionID="61776765">
      <Title>Data warehouse Business Analyst - 6 Month contract</Title>
      <Summary><![CDATA[Business Analyst required with a strong knowledge and experience working on datawarehouse projects This business analysis role with require strong reporting experience and Business Analysis duties will revolve around converting old reporting into new reports and design new reports around TAX reporting from scratch. Strong SQL experience is required along with ability to write functional design spe]]></Summary>
      <DateActive Date="2009-11-10T23:00:57-05:00">11/10/2009</DateActive>
      <DateExpires Date="2009-12-11T02:09:33-05:00">12/11/2009</DateExpires>
      <DateUpdated Date="2009-11-11 02:10:00">11/11/2009</DateUpdated>
      <Location>
        <Country>Australia</Country>
        <State>NSW</State>
        <City>Sydney</City>
        <PostalCode>2000</PostalCode>
      </Location>
      <CompanyName>Ambition Technology</CompanyName>
      <DisplayOptions />
      <AddressType>6</AddressType>
    </Job>   
    </Jobs>

My xslt file:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt; 

  <xsl:output method="xml" indent="yes" media-type="application/xml" cdata-section-elements="Summary"/>
  <!-- default: copy everything using the identity transform --> 
  <xsl:template match="@*|node()"> 
    <xsl:copy> 
      <xsl:apply-templates select="@*|node()"/> 
    </xsl:copy> 
  </xsl:template>

  <!-- override: for Location and Salary nodes, just process the children --> 
  <xsl:template match="Location|Salary"> 
    <xsl:apply-templates select="node()"/>   
  </xsl:template> 

  <!-- override: for selected elements, convert attributes to elements --> 
  <xsl:template match="Jobs/@*|Job/@*"> 
    <xsl:element name="{name()}"> 
      <xsl:value-of select="."/> 
    </xsl:element> 
  </xsl:template> 

  <!-- override: for selected elements, remove attributes --> 
  <xsl:template match="DateActive/@*|DateExpires/@*|DateUpdated/@*"/>

  <!-- override: for selected elements, remove attributes -->
  <xsl:template match="Min/@*|Max/@*|Type/@*|Currency/@*"/>

  <!-- override: for selected elements, remove attributes -->
  <xsl:template match="Jobs/@*"/>

</xsl:stylesheet>

Help me.

+1  A: 

Your question's really not at all clear, but in general, if you want an identity transform to populate a parent element with a default child element, you have to make a template for that, e.g.:

<xsl:template match="Parent[not(Child)]">
    <xsl:copy>
        <Child>This is the default Child element</Child>
        <xsl:apply-templates select="node()|@*"/>
    </xsl:copy>
</xsl:template>

The ordinary identity transform will copy any Parent element that does have a Child child element; this one, which only matches those that don't, copies the Parent element and its content, but inserts a Child element into it.

Edit:

Now that we know a little more about the data, here's a better example. Also, this addresses the concern about creating the element in its proper order in the document:

<xsl:template match="Salary/Max[not(preceding-sibling::Min)]"/>
   <Min Value="0">0</Min>
   <xsl:copy>
      <xsl:apply-templates select="node() | @*"/>
   </xsl:copy>
</xsl:template>

That will match any Max element that's a child of Salary and that doesn't have a preceding sibling named Min, and will emit the default Min element and then a copy of the Max element.

If you wanted to be even more explicit, you could use the pattern Salary/Max[not(preceding-sibling::*[1]/name() = 'Min')], which will match any Max element whose immediately preceding sibling is not named Min.

Robert Rossney
Thanks for response, but regarding the question let me explain in salary block somewhere tag <Min> is exist and in some it is not exist. I want to create this tag where it is not exist with value 0.
naidu
@Robert: your answer inserts the missing element but don't respect its position in the element order of the document.
Erlock
+1  A: 

In this case, you must use two templates which exclude themselves mutually:

<xsl:template match="Max[not(preceding-sibling::Min)]">
  <Min Value="0">0</Min>
  <xsl:copy-of select="."/>
</xsl:template>
<xsl:template match="Max[preceding-sibling::Min]">
  <xsl:copy-of select="."/>
</xsl:template>
Erlock
Thanks for response, but it is not created where <Min><Max><Type>Currenct> tag is not exists. I need these tags in the xml file where they are not exist.
naidu
A: 

If the position of the Min tag is not important, you could change the template that matches any node so that when it matches a Job node, it inserts the missing elements at the end.

<xsl:template match="@*|node()">
   <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
      <xsl:if test="name()='Job' and not(Salary/Min)">
         <Min>0.00</Min>
      </xsl:if>
   </xsl:copy>
</xsl:template>

Alternatively, if you want the missing Min element to always appear in the same place (i.e. after the CompanyName element) you could do something like this.

<xsl:template match="@*|node()">
   <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
   </xsl:copy>
   <xsl:if test="name()='CompanyName' and not(../Salary/Min)">
      <Min>0.00</Min>
   </xsl:if>
</xsl:template>
Tim C
Thanks for your help, but it is deleteing exiting tag <Min> tag with values and replacing with <Min>0</Min>, I want if any existing <Min> exists it should not change but if it is not exist is should create
naidu
Are you sure? All my code does is append an extra Min node to the code. It does not delete anything the existing code did not.
Tim C
Yes, i tested the code, which is creating <Min> tag but even deleting the existing one and replacing with 0.00
naidu