<!-- Identity transform by default -->
<xsl:template match="node() | @*">
<xsl:copy>
<xsl:apply-templates select="node() | @*"/>
</xsl:copy>
</xsl:template>
<!-- Override identity transform for elements with blank namespace -->
<xsl:template match="*[namespace-uri() = '']">
<xsl:element name="{local-name()}" namespace="http://thisns.com/">
<xsl:apply-templates select="node() | @*"/>
</xsl:element>
</xsl:template>
<!-- Override identity transform for attributes with blank namespace -->
<xsl:template match="@*[namespace-uri() = '']">
<xsl:attribute name="{local-name()}" namespace="http://thisns.com/"><xsl:value-of select="."/></xsl:attribute>
</xsl:template>
这将产生与以下类似的结果:
<x:doc xmlns:x="http://thisns.com/">
<x:node x:property="true">
this part passes validation
</x:node>
<node xp_0:property="false" xmlns="http://thisns.com/" xmlns:xp_0="http://thisns.com/">
this part does not pass validation
</node>
</x:doc>
请注意,第二个<节点>仍然没有名称空间前缀,但由于xmlns=属性,它现在被视为同一名称空间的一部分。