问题
I am using an xslt transform on the harvested files file in a WIX installation. I am trying to mark all files in certain directories as permanent. In order to do this I have to do a contains to see if the folder name is in the source attribute. Below is one of the Nodes and then the transform. Any help would be greatly appreciated.
<Component Id="cmpE4293ADC65367393D7A7630023A43F89" Directory="dirAFEA15D2A28EA2E6080FAD1EE1935E0A" Guid="{691DB98F-E5F4-4979-B2E5-63E14AF8A328}">
<File Id="filE11EC7DCDC230815BECFE0925B1F3DC4" KeyPath="yes" Source="$(var.publishDir)\WebConfig\appSettings.config" />
</Component>
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:wix="http://schemas.microsoft.com/wix/2006/wi"
xmlns="http://schemas.microsoft.com/wix/2006/wi"
xmlns:msxml="urn:schemas-microsoft-com:xslt"
exclude-result-prefixes="wix">
<xsl:template match="wix:Wix">
<xsl:copy>
<xsl:apply-templates select="@*" />
<xsl:apply-templates />
</xsl:copy>
</xsl:template>
<xsl:template match="wix:Component">
<!-- Just copy the tag itself -->
<xsl:copy>
<xsl:variable name="fvsys" >
<xsl:value-of select="node()/File/@Source"/>
</xsl:variable>
<!-- Copy all attributes -->
<xsl:apply-templates select="@*" />
<!-- Here comes the distinction: if you find our special component, do some special things -->
<xsl:choose>
<!-- Note that the string is translated to all lower case, so you don't have to care about being case sensitive or not -->
<xsl:when test="contains($fvsys, 'WebConfig')">
<!-- Here we will add the Permanent-attribute to this very special component -->
<xsl:attribute name="Permanent">yes</xsl:attribute>
</xsl:when>
</xsl:choose>
<xsl:choose>
<!-- Note that the string is translated to all lower case, so you don't have to care about being case sensitive or not -->
<xsl:when test="contains($fvsys, 'DocumentConversions')">
<!-- Here we will add the Permanent-attribute to this very special component -->
<xsl:attribute name="Permanent">yes</xsl:attribute>
</xsl:when>
</xsl:choose>
<!-- Now take the rest of the inner tag -->
<xsl:apply-templates select="node()" />
</xsl:copy>
</xsl:template>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()" />
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
回答1:
I am assuming you want to add the Permanent
to the Component
node, as opposed to the File
node?
If so, the problem is with how you define the fvsys
variable:
<xsl:variable name="fvsys" >
<xsl:value-of select="node()/File/@Source"/>
</xsl:variable>
There are two problems here. Firstly, because you are already positioned on a Component
node, this will be looking for a File
node which is a "grand-child" as opposed to a direct child. Secondly, it looks like the File
node is also part of the wix
namespace, so will also need to include the prefix.
Try this instead
<xsl:variable name="fvsys" select="wiz:File/@Source" />
Note the use of the select
on the variable itself. In your original version you creating a copy of the value of the attribute, but in the latter declaration it is still referencing the attribute directly.
回答2:
Thanks for the answer I just found one that works here is my transform, this worked for my needs. It marks each file inside the folder as permanent.
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:wix="http://schemas.microsoft.com/wix/2006/wi"
xmlns="http://schemas.microsoft.com/wix/2006/wi"
xmlns:msxml="urn:schemas-microsoft-com:xslt"
exclude-result-prefixes="wix">
<xsl:template match="wix:Wix">
<xsl:copy>
<xsl:apply-templates select="@*" />
<xsl:apply-templates />
</xsl:copy>
</xsl:template>
<xsl:template match="wix:Component">
<!-- Just copy the tag itself -->
<xsl:copy>
<xsl:variable name="fvsys" >
<xsl:value-of select="*[local-name()='File']/@Source"/>
</xsl:variable>
<!-- Copy all attributes -->
<xsl:apply-templates select="@*" />
<!-- This will mark all files in the WebConfig folder as permanent -->
<xsl:choose>
<!-- Note that the string is translated to all lower case, so you don't have to care about being case sensitive or not -->
<xsl:when test="contains($fvsys, 'WebConfig\')">
<!-- Here we will add the Permanent-attribute to this very special component -->
<xsl:attribute name="Permanent">yes</xsl:attribute>
</xsl:when>
</xsl:choose>
<xsl:choose>
<!-- This will mark all files in the DocumentConversions folder as permanent -->
<xsl:when test="contains($fvsys, 'DocumentConversions\')">
<!-- Here we will add the Permanent-attribute to this very special component -->
<xsl:attribute name="Permanent">yes</xsl:attribute>
</xsl:when>
</xsl:choose>
<!-- Now take the rest of the inner tag -->
<xsl:apply-templates select="node()" />
</xsl:copy>
</xsl:template>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()" />
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
来源:https://stackoverflow.com/questions/26076351/xslt-add-attribute-to-parent-based-on-child-attribute-value-containing-a-specif