通过XSLT递归更改属性名称

时间:2015-11-06 09:20:31

标签: xslt

我有一个xml,我必须将组件的属性Id更改为file1,file2,file3 .......以及ComponentRef到file1,file2,file3 .... Component和componentRef应该是相同的。

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

1 个答案:

答案 0 :(得分: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>