如果不存在则创建空元素

时间:2009-10-13 09:57:58

标签: xml xslt

嗨朋友们希望一切顺利。我的xml文件有问题,即在一组中存在某些元素,在下一组中它不存在,所以我想创建那些在集合中不存在的元素。下面是我的xml文件。 PLS。帮助我。

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

所以,我想添加像SalMin,SalMax,SalT​​ype和SalCurrency这样的新元素作为空元素,如果它们不存在的话。我想在使用xslt转换时这样做。

1 个答案:

答案 0 :(得分:3)

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

致电:

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