Search code examples
xmlxsltxmlstarlet

xmlstarlet xslt transform failing to remove nodes


I'm attempting to use xmlstarlet to work on a large group of .Net project files, however attempting this simple transform using xsl to remove ProductVersion isn't working.

I have tried //ProductVersion, could the namespace in this instance be causing issues?

run.cmd

SET ProjFile=test.vbproj
SET TempFile=%TEMP%\temp.proj
xml tr clean.xsl %ProjFile% > %TempFile%
move /Y %TempFile% %ProjFile%

clean.xsl

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

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

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

    <xsl:template match="ProductVersion"/>
</xsl:stylesheet>

test.vbproj

<?xml version="1.0" encoding="UTF-8"?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="Build" ToolsVersion="4.0">
  <PropertyGroup>
    <ProductVersion>9.0.30729</ProductVersion>
    <SchemaVersion>2.0</SchemaVersion>
  </PropertyGroup>
</Project>

Solution

  • You can do this directly with the xmlstarlet edit subcommand:

    xmlstarlet ed \
      -N ms=http://schemas.microsoft.com/developer/msbuild/2003 \
      -d '//ms:ProductVersion' test.vbproj
    

    Alternately, you can modify your XSLT to respect the namespace:

    <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                    xmlns:ms="http://schemas.microsoft.com/developer/msbuild/2003">
        <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
        <xsl:strip-space elements="*"/>
        <xsl:template match='@*|node()'>
            <xsl:copy>
                <xsl:apply-templates select='@*|node()'/>
            </xsl:copy>
        </xsl:template>
        <xsl:template match="ms:ProductVersion"/>
    </xsl:stylesheet>