0

I have an xml

<ApplicationInsights xmlns="http://schemas.microsoft.com/ApplicationInsights/2013/Settings">
<InstrumentationKey>xxx-yuyu</InstrumentationKey>
</ApplicationInsights>

I have an xslt

<xsl:transform version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

 <xsl:output omit-xml-declaration="yes" indent="yes"/>

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

 <xsl:template match="InstrumentationKey">
    <InstrumentationKey>BBB</InstrumentationKey>
 </xsl:template>

</xsl:transform>

This just will not replace InstrumentationKey. It only works when I remove xmlns="http://schemas.microsoft.com/ApplicationInsights/2013/Settings" from the ApplicationInsights.

Any idea where I'm going wrong?

user7786267
  • 137
  • 12
  • I'm closing this as a duplicate because the same question comes up so often. Don't take that amiss; it's an elephant trap in the language and you can't be expected to know that everyone else is falling into the same trap. For other answers, just search for "XSLT default namespace". – Michael Kay Jan 09 '20 at 17:59

1 Answers1

0

You need to handle namespaces properly. Check it out the following XSLT:

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:ns2="http://schemas.microsoft.com/ApplicationInsights/2013/Settings" exclude-result-prefixes="ns2">
    <xsl:output omit-xml-declaration="yes" indent="yes"/>

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

    <xsl:template match="ns2:InstrumentationKey">
        <xsl:element name="{name()}" namespace="{namespace-uri()}">
            <xsl:text>BBB</xsl:text>
        </xsl:element>
        <!--<InstrumentationKey>BBB</InstrumentationKey>-->
    </xsl:template>
</xsl:stylesheet>
Yitzhak Khabinsky
  • 18,471
  • 2
  • 15
  • 21