如果元素不存在则创建xml元素

时间:2009-11-16 06:42:20

标签: xml xslt

我遇到的问题出现在我的xml文件中某个特定的xml元素是否存在。所以我想用值0创建不存在的元素。下面是我的xml文件和xslt文件。

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

XSLT:

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

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

有人可以帮助我吗?

3 个答案:

答案 0 :(得分:3)

您的问题实际上并不清楚,但一般来说,如果您希望使用标识转换来填充具有默认子元素的父元素,则必须为其创建模板,例如:

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

普通身份转换将复制具有Parent子元素的任何Child元素;这个只与那些不匹配的元素匹配,复制Parent元素及其内容,但会在其中插入Child元素。

修改

现在我们对数据有了更多了解,这里有一个更好的例子。此外,这解决了在文档中以正确顺序创建元素的问题:

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

这将匹配任何Max元素,该元素是Salary的子元素,并且没有前面的兄弟Min,并且将发出默认的Min元素,然后是Max元素的副本。

如果您想更加明确,可以使用模式Salary/Max[not(preceding-sibling::*[1]/name() = 'Min')],该模式将匹配其前一个兄弟未命名为Max的任何Min元素。

答案 1 :(得分:1)

在这种情况下,您必须使用两个相互排除的模板:

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

答案 2 :(得分:1)

如果Min标记的位置不重要,您可以更改与任何节点匹配的模板,这样当它与Job节点匹配时,它会在末尾插入缺少的元素。

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

或者,如果您希望缺少的Min元素始终出现在同一个地方(即在CompanyName元素之后),您可以执行类似的操作。

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