The pattern you're looking for is the "modified identity transform". The basis of this approach is the identity transform rule, the first template rule in the stylesheet below. Each rule after that represents an exception to the copying behavior.
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<!-- By default, copy all nodes unchanged -->
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<!-- But strip out <foo> elements (including their content) -->
<xsl:template match="foo"/>
<!-- For <bar> elements, strip out start & end tags, but leave content -->
<xsl:template match="bar">
<xsl:apply-templates/>
</xsl:template>
<!-- For <bat> elements, insert an attribute and append a child -->
<xsl:template match="bat">
<xsl:copy>
<xsl:apply-templates select="@*"/>
<xsl:attribute name="id">123</xsl:attribute>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
What's least satisfying to me about the above is the duplication of logic found in the last template rule. That's a lot of code for just adding one attribute. And imagine if we need a bunch of these. Here's another approach that allows us to be more surgically precise in what we want to override:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<!-- By default, copy all nodes unchanged -->
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@*"/>
<xsl:apply-templates mode="add-atts" select="."/>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
<!-- By default, don't add any attributes -->
<xsl:template mode="add-atts" match="*"/>
<!-- For <bat> elements, insert an "id" attribute -->
<xsl:template mode="add-atts" match="bat">
<xsl:attribute name="id">123</xsl:attribute>
</xsl:template>
</xsl:stylesheet>
Finally, this can be carried much further, using a different mode for each kind of edit you might want to make:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<!-- For <bat> elements, insert an "id" attribute -->
<xsl:template mode="add-atts" match="bat">
<xsl:attribute name="id">123</xsl:attribute>
</xsl:template>
<!-- Append <new-element/> to <bat> -->
<xsl:template mode="append" match="bat">
<new-element/>
</xsl:template>
<!-- Insert an element in <foo> content -->
<xsl:template mode="insert" match="foo">
<inserted/>
</xsl:template>
<!-- Add content before the <bar/> and <bat/> elements -->
<xsl:template mode="before" match="bar | bat">
<before-bat-and-bar/>
</xsl:template>
<!-- Add content only after <bat/> -->
<xsl:template mode="after" match="bat">
<after-bat/>
</xsl:template>
<!-- Here's the boilerplate code -->
<!-- By default, copy all nodes unchanged -->
<xsl:template match="@* | node()">
<xsl:apply-templates mode="before" select="."/>
<xsl:copy>
<xsl:apply-templates select="@*"/>
<xsl:apply-templates mode="add-atts" select="."/>
<xsl:apply-templates mode="insert" select="."/>
<xsl:apply-templates/>
<xsl:apply-templates mode="append" select="."/>
</xsl:copy>
<xsl:apply-templates mode="after" select="."/>
</xsl:template>
<!-- By default, don't add anything -->
<xsl:template mode="add-atts" match="*"/>
<xsl:template mode="insert" match="*"/>
<xsl:template mode="append" match="*"/>
<xsl:template mode="before" match="@* | node()"/>
<xsl:template mode="after" match="@* | node()"/>
</xsl:stylesheet>
In XSLT 2.0, some of the boilerplate can be slightly simplified, thanks to multi-mode template rules:
<!-- By default, don't add anything -->
<xsl:template mode="add-atts
insert
append
before
after" match="@* | node()"/>
I sometimes use all of these custom modes in the same stylesheet, but more often than not I add them lazily--as needed.