2013-07-29 87 views
0

我需要從XML文件中從下到上移除重複項,因爲我會將很多項目(元素)添加到此XML文件中,而我不會希望新價值被舊價值覆蓋。從xml文件中使用xslt從下到上移除重複的節點

在以下示例中,項目「staticproperties」和febrelease2013有兩個變量「prop1」和「prop2」。但這些變量的最新值來自於項目febrelease2013。

是否有可能總是從下到上覆制節點。

在下面的網址代碼工作正常,但它是從頂部到底部應對。

remove duplicate nodes from xml file using xsl

實施例:

<?xml version="1.0" encoding="utf-8" standalone="no"?> 
<projects> 
     <project id="staticproperties"> 
      <property name="prop1">old-value</property>  
      <property name="prop2">abc</property>    
      <property name="prop3">old-value</property>  
      <property name="prop4">def</property> 
      </project> 
     <project id="febrelease2013"> 
      <property name="prop">abcd123</property> 
      <property name="prop1">new-value</property> 
      <property name="prop3">new-value</property>     
      <property name="prop5">defg</property> 
     </project> 
</projects> 

預期輸出是:

<?xml version="1.0" encoding="utf-8" standalone="no"?> 
<projects> 
     <project id="staticproperties">    
      <property name="prop2">abc</property>  
      <property name="prop4">def</property>  
      </project> 
     <project id="febrelease2013"> 
      <property name="prop">abcd123</property> 
      <property name="prop1">new-value</property> 
      <property name="prop3">new-value</property>     
      <property name="prop5">defg</property>  
     </project> 
</projects> 

回答

1

以下應該做的伎倆:

t:\ftemp>type projects.xml 
<?xml version="1.0" encoding="utf-8" standalone="no"?> 
<projects> 
     <project id="staticproperties"> 
      <property name="prop1">old-value</property> 
      <property name="prop2">abc</property> 
      <property name="prop3">old-value</property> 
      <property name="prop4">def</property> 
      </project> 
     <project id="febrelease2013"> 
      <property name="prop">abcd123</property> 
      <property name="prop1">new-value</property> 
      <property name="prop3">new-value</property> 
      <property name="prop5">defg</property> 
     </project> 
</projects> 
t:\ftemp>xslt projects.xml projects2.xsl 
<?xml version="1.0" encoding="utf-8"?><projects> 
     <project id="staticproperties"> 

      <property name="prop2">abc</property> 

      <property name="prop4">def</property> 
      </project> 
     <project id="febrelease2013"> 
      <property name="prop">abcd123</property> 
      <property name="prop1">new-value</property> 
      <property name="prop3">new-value</property> 
      <property name="prop5">defg</property> 
     </project> 
</projects> 
t:\ftemp>type projects2.xsl 
<?xml version="1.0" encoding="US-ASCII"?> 
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
       version="1.0"> 

<xsl:template match="@*|node()" name="copy-this"> 
    <xsl:copy> 
    <xsl:apply-templates select="@*|node()"/> 
    </xsl:copy> 
</xsl:template> 

<xsl:key name="properties" match="property" use="@name"/> 

<xsl:template match="property"> 
<xsl:if test="generate-id(.)=generate-id(key('properties',@name)[last()])"> 
    <xsl:call-template name="copy-this"/> 
</xsl:if> 
</xsl:template> 

</xsl:stylesheet> 

t:\ftemp> 

代替識別該節點是關鍵表中的第一個,我將它識別爲最後一個。

+0

這工作gr8,謝謝。 – phani