Dbl
Dbl

Reputation: 5894

How to rewrite an element's attribute value using the current attribute value?

I've got an almost working XSLT sample, but for some reason it adds the attribute to the parent instead of rewriting the node I am trying to rewrite.

Input XML

<Include xmlns="http://schemas.microsoft.com/wix/2006/wi">
    <Fragment>
        <DirectoryRef Id="WEBFOLDER">
            <Directory Id="dirA82847423D2E0E6780E69FEDB5941AC0" Name="web">
                <Component Id="cmpADE52C2B19915F5AFB7C41166996B6C5" Guid="*">
                    <File Id="fil83688EC53AE556DE24B5F5444F16F6BE" KeyPath="yes" Source="$(var.SolutionDir)\Company.PCR.Blazor.dll" />
                </Component>
            </Directory>
        </DirectoryRef>
    </Fragment>
</Include>

My XSL (transformation)

<!-- patch relative paths -->
<xsl:template match="wix:File">
    <xsl:attribute name="Source">
        <xsl:value-of select="concat(substring(self::node()/@Source,0,19), '..\artifacts\msi\web\', substring(self::node()/@Source, 20))"/>
    </xsl:attribute>
</xsl:template>

Actual output (currently transformed)

<Include xmlns="http://schemas.microsoft.com/wix/2006/wi">
   <Fragment>
      <DirectoryRef Id="WEBFOLDER">
         <Directory Id="dirA82847423D2E0E6780E69FEDB5941AC0" Name="web">
            <Component Id="cmpADE52C2B19915F5AFB7C41166996B6C5" Guid="*" Source="$(var.SolutionDir)..\artifacts\msi\web\Company.PCR.Blazor.dll"/>
         </Directory>
      </DirectoryRef>
   </Fragment>
</Include>

Expected XML output (transformed):

<Include xmlns="http://schemas.microsoft.com/wix/2006/wi">
    <Fragment>
        <DirectoryRef Id="WEBFOLDER">
            <Directory Id="dirA82847423D2E0E6780E69FEDB5941AC0" Name="web">
                <Component Id="cmpADE52C2B19915F5AFB7C41166996B6C5" Guid="*">
                    <File Id="fil83688EC53AE556DE24B5F5444F16F6BE" KeyPath="yes" Source="$(var.SolutionDir)..\artifacts\msi\web\Company.PCR.Blazor.dll" />
                </Component>
         </Directory>
      </DirectoryRef>
   </Fragment>
</Include>

Can you explain why my match="wix:File" applies its attribute change to its parent?

Upvotes: 1

Views: 55

Answers (1)

michael.hor257k
michael.hor257k

Reputation: 116992

If you want to modify the Source attribute, then why not make the template match the Source attribute?

<xsl:template match="wix:File/@Source">
    <xsl:attribute name="Source">
        <xsl:value-of select="concat(substring(., 1, 18), '..\artifacts\msi\web\', substring(., 20))"/>
    </xsl:attribute>        
</xsl:template>

Upvotes: 2

Related Questions