2015-11-06 62 views
-2

我有一个xml,其中我必须将组件的属性Id更改为file1,file2,file3 .......并且还将ComponentRef更改为file1,file2,file3 .. .. 组件和组件的提示是一样的。通过XSLT递归更改属性名称

XML:

<?xml version="1.0" encoding="utf-8"?>  
 
    <Wix xmlns="http://schemas.microsoft.com/wix/2006/wi"> 
 
    <Fragment> 
 
    <DirectoryRef Id="WFA.Wire.Web.Content"> 
 
      <Component Id="cat" Guid="{39081D95}"> 
 
       <File Id="rtgfgfgfgf" > 
 
      </Component> 
 
      <Component Id="rat" Guid="{D878B422}"> 
 
       <File Id="fgftyfthfhg" /> 
 
      </Component> 
 
      <Directory Id="fhfhghjgjhj" Name="JavaScripts"> 
 
       <Component Id="goat" Guid="{66CA28E3}"> 
 
        <File Id="6576867" /> 
 
       </Component> 
 
       <Component Id="tiger" Guid="{A2465A25}"> 
 
        <File Id="97675" /> 
 
       </Component> 
 
      </Directory> 
 
      <Directory Id="fghfghf" Name="StyleSheets"> 
 
       <Component Id="cow" Guid="{4156E206}"> 
 
        <File Id="123458" /> 
 
       </Component> 
 
       <Directory Id="fhgfhg" Name="images"> 
 
        <Component Id="ring" Guid="{DC55010C}"> 
 
         <File Id="65432" /> 
 
        </Component> 
 
        <Component Id="show" Guid="{CE5CA15B}"> 
 
         <File Id="12345" /> 
 
        </Component>         
 
       </Directory> 
 
      </Directory> 
 
     </DirectoryRef> 
 
    </Fragment> 
 
    <Fragment> 
 
     <ComponentGroup Id="WFA.Wire.Web.Content"> 
 
      <ComponentRef Id="cat" /> 
 
      <ComponentRef Id="rat" /> 
 
      <ComponentRef Id="goat" /> 
 
      <ComponentRef Id="tiger" /> 
 
      <ComponentRef Id="cow" /> 
 
      <ComponentRef Id="ring" /> 
 
      <ComponentRef Id="show" />   
 
     </ComponentGroup> 
 
    </Fragment> 
 
</Wix> 

我必须将它更改为

输出:

<?xml version="1.0" encoding="utf-8"?> 
 
<Wix xmlns="http://schemas.microsoft.com/wix/2006/wi"> 
 
    <Fragment> 
 
     <DirectoryRef Id="WFA.Wire.Web.Content"> 
 
      <Component Id="file1" Guid="{39081D95}"> 
 
       <File Id="rtgfgfgfgf" > 
 
      </Component> 
 
      <Component Id="file2" Guid="{D878B422}"> 
 
       <File Id="fgftyfthfhg" /> 
 
      </Component> 
 
      <Directory Id="fhfhghjgjhj" Name="JavaScripts"> 
 
       <Component Id="file3" Guid="{66CA28E3}"> 
 
        <File Id="6576867" /> 
 
       </Component> 
 
       <Component Id="file4" Guid="{A2465A25}"> 
 
        <File Id="97675" /> 
 
       </Component> 
 
      </Directory> 
 
      <Directory Id="fghfghf" Name="StyleSheets"> 
 
       <Component Id="file5" Guid="{4156E206}"> 
 
        <File Id="123458" /> 
 
       </Component> 
 
       <Directory Id="fhgfhg" Name="images"> 
 
        <Component Id="file6" Guid="{DC55010C}"> 
 
         <File Id="65432" /> 
 
        </Component> 
 
        <Component Id="file7" Guid="{CE5CA15B}"> 
 
         <File Id="12345" /> 
 
        </Component>         
 
       </Directory> 
 
      </Directory> 
 
     </DirectoryRef> 
 
    </Fragment> 
 
    <Fragment> 
 
     <ComponentGroup Id="WFA.Wire.Web.Content"> 
 
      <ComponentRef Id="file1" /> 
 
      <ComponentRef Id="file2" /> 
 
      <ComponentRef Id="file3" /> 
 
      <ComponentRef Id="file4" /> 
 
      <ComponentRef Id="file5" /> 
 
      <ComponentRef Id="file6" /> 
 
      <ComponentRef Id="file7" />   
 
     </ComponentGroup> 
 
    </Fragment> 
 
</Wix>

回答

0

似乎很简单 - 至少在给定的例子:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
xmlns:wix="http://schemas.microsoft.com/wix/2006/wi"> 
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/> 
<xsl:strip-space elements="*"/> 

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

<!-- renumber components --> 
<xsl:template match="wix:Component/@Id" > 
    <xsl:attribute name="Id"> 
     <xsl:text>file</xsl:text> 
     <xsl:number count="wix:Component" level="any"/> 
    </xsl:attribute> 
</xsl:template> 

<!-- renumber references --> 
<xsl:template match="wix:ComponentRef/@Id"> 
    <xsl:attribute name="Id"> 
     <xsl:text>file</xsl:text> 
     <xsl:number count="wix:ComponentRef" level="any"/> 
    </xsl:attribute> 
</xsl:template> 

</xsl:stylesheet> 
+0

谢谢迈克尔,我错过了我的代码命名空间(维克斯)的一部分。感谢您的回复 –