Home > Enterprise >  How to use ends-with function in my xslt file
How to use ends-with function in my xslt file

Time:03-24

I have this example of a wix (.wxs) file:

<?xml version="1.0" encoding="utf-8"?>
<Wix xmlns="http://schemas.microsoft.com/wix/2006/wi">
    <Fragment>
        <DirectoryRef Id="INSTALLLOCATION">
            <Component Id="cmpE48528C4F5932A4EFD89F7331108F45D" Guid="*">
                <File Id="filF1B84C2C6BCC693224B7A7959445B74F" KeyPath="yes" Source="$(var.SourceDir)\MyFont.ttf" />
            </Component>
            <Component Id="cmpE48528C4F5932A4EFD89F7331108F45D" Guid="*">
                <File Id="filF1B84C2C6BCC693224B7A7959445B74F" KeyPath="yes" Source="$(var.SourceDir)\Newtonsoft.Json.dll" />
            </Component>
        </DirectoryRef>
    </Fragment>
</Wix>

And now I want to add the atribute TrueType="yes" to all the File elements where the Source ends with 'ttf'.

So it should look like this:

<?xml version="1.0" encoding="utf-8"?>
<Wix xmlns="http://schemas.microsoft.com/wix/2006/wi">
    <Fragment>
        <DirectoryRef Id="INSTALLLOCATION">
            <Component Id="cmpE48528C4F5932A4EFD89F7331108F45D" Guid="*">
                <File Id="filF1B84C2C6BCC693224B7A7959445B74F" KeyPath="yes" TrueType="yes" Source="$(var.SourceDir)\MyFont.ttf" />
            </Component>
            <Component Id="cmpE48528C4F5932A4EFD89F7331108F45D" Guid="*">
                <File Id="filF1B84C2C6BCC693224B7A7959445B74F" KeyPath="yes" Source="$(var.SourceDir)\Newtonsoft.Json.dll" />
            </Component>
        </DirectoryRef>
    </Fragment>
</Wix>

This is the XSLT I have so far, but it doesn't work (yet):

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
            xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
            xmlns:msxsl="urn:schemas-microsoft-com:xslt"
            exclude-result-prefixes="msxsl"
            xmlns:wix="http://schemas.microsoft.com/wix/2006/wi"
            xmlns:my="my:my">

    <xsl:output method="xml" indent="yes"/>

    <xsl:strip-space elements="*"/>

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

    <xsl:template match='//File[ends-with(@Source , 'ttf')]'>
        <xsl:attribute name="TrueType">
            <xsl:value-of select="yes"/>
        </xsl:attribute>
    </xsl:template>
</xsl:stylesheet>

How do I apply this ends-with() function?

CodePudding user response:

Here's a way you could do this :

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:wi="http://schemas.microsoft.com/wix/2006/wi"
    version="1.0">

  <xsl:output method="xml" indent="yes"/>
  
  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="wi:File">
    <xsl:copy>
      <xsl:copy-of select="@*"/>
      <xsl:if test="substring(@Source,string-length(@Source)-2)='ttf'">
        <xsl:attribute name="TrueType">yes</xsl:attribute>
      </xsl:if>
    </xsl:copy>
  </xsl:template>
  
</xsl:stylesheet>

See it working here : https://xsltfiddle.liberty-development.net/pNP54et

  • Related